This is an elegant way of adding global functions on startup, reducing function node code bloat and increasing code reuse.
It works. But is it safe? Nick?
--
http://nodered.org
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send an email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/86da8fd7-a8dd-47da-93ea-a5cf705a8125%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Understood… but if the non-serialisable stuff is just stripped out, and can be added back on reload, is it perfectly safe?
My persistence and liking for this technique is because it’s a very different use-case to using functionGlobalContext in settings.js. Where functions relate to globally held data, they can join that data in very intimate OO way, and live and be maintained within flows.json rather than in some external library.
Jon
From: node...@googlegroups.com [mailto:node...@googlegroups.com] On Behalf Of Nick O'Leary
Sent: 22 April 2017 08:58
To: Node-RED Mailing List <node...@googlegroups.com>
Subject: Re: [node-red] Functions in global.set objects - safe?
The main caveat with that technique is that in a future where we support persisting the contents of context, only JSON serialisable data will be saved with the .get/.set functions. Non-serialisable data will still get stored in memory, but won't be restored across restarts.
The preferred way of doing that is via the functionGlobalSettings option in your settings file, rather than trying to do it dynamically with global.set()
Nick
On 22 April 2017 at 08:48, Jon Silver <jonsilve...@gmail.com> wrote:
1. In a function node, declare an object with a function member
2. Store the object using global.set
3. Somewhere else, retrieve the object with global.get
4. Execute the function
This is an elegant way of adding global functions on startup, reducing function node code bloat and increasing code reuse.
It works. But is it safe? Nick?
--
http://nodered.org
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send an email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/86da8fd7-a8dd-47da-93ea-a5cf705a8125%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
http://nodered.org
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/DFDDveKNhzs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/CAF%3DvhqdT3Awi17ThwSoD8ujd_vUq3-Tuo7b8P9K9a5UQ16Z0xA%40mail.gmail.com.
[
{
"id": "c9398a88.07d288",
"type": "inject",
"z": "841e269b.77d478",
"name": "store",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": true,
"x": 150,
"y": 3540,
"wires": [
[
"c3098054.262d4"
]
]
},
{
"id": "c3098054.262d4",
"type": "function",
"z": "841e269b.77d478",
"name": "",
"func": "var fns = {};\n\nfns.something = something;\nfns.somethingelse = somethingelse;\n\nglobal.set(\"fns\", fns);\n\nreturn msg;\n\n\nfunction something(thing) {\n return \"one: \" + thing;\n}\n\nfunction somethingelse(thing) {\n return \"two: \" + thing;\n}",
"outputs": 1,
"noerr": 0,
"x": 310,
"y": 3540,
"wires": [
[]
]
},
{
"id": "31f6f9e9.57f1f6",
"type": "inject",
"z": "841e269b.77d478",
"name": "test",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": false,
"x": 190,
"y": 3600,
"wires": [
[
"a6a9e393.656e4"
]
]
},
{
"id": "a6a9e393.656e4",
"type": "function",
"z": "841e269b.77d478",
"name": "",
"func": "var fns = global.get(\"fns\");\n\nmsg.payload = {something: fns.something(msg.payload), somethingelse: fns.somethingelse(msg.payload)};\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 340,
"y": 3600,
"wires": [
[
"685feaeb.e5deb4"
]
]
},
{
"id": "685feaeb.e5deb4",
"type": "debug",
"z": "841e269b.77d478",
"name": "",
"active": true,
"console": "false",
"complete": "false",
"x": 520,
"y": 3600,
"wires": []
}
]