Last active
November 27, 2021 15:04
-
-
Save Vikasg7/70eb7743f34fc23344b0128acdd125c9 to your computer and use it in GitHub Desktop.
A custom Rxjs operator that takes the source observable and repeats the latest value on an interval while waiting for the new values to arrive.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage:- https://replit.com/@Vikasg7/repeatLatestOnInterval | |
// Clojure version:- https://github.com/Vikasg7/clj-snake/blob/main/src/clj_snake/utils.clj | |
const repeatLatestOnInterval = (delayInMS) => (source) => | |
new Observable((observer) => { | |
let id = null | |
const next = (value) => { | |
observer.next(value) // sending downstream | |
clearTimeout(id) | |
id = setTimeout(next, delayInMS, value) | |
} | |
const error = (err) => { | |
clearTimeout(id) | |
observer.error(err) | |
} | |
const complete = () => { | |
clearTimeout(id) | |
observer.complete() | |
} | |
return source.subscribe({next, error, complete}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment