Skip to content

Instantly share code, notes, and snippets.

@tusharmath
Created August 14, 2016 14:08
Show Gist options
  • Save tusharmath/a51678511248fc1e36a41364cbd8508d to your computer and use it in GitHub Desktop.
Save tusharmath/a51678511248fc1e36a41364cbd8508d to your computer and use it in GitHub Desktop.
Rx stream throttler via requestAnimationFrame
import raf from 'raf'
import {Observable as O} from 'rx'
function RAFThrottle (source) {
return O.create(observer => {
let frame = null
function queueValue (value) {
if (frame) raf.cancel(frame)
frame = raf(() => observer.onNext(value))
}
return source.subscribe(
queueValue,
err => observer.onError(err),
() => observer.onCompleted()
)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment