diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/queue.js | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/tools/queue.js b/tools/queue.js new file mode 100644 index 0000000..ebe17cd --- /dev/null +++ b/tools/queue.js @@ -0,0 +1,24 @@ +// A generalized queue object for rapid responses (doesn't do prioritization or algorithmic timesharing) + +module.exports = function(delayms, maxExport, call){ + let queue = []; + let open = true; + let getObjs = (maxExport == 1) ? + () => queue.shift() : + () => queue.splice(0, maxExport) ; + this.enqueue = function(){ + for (let i = 0; i < arguments.length; i++) + queue.push(arguments[i]); + if (open) dequeue(); + } + + function dequeue(){ + if (queue.length == 0) + open = true; + else { + open = false; + call(getObjs()); + setTimeout(dequeue, delayms); + } + } +} |