aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHolden Rohrer <hr@hrhr.dev>2020-01-20 20:52:51 -0500
committerHolden Rohrer <hr@hrhr.dev>2020-01-20 20:55:18 -0500
commitb9dfc89f239cdecaf546238ed5dae055077e315f (patch)
tree34ccfb18fa7d66b781060d9819b4073a03ea3c0b
parent1e16858608e40590941a52b9d080ccbc031c5ce9 (diff)
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)
-rw-r--r--tools/schedule.js11
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){