Skip to content

Instantly share code, notes, and snippets.

@tweinfeld
Last active October 22, 2015 06:57
Show Gist options
  • Save tweinfeld/ad59e4b5858ab360ec3f to your computer and use it in GitHub Desktop.
Save tweinfeld/ad59e4b5858ab360ec3f to your computer and use it in GitHub Desktop.
Buffering Debounce with Bacon
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