Last active
December 8, 2016 21:16
-
-
Save ashish173/fc8881a6ef6e6727914a408fa6eff163 to your computer and use it in GitHub Desktop.
Buffer operator
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
//Create an observable that emits a value every second | |
const myInterval = Rx.Observable.interval(1000); | |
//Create an observable that emits every time button is clicked | |
var button = document.getElementById('clickButton'); | |
const bufferBy = Rx.Observable.fromEvent(button, 'click'); | |
/* | |
Collect all values emitted by our interval observable until we click document. This will cause the bufferBy Observable to emit a value, satisfying the buffer. Pass us all collected values since last buffer as an array. | |
*/ | |
const myBufferedInterval = myInterval.buffer(bufferBy); | |
/* ASCII REPRESENTATION | |
* | |
* --x----x----x----x-------> | |
* | |
* ----c----------c----c----> | |
* | |
* ----x----------xx---x----> | |
*/ | |
//Print values to console | |
const subscribe = myBufferedInterval.subscribe( | |
val => { | |
console.clear(); | |
console.log('new notification ids', val); | |
var numberOfNotifications = val.length; | |
console.log('New Notifications ', numberOfNotifications) | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment