If your subflow is simply a function node, you would greatly benefit from using the link-call
node and making a simple subroutine.
Each subflow results in multiple nodes being generated every single node inside X the umber of instances. For example: if your subflow has 10 nodes and you have 50 instances, that is 500 nodes.
A Link-call subroutine equivalent would be 10 + 2
[{"id":"03caf84ad794a574","type":"inject","z":"eaa6d97a6ede80ab","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"seven","payload":"7","payloadType":"num","x":170,"y":2900,"wires":[["158d0f3af2d3e05a"]]},{"id":"529d2fd7dc616683","type":"inject","z":"eaa6d97a6ede80ab","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"nine","payload":"9","payloadType":"num","x":170,"y":2940,"wires":[["158d0f3af2d3e05a"]]},{"id":"158d0f3af2d3e05a","type":"link call","z":"eaa6d97a6ede80ab","name":"","links":["4057e7d7e66892ce"],"linkType":"static","timeout":"30","x":370,"y":2920,"wires":[["c943c9c28b42ec77"]]},{"id":"c943c9c28b42ec77","type":"debug","z":"eaa6d97a6ede80ab","name":"decorated payload","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":620,"y":2920,"wires":[]},{"id":"2e3f6c78987eb8d1","type":"group","z":"eaa6d97a6ede80ab","name":"My subroutine (much lighter than a subflow)","style":{"fill":"#92d04f","fill-opacity":"0.22","label":true},"nodes":["4057e7d7e66892ce","b5344392fd72a5ad","8c0101828c34f512"],"x":124,"y":2999,"w":312,"h":82},{"id":"4057e7d7e66892ce","type":"link in","z":"eaa6d97a6ede80ab","g":"2e3f6c78987eb8d1","name":"decorate-payload","links":[],"x":165,"y":3040,"wires":[["b5344392fd72a5ad"]]},{"id":"b5344392fd72a5ad","type":"function","z":"eaa6d97a6ede80ab","g":"2e3f6c78987eb8d1","name":"decorate","func":"// set some Values in a UIBUILDER driven GUI\n\n// define a basic default set - for the case garbage comes in\n// so we can handle or debug better within the GUI\n\nconst myMsg = RED.util.cloneMessage(msg);\n\n// here we build the parameters we need in the GUI\n// this values would be nice to define outside the subflow \n// so the (sub)flow has not to be copied around and changed\nconst myNewPayload = { \n \"elementtype\": \"id\", // could be id/class/attribute\n \"elementselector\": \"ccc-hatch\", // any CSS selector\n \"todo\": \"set\", // set/add/remove/toggle\n \"targettype\": \"data-status\", // innerHTML, any attribute e.g. data-* (more to come)\n \"targetvalue\": msg.payload, // the value whatever you want to be\n originalTopic: msg.topic\n};\n\nmyMsg.topic = \"updateElement: ccc-hatch\";\nmyMsg.payload = myNewPayload;\n\nreturn myMsg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":3040,"wires":[["8c0101828c34f512"]]},{"id":"8c0101828c34f512","type":"link out","z":"eaa6d97a6ede80ab","g":"2e3f6c78987eb8d1","name":"link out 1","mode":"return","links":[],"x":395,"y":3040,"wires":[]}]
some additional notes:
- your function disregards the original
msg
. This is not advised. One day, you will encounter an issue where the original msg
has a property that is required downstream in the flow.
- you
let
create myNewPayload
as a string then change it to an object. Better to simply const
create the object const myNewPayload = { ... }
- you
let
create myMsg
with a topic and payload, then overwrite them below. Better to const
create the myMsg
object e.g. const myMsg = {}
then simply append final values `
Here is how I restructured your function
const myMsg = RED.util.cloneMessage(msg);
const myNewPayload = {
"elementtype": "id", // could be id/class/attribute
"elementselector": "ccc-hatch", // any CSS selector
"todo": "set", // set/add/remove/toggle
"targettype": "data-status", // innerHTML, any attribute e.g. data-* (more to come)
"targetvalue": msg.payload, // the value whatever you want to be
originalTopic: msg.topic
};
myMsg.topic = "updateElement: ccc-hatch";
myMsg.payload = myNewPayload;
return myMsg;