aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHolden Rohrer <holden.rohrer@gmail.com>2019-12-24 17:47:59 -0500
committerHolden Rohrer <holden.rohrer@gmail.com>2019-12-27 14:52:44 -0500
commit0de6b79203844870e56b8d3e4ae56798a9e43dc8 (patch)
treed3dbb6328a807292b2bf73663c7cc027cc27b305
parent86db15f22b2615ddf89e5cbd3c7a931935827c5e (diff)
Improved jarvis.js queueing
-rw-r--r--examples/jarvis.js25
1 files changed, 11 insertions, 14 deletions
diff --git a/examples/jarvis.js b/examples/jarvis.js
index f5a3ce2..d60bc44 100644
--- a/examples/jarvis.js
+++ b/examples/jarvis.js
@@ -56,25 +56,22 @@ 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 send(){
- if (queue.length == 0) open = true;
+function dequeue(){
+ if (queue.length == 0)
+ open = true;
else{
- main.write(queue.shift());
- setTimeout(send, 1000);
+ open = false;
+ main.write(queue.shift()) // Make more general (possibly a function pointer or a conf variable)
+ setTimeout(dequeue, 1000);
}
}
+
function respond(coord){
- response.loc = coord;
console.log('called at', coord);
- if (open){
- main.write(response.towrite());
- open = false;
- setTimeout(send, 1000);
- } else {
- queue.push(response.towrite());
- }
- send(response.towrite());
- main.write(response.towrite());
+ response.loc = coord;
+ queue.push(response.towrite());
+ if (open) dequeue();
}