aboutsummaryrefslogtreecommitdiff
path: root/tools/queue.js
blob: ebe17cd0f16ec3aafccded54fca818cec5cda7e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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);
    }
  }
}