This flow works - as best I can tell.
The node counters are NOT zero counting which I think helps with seeing which message you are viewing.
Could you help me with a more tidy way with the variable j
in the scheme of things.
That doesn't mean directly you. But more a general request for help.
Sorry the main function
node still has a bit of old stuff in it.
That's the node with the j
variable in it.
The node shows how many entries in the array. NOT zero counting.
So as the node shows the index number, I wanted it to also be non-zero counting.
Found a couple of spelling mistakes.
[{"id":"8e458e68.2f8d88","type":"function","z":"184dc884.7aba5f","name":"Array","func":"//\nvar output = \"\"; // This is what returns the indexed value.\nvar i = context.get('pointer') || 0;\nvar j = 0;\nvar queue_array = flow.get(\"queue\") // I'm guessing this checks if the array exists.\nif (!Array.isArray(queue_array))\n{\n // If not, set up array.\n node.warn(\"Initialising array\");\n queue_array = [];\n flow.set('queue',queue_array);\n node.warn(\"Array set up.\");\n}\n\nsize = queue_array.length;\n\nif (msg.topic == \"CONTROL\")\n{\n // Stuff to be done with array.\n if (msg.payload == \"NEXT\")\n {\n // Display next item\n i = i + 1;\n j = i + 1;\n node.warn(\"Next entry \" + j);\n output = queue_array[i];\n msg.payload = output;\n context.set('pointer',i); // Save the value.\n node.status(size + \" \" + j);\n return msg;\n } else\n if (msg.payload == \"PREVIOUS\")\n {\n // Display previous item\n i = i - 1;\n j = i + 1;\n node.warn(\"Previous entry \" + j);\n output = queue_array[i];\n msg.payload = output;\n context.set('pointer',i); // Save the value.\n node.status(size + \" \" + j);\n return msg;\n } else\n if (msg.payload == \"WIPE\")\n {\n // Delete this item\n } else\n if (msg.payload == \"WIPE ALL\")\n {\n // Wipe the whole array\n node.warn(\"Initialising array\");\n queue_array = [];\n flow.set('queue',queue_array);\n node.warn(\"Array Wiped.\");\n return;\n }\n}\n\nnode.warn(\"saving data to array\");\n\nqueue_array.push(msg.payload);\n\nsize = queue_array.length;\n\ncontext.set('pointer',i); // Save the value - for what ever reason.\n\nflow.set('queue',queue_array);\nnode.status(size + \" \" + pointer);\n\nreturn;\n","outputs":1,"noerr":0,"x":490,"y":1320,"wires":[["e09fc19c.cf203"]]},{"id":"c0ef557b.139f1","type":"template","z":"184dc884.7aba5f","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Message {{payload}}","output":"str","x":230,"y":1280,"wires":[["8e458e68.2f8d88","fcb9dca0.971b5"]]},{"id":"e09fc19c.cf203","type":"debug","z":"184dc884.7aba5f","name":"Index result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":650,"y":1320,"wires":[]},{"id":"c4bbdffc.81ab78","type":"inject","z":"184dc884.7aba5f","name":"Next","topic":"CONTROL","payload":"NEXT","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":1360,"wires":[["8e458e68.2f8d88"]]},{"id":"16235d1e.a6c2db","type":"inject","z":"184dc884.7aba5f","name":"Previous","topic":"CONTROL","payload":"PREVIOUS","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":1390,"wires":[["8e458e68.2f8d88"]]},{"id":"8d565471.b8f5a","type":"inject","z":"184dc884.7aba5f","name":"Wipe All","topic":"CONTROL","payload":"WIPE ALL","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":1470,"wires":[["8e458e68.2f8d88"]]},{"id":"bb6554dd.38b188","type":"inject","z":"184dc884.7aba5f","name":"Wipe","topic":"CONTROL","payload":"WIPE","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":1440,"wires":[["8e458e68.2f8d88"]]},{"id":"26162964.640a16","type":"random","z":"184dc884.7aba5f","name":"Random number","low":"1","high":"10","inte":"true","property":"payload","x":200,"y":1250,"wires":[["c0ef557b.139f1"]]},{"id":"fcb9dca0.971b5","type":"debug","z":"184dc884.7aba5f","name":"History","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":500,"y":1230,"wires":[]},{"id":"638c9635.68a788","type":"inject","z":"184dc884.7aba5f","name":"Add new item to array","topic":"","payload":"blah_","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":1220,"wires":[["26162964.640a16"]]}]
And this is for the single wipe
part of the function
node code:
// Delete this item
j = i + 1;
node.warn("Removing item " + j + " which is " + queue_array[i]);
queue_array.splice(i,1);
flow.set('queue', queue_array);
return;
All seems to work as I want.
(Just copy/paste this in the code at the delete this item
part is. Which as posted is basically empty.)
Could someone give it a sanity check?
Please.