diff options
-rw-r--r-- | tools/schedule.js | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/tools/schedule.js b/tools/schedule.js index 42ec127..c9c39eb 100644 --- a/tools/schedule.js +++ b/tools/schedule.js @@ -25,6 +25,7 @@ exports.Queue = function(delayms, maxExport, call){ let open = true; // Is dequeue() allowed to be called (i.e., has the timeout expired?) let disab = true; // Is the queue disabled? let pauseCall = null; // Either null or a function which will replace the next dequeue() call. + this.size = 0; this.enqueue = function(job){ if (! job[0]) return; @@ -36,12 +37,13 @@ exports.Queue = function(delayms, maxExport, call){ } jobs[prio].splice(0, Math.abs(bs(jobs[prio], job, (el, ne) => el.maxr() - ne.maxr())), job); // These were sorted like this so that getNumOrAll could use [0] or [.length-1] or .pop instead of having to re-sort lists repetitively. - if (open) dequeue(); + this.size += job.data.length; + if (open) this.dequeue(); } this.enable = function(){ disab = false; - if (open) dequeue(); + if (open) this.dequeue(); } this.disable = function(){ @@ -52,7 +54,7 @@ exports.Queue = function(delayms, maxExport, call){ pauseCall = call; } - function dequeue(){ + this.dequeue = function(){ // Wraps getNumOrAll, managing open/disab, and concatenating possibly multiple layers if (disab){ open = true; @@ -72,8 +74,9 @@ exports.Queue = function(delayms, maxExport, call){ while (prios.length > 0 && data.length < maxExport){ data.push(...getNumOrAll(prios[prios.length-1], maxExport-data.length)); } + this.size -= data.length; call(data); - setTimeout(dequeue, delayms); + if (delayms) setTimeout(this.dequeue, delayms); } function getNumOrAll(prio, num){ |