Last active
October 22, 2015 06:57
-
-
Save tweinfeld/ad59e4b5858ab360ec3f to your computer and use it in GitHub Desktop.
Buffering Debounce with Bacon
This file contains hidden or 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
const QUEUE_TIMEOUT = 500, | |
QUEUE_DEBOUNCE = 5; | |
Bacon | |
.repeatedly(10, ["*", "-"]) | |
.flatMapLatest((function(buffer, stamp) { | |
return function(request) { | |
buffer.length === 0 && (stamp = Date.now()); | |
buffer.push(request); | |
return (stamp + QUEUE_TIMEOUT < Date.now()) ? Bacon.once(buffer.splice(0)) : Bacon.fromBinder(function(sink) { | |
var timer = setTimeout(function() { | |
sink(buffer.splice(0)); | |
}, QUEUE_DEBOUNCE); | |
return clearTimeout.bind(undefined, timer); | |
}); | |
} | |
})([])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment