buymymojo.net/node_modules/throttles/dist/index.js
2021-07-02 19:29:34 +10:00

21 lines
359 B
JavaScript

module.exports = function (limit) {
limit = limit || 1;
var queue=[], wip=0;
function toAdd(fn) {
queue.push(fn) > 1 || run(); // initializes if 1st
}
function isDone() {
wip--; // make room for next
run();
}
function run() {
if (wip < limit && queue.length > 0) {
queue.shift()(); wip++; // is now WIP
}
}
return [toAdd, isDone];
}