Skip to content

Instantly share code, notes, and snippets.

@nash403
Created May 4, 2016 13:48
Show Gist options
  • Save nash403/efd3fb54a6d612182f50183842d61ace to your computer and use it in GitHub Desktop.
Save nash403/efd3fb54a6d612182f50183842d61ace to your computer and use it in GitHub Desktop.
Distinguish clicks and multi clicks with RxJS
var tapStream = Rx.Observable.fromEvent(element, 'click');
// multi-tap logic
var multiTapStream = tapStream
.buffer(function() { return tapStream.debounce(250); })
.map(function(list) { return list.length; })
.filter(function(x) { return x >= 2; });
// Same as above, but detects single taps
var singleTapStream = tapStream
.buffer(function() { return tapStream.debounce(250); })
.map(function(list) { return list.length; })
.filter(function(x) { return x === 1; });
// Subscribe to both streams
multiTapStream.subscribe(function (numtaps) {
console.log(numtaps+'x clicked !!!!!!');
});
singleTapStream.subscribe(function (numtaps) {
console.log("clicked !!!!!!");
});
// When we are done !
singleTapStream.dispose();
multiTapStream.dispose();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment