Skip to content

Instantly share code, notes, and snippets.

@Vikasg7
Last active November 27, 2021 15:04
Show Gist options
  • Save Vikasg7/70eb7743f34fc23344b0128acdd125c9 to your computer and use it in GitHub Desktop.
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.
// 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