Created
May 4, 2016 13:48
-
-
Save nash403/efd3fb54a6d612182f50183842d61ace to your computer and use it in GitHub Desktop.
Distinguish clicks and multi clicks 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
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