Skip to content

Instantly share code, notes, and snippets.

@aanand
Last active December 13, 2015 20:09
Show Gist options
  • Save aanand/4968356 to your computer and use it in GitHub Desktop.
Save aanand/4968356 to your computer and use it in GitHub Desktop.
Alternate throttle function for Bacon.js
// var throttled = source.steady(2)
// source: asdf------qwerzxcv---------
// throttled: --a-s-d-f---q-w-e-r-z-x-c-v
Bacon.EventStream.prototype.steady = function(delay) {
var bus = new Bacon.Bus();
var queue = [];
var timeout = null;
var emit = function() {
if (queue.length > 0) {
bus.push(queue.pop());
timeout = window.setTimeout(emit, delay);
} else {
timeout = null;
}
};
this.onValue(function(value) {
queue.push(value);
if (!timeout) timeout = window.setTimeout(emit, delay);
});
return bus;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment