aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/jarvis.js16
-rw-r--r--tools/queue.js24
2 files changed, 27 insertions, 13 deletions
diff --git a/examples/jarvis.js b/examples/jarvis.js
index d60bc44..00b4610 100644
--- a/examples/jarvis.js
+++ b/examples/jarvis.js
@@ -5,6 +5,7 @@ const Socket = require('../socket');
const getdims = require('../utils/getdims');
const tilekeys = require('../utils/tilekeys');
const Search = require('../tools/search');
+const Queue = require('../tools/queue');
var main = new Socket();
@@ -57,21 +58,10 @@ function identity(sender){
let response = new Space();
response.adhoc('yes, my liege');
-let queue = []; // Must be expanded to handle actual use cases
-let open = true;
-function dequeue(){
- if (queue.length == 0)
- open = true;
- else{
- open = false;
- main.write(queue.shift()) // Make more general (possibly a function pointer or a conf variable)
- setTimeout(dequeue, 1000);
- }
-}
+let writes = new Queue(1000, 200, (elems) => main.write(elems));
function respond(coord){
console.log('called at', coord);
response.loc = coord;
- queue.push(response.towrite());
- if (open) dequeue();
+ writes.enqueue(...response.towrite());
}
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);
+ }
+ }
+}