Skip to content

Instantly share code, notes, and snippets.

@rockydd
Created July 27, 2018 03:36
Show Gist options
  • Save rockydd/347f69e4956a1823b9c4c9786e82666c to your computer and use it in GitHub Desktop.
Save rockydd/347f69e4956a1823b9c4c9786e82666c to your computer and use it in GitHub Desktop.
get single-click double-click and triple-click with rxjs
//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