From b9dfc89f239cdecaf546238ed5dae055077e315f Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Mon, 20 Jan 2020 20:52:51 -0500 Subject: scheduler built for external access .dequeue has been made available, along with an infinite delay-time so that a secondary loop can manage multiple schedules at a time. This is paired with .size to make the external loop more efficient (hopefully, baudrate is used optimally) --- tools/schedule.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'tools') 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){ -- cgit