bartbutenaers / node-red-contrib-blockly

A Node Red node for visual programming a function using Blockly
Apache License 2.0
91 stars 22 forks source link

Issue with for loop #56

Closed mannharleen closed 3 years ago

mannharleen commented 4 years ago

Now, I have to admit that its the first time I am using blockly, however, I have been using NR for a long time and so this warrants your attention.

Issue:

My flow looks like this: Inject node sends a msg with payload = [0,1,2] (an array object) image Blockly turns the input msg into 3 distinct msg. image Note: I am sending each msg twice (highlighted with yellow) - this was just for testing. i.e. total = 6 msg obj as output The output on the debug window shows "payload = 2" for all 6 messages However, the console window where node.log hits shows me 0,1&2 correctly.

Expected

The debug window should also show 0,1&2

cymplecy commented 4 years ago

When sending messages in a loop, you need to make sure each message is unique

image

or

image

bartbutenaers commented 4 years ago

Indeed you have to do it like Simon explained. You send an object, then you change that object, you send the SAME object again, and so on... So you end up with N messages, all referring to the SAME object (containing ALL the changes. Please close this issue if it is solved! Bart

mannharleen commented 4 years ago

That makes sense. However, this means the blocky node is not a true reflection of the existing function node. The NR function node clones the msg by default; as a matter of fact if I copied the js code produced by blockly node and ran it inside the NR function node, it gives me 0,1&2 (as intended).

I think this should be mentioned in the README.md of this project, and any more inherent differences if any. I am happy to assist in creating a PR if you agree.

bartbutenaers commented 4 years ago

That is new to me. Can you please share your flow , so I can have a look at it.

bartbutenaers commented 4 years ago

blocky node is not a true reflection of the existing function node

Well the Blockly node had the same cloning behaviour as the Function node, at the time I have developed it. Now I remember it again: afterwards in Node-RED 1.0 the cloning has been changed. In this article, you can read about it: "We’re changing the default approach to cloning used by the Function node to prevent this issue" ...

mannharleen commented 4 years ago

Yes, its because after 1.0.0 NR function node clones every msg object when using node.send().

Here is my flow, just in case you still need it:

[{"id":"546ef6ca.37e8c8","type":"Blockly","z":"14bb364f.9ba5ea","language":"en","func":"var j;\n\n\nvar j_list = (msg['payload']);\nfor (var j_index in j_list) {\n  j = j_list[j_index];\n  msg['payload'] = j;\n  node.log(msg.payload);\n  node.send(msg);\n  node.status({fill:\"blue\", shape:\"ring\", text:j});\n  node.send([msg]);\n}\n","workspaceXml":"<xml xmlns=\"http://www.w3.org/1999/xhtml\"><variables><variable type=\"\" id=\"A-,$O)LJq:9F9`-d{KHW\">j</variable></variables><block type=\"controls_forEach\" id=\"Y9)A;rl]~qE[c2Z^D9^D\" x=\"-380\" y=\"81\"><field name=\"VAR\" id=\"A-,$O)LJq:9F9`-d{KHW\" variabletype=\"\">j</field><value name=\"LIST\"><block type=\"node_object_get\" id=\"}#5dxn}3IB|NXJAM,`{-\"><mutation action=\"GET\"></mutation><field name=\"action\">GET</field><value name=\"object\"><shadow type=\"node_msg\" id=\"m|CMl(CCa?f%aD;Un%dX\"></shadow></value><value name=\"field_name\"><shadow type=\"text\" id=\"V~mP:|q!jG;})gyw[PMs\"><field name=\"TEXT\">payload</field></shadow></value></block></value><statement name=\"DO\"><block type=\"node_object_set\" id=\"tX4ks@RR}tH+S.|XiY2~\"><value name=\"object_field\"><shadow type=\"node_msg\" id=\"4~`!?yEIKU$(/Cuwdwh#\"></shadow></value><value name=\"field_name\"><shadow type=\"text\" id=\"i{3+N/W;;|ScZhI/t#Rd\"><field name=\"TEXT\">payload</field></shadow></value><value name=\"value_field\"><shadow type=\"text\" id=\"{b,qtnrhtrNb$ru9V9eo\"><field name=\"TEXT\"></field></shadow><block type=\"variables_get\" id=\"T9WyQq)^k~%sQj!IZifW\"><field name=\"VAR\" id=\"A-,$O)LJq:9F9`-d{KHW\" variabletype=\"\">j</field></block></value><next><block type=\"misc_javascript\" id=\"tt`U4uMw5%NxqibY1U(o\"><value name=\"STATEMENT\"><shadow type=\"text\" id=\"/D-O+tIQP/#[xa{#vUUA\"><field name=\"TEXT\">node.log(msg.payload)</field></shadow></value><next><block type=\"misc_javascript\" id=\"FW3NoUolWpP*qnvouQ|_\"><value name=\"STATEMENT\"><shadow type=\"text\" id=\"/TYpW@p4%StJ_=blGbu}\"><field name=\"TEXT\">node.send(msg)</field></shadow></value><next><block type=\"node_status\" id=\"z#BZk+(jKx=WEnT%F!T{\"><field name=\"COLOUR\">#0000FF</field><field name=\"SHAPE\">RING</field><value name=\"TEXT_INPUT\"><shadow type=\"text\" id=\"3vhhXV/Dy2yduYX.ebv[\"><field name=\"TEXT\">j</field></shadow><block type=\"variables_get\" id=\"KaF[/{2Py+#;iqNRL;e9\"><field name=\"VAR\" id=\"A-,$O)LJq:9F9`-d{KHW\" variabletype=\"\">j</field></block></value><next><block type=\"node_send\" id=\"@S:jAz@3q,%fy^F*;d-v\"><field name=\"OUTPUT_NR\">1</field><value name=\"MESSAGE_INPUT\"><shadow type=\"node_msg\" id=\"`j{=u]9_#1=6-)t}rY~_\"></shadow><block type=\"node_msg\" id=\"k~lF6F3{H.h_Q`?}iG^U\"></block></value></block></next></block></next></block></next></block></next></block></statement></block></xml>","outputs":1,"name":"","x":360,"y":200,"wires":[["ef254a9a.f3e9b8"]]},{"id":"21c7fe09.863b22","type":"inject","z":"14bb364f.9ba5ea","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[0,1,2]","payloadType":"json","x":140,"y":200,"wires":[["546ef6ca.37e8c8"]]},{"id":"ef254a9a.f3e9b8","type":"debug","z":"14bb364f.9ba5ea","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":560,"y":200,"wires":[]}]

So, would your plan be to update the codebase for blockly to reflect the NR >1.0 clone functionality?

bartbutenaers commented 4 years ago

The backend side of this node is a copy of the function node, where I have added some stuff at the time being. Indeed it would be a good idea to sync both nodes again after two years.

However when I sync the code from the function node again, I need to do some extra stuff:

Should put that on my planning somewhere, because it will involve some work. And I'm in the middle of some other Node-RED developments at the moment, which I want to finish first.

I think it is best if I combine this with an upgrade of the Blockly library also, because this node uses a rather old version of Blockly. The Blockly team has been so kind to fix my fullscreen issue issue already months ago, but I should implement that here too...

And then we need our friend @cymplecy to make sure it all fits within the current way of working, because he has a paranormal talent to sense how the brain of our Blockly users works ;-)

But as mentioned before I need to finish some other nodes first, otherwise I have too much context switching and nothing gets finished. To be continued ...

Meanwhile you can use Simon's workaround...

mannharleen commented 4 years ago

I am still evaluating the use of blockly within NR for some of my users, so I can definitely wait for a future better, bolder release (I dont need a workaround, but I understand why, thanks to Simon).

I don't mind testing the new version once it ready. Until then keep up the good work!

bartbutenaers commented 3 years ago

Hi @mannharleen,

It is a long time ago since we had this talk, due to a lack of free time. Not sure if you are still using this node ...

Anyway we are soon publishing a major version of this node, which is currently being developed in the "release-1.1.0" branch. That branch is build on a recent version of the function-node.

The output on the debug window shows "payload = 2" for all 6 messages

When I run your flow with our new version:

image

Then I get this in de debug panel:

image

So I believe your problem is solved with our new version, and therefore I'm going to close this issue. If you have any problems afterwards with this, feel free to reopen it!!