Last active
October 25, 2016 12:13
-
-
Save shrynx/72ce3e4eeb570ce826bcd3f6db5d743e to your computer and use it in GitHub Desktop.
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
// 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