Skip to content

Instantly share code, notes, and snippets.

@ashish173
Last active December 8, 2016 21:16
Show Gist options
  • Save ashish173/fc8881a6ef6e6727914a408fa6eff163 to your computer and use it in GitHub Desktop.
Save ashish173/fc8881a6ef6e6727914a408fa6eff163 to your computer and use it in GitHub Desktop.
Buffer operator
//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