LIFO
LIFO - Last in First Out subflow
This subflow receives a series of msg.payloads and builds a queue using LIFO (Last in First out) method. The LIFO size is configurable through subflow environment variables.
This subflow was created by Smart-Tech as part of the ST-One project.
Input
msg.topic == put
Will add the msg.payload to the end of the array. Everytime a new item is received the second output sends the complete queue.
msg.topic == get
Will remove the first item of the stack and will send it in the first output
msg.topic == list
Sends the complete list on the second output.
Outputs
output 1
The first item on the top of the stack. It is sent only when msg.topic == get.
output 2
The complete queue in Array format. It is sent every time a new item is added, removed or when msg.topic == list
Status
Always show the queue size.
Environment Variables
Buffer_Size defines the size of the queue. If a new item arrives and the queue is full, the oldest item will be removed.
[{"id":"ada93e7.0ac0ac","type":"subflow","name":"LIFO","info":"# LIFO\n## Last in First Out subflow\nThis flow receives a series of msg.payloads and builds a queue using LIFO (Last in First out) method. The LIFO size is configurabe through subflow enviroment variables.\n\n### Input\n`msg.topic == put`\nWill add the `msg.payload` to the end of the array. Everytime a new item is received the second output sends the complete queue.\n\n`msg.topic == get`\nWill remove the first item of the stack and will send it in the first output\n\n`msg.topic == list`\nSends the complete list on the second output.\n\n### Output 1\nThe first item on the top of the stack. It is sent only when `msg.topic == get`.\n\n### Output 2\nThe complete queue in Array format. It is sent everytime a new item is added, removed or when `msg.topic == list`\n\n### Status\nAlways show the queue size.\n\n### Enviroment Variables\n`Buffer_Size` defines the size of the queue. If a new item arrives and the queue is full, the oldest item will be removed.","category":"","in":[{"x":102,"y":160,"wires":[{"id":"800335b4.3ebae8"}]}],"out":[{"x":700,"y":60,"wires":[{"id":"6fdeecd7.5a57b4","port":0}]},{"x":880,"y":120,"wires":[{"id":"1efe2fec.11ef8","port":0}]}],"env":[{"name":"Buffer_Size","type":"num","value":"5"}],"inputLabels":["Item"],"outputLabels":["Item","Complete queue"],"icon":"font-awesome/fa-ellipsis-v","status":{"x":1020,"y":180,"wires":[{"id":"5aa3b6a6.319ee8","port":0}]}},{"id":"800335b4.3ebae8","type":"switch","z":"ada93e7.0ac0ac","name":"Topic","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"get","vt":"str"},{"t":"eq","v":"put","vt":"str"},{"t":"eq","v":"list","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":4,"x":250,"y":160,"wires":[["6fdeecd7.5a57b4"],["84b8c04c.99c06"],["1efe2fec.11ef8"],["1efe2fec.11ef8"]]},{"id":"84b8c04c.99c06","type":"function","z":"ada93e7.0ac0ac","name":"put","func":"var queue = flow.get(\"queue\")\n\n\nif (!Array.isArray(queue)){\n queue = [];\n}else if ( queue.length >= env.get(\"Buffer_Size\") ){\n queue.pop(1);\n}\n\nqueue.unshift(msg.payload);\n\nflow.set(\"queue\", queue);\nmsg.payload = queue;\n\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":140,"wires":[["1efe2fec.11ef8"]]},{"id":"6fdeecd7.5a57b4","type":"function","z":"ada93e7.0ac0ac","name":"get","func":"var queue = flow.get(\"queue\")\nvar item;\n\nif (!Array.isArray(queue)){\n queue = [];\n}else{\n item = queue.shift();\n}\n\n\nflow.set(\"queue\", queue)\nmsg.payload = item;\n\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":100,"wires":[["1efe2fec.11ef8"]]},{"id":"1efe2fec.11ef8","type":"change","z":"ada93e7.0ac0ac","name":"list","rules":[{"t":"set","p":"payload","pt":"msg","to":"queue","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":180,"wires":[["5aa3b6a6.319ee8"]]},{"id":"5aa3b6a6.319ee8","type":"change","z":"ada93e7.0ac0ac","name":"Count","rules":[{"t":"set","p":"payload","pt":"msg","to":"$count(msg.payload)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":910,"y":180,"wires":[[]]},{"id":"ca263123.8f9b4","type":"subflow:ada93e7.0ac0ac","z":"ab14b933.6f7428","name":"","env":[],"x":500,"y":160,"wires":[["421f3ce6.3785b4"],["e235969.4731268"]]}]