Created
July 27, 2018 03:36
-
-
Save rockydd/347f69e4956a1823b9c4c9786e82666c to your computer and use it in GitHub Desktop.
get single-click double-click and triple-click with rxjs
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
//https://stackoverflow.com/questions/37310640/rxjs-buffer-how-to-group-multi-click-events-as-stream/51547034#51547034 | |
const btn = document.querySelector('#btn'); | |
const click$ = Rx.Observable.fromEvent(btn, "click"); | |
const trigger$ = | |
click$.exhaustMap(r => | |
click$ | |
.take(2) | |
.last() | |
.race(click$ | |
.startWith(0) | |
.debounceTime(500) | |
.take(1)) | |
); | |
click$ | |
.buffer(trigger$) | |
.map(l => l.length) | |
.map(x => ({ | |
1: 'single', | |
2: 'double', | |
3: 'tripple' | |
}[x])) | |
.map(c => c + '-click') | |
.subscribe(log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment