Last active
December 13, 2015 20:09
-
-
Save aanand/4968356 to your computer and use it in GitHub Desktop.
Alternate throttle function for Bacon.js
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
// 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