Last active
December 18, 2015 22:49
-
-
Save Havvy/5856889 to your computer and use it in GitHub Desktop.
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
| module.exports = function (delayedFn, period) { | |
| var queue = []; | |
| var queueEmpty = true; | |
| var interval; | |
| function consume () { | |
| if (queue.length === 0) { | |
| queueEmpty = true; | |
| clearInterval(interval); | |
| } else { | |
| delayedFn.apply(null, queue.shift()); | |
| } | |
| } | |
| return function (inputs) { | |
| queue.push(inputs); | |
| if (queueEmpty) { | |
| consume(); | |
| setInterval(consume, period) | |
| queueEmpty = false; | |
| } | |
| } | |
| } |
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
| module.exports = function (delayedFn, period) { | |
| return { | |
| [[prototype]] : Function.prototype | |
| queue: [], | |
| queueEmpty: true, | |
| interval: null, | |
| [[call]] : function (...inputs) { | |
| self.queue.push(inputs); | |
| if (queueEmpty) { | |
| consume(); | |
| setInterval(self.consume, period) | |
| self.queueEmpty = false; | |
| } | |
| }, | |
| consume : function consume () { | |
| if (self.queue.length === 0) { | |
| self.queueEmpty = true; | |
| clearInterval(self.interval); | |
| } else { | |
| delayedFn.call(null, self.queue.shift()); | |
| } | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment