Created
August 14, 2016 14:08
-
-
Save tusharmath/a51678511248fc1e36a41364cbd8508d to your computer and use it in GitHub Desktop.
Rx stream throttler via requestAnimationFrame
This file contains 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
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