Skip to content

Instantly share code, notes, and snippets.

@shrynx
Last active October 25, 2016 12:13
Show Gist options
  • Save shrynx/72ce3e4eeb570ce826bcd3f6db5d743e to your computer and use it in GitHub Desktop.
Save shrynx/72ce3e4eeb570ce826bcd3f6db5d743e to your computer and use it in GitHub Desktop.
// Select the button we need need to listen upon.
const myButton = document.querySelector('#click-me')
// Create a stream of clicks, using fromEvent function,
// by passing the button and the event listener.
const click$ = Rx.Observable.fromEvent(myButton, 'click')
// From the click stream, we create a new stream,
// where we first collect all the clicks
// that happen in 250ms inverval of the previous click
// and group them together.
const dbClick$ = click$.bufferWhen(() => click$.debounceTime(250))
// We then take the size of each group
.map(list => list.length)
// and filter out if the group size is greater than or equal to 2
.filter(length => length >= 2)
// Finally with the doucle click stream, we can now subscribe to it
// and apply any function.
// Here we are just simply logging the double clicks.
const subscriber = dbClick$.subscribe(() => {
console.log('double click');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment