Closed mikeS7 closed 5 years ago
Nice job Mike!
I will merge and publish the changes sometime next week.
I've had an idea to make a new project: node-red-contrib-async-function
node.
Instead of doing:
var functionText = "module.exports = function(util, RED, __node__, context, flow, global, setTimeout, clearTimeout, setInterval, clearInterval) { " +
" return function(msg) { " +
" var __msgid__ = msg._msgid;" +
" var node = {" +
" id:__node__.id," +
" name:__node__.name," +
" log:__node__.log," +
" error:__node__.error," +
" warn:__node__.warn," +
" debug:__node__.debug," +
" trace:__node__.trace," +
" on:__node__.on," +
" status:__node__.status," +
" send:function(msgs) { __node__.send(__msgid__,msgs);}" +
" };\n" +
this.func + "\n" +
" };" +
"};";
Do something like:
var functionText = "module.exports = function(util, RED, __node__, context, flow, global, setTimeout, clearTimeout, setInterval, clearInterval) { " +
" return function(msg) { " +
" var __msgid__ = msg._msgid;" +
" var node = {" +
" id:__node__.id," +
" name:__node__.name," +
" log:__node__.log," +
" error:__node__.error," +
" warn:__node__.warn," +
" debug:__node__.debug," +
" trace:__node__.trace," +
" on:__node__.on," +
" status:__node__.status," +
" send:function(msgs) { __node__.send(__msgid__,msgs);}" +
" };\n" +
" (async () => {\n" +
this.func + "\n" +
" })().then(msgs => __node__.send(__msgid__, msgs)).catch(err => __node__.error(err, msg));\n"
" };" +
"};";
This way the editor's code could contain "await" calls. Unfortunately last time I checked the the ACE editor used by node-red showed lint-warnings on "async" and "await" keywords, but I haven't updated node-red for a really long time.
Another great feature could be to wrap everything with a configurable p-limit, so you could, for example, configure the limit to 1 and make sure the node does not handle two msgs at the same time (and maintains FIFO order).
What do you think? Want to help?
Oren, I just performed copy&paste job. To be honest I'm using only "unsafe" part and not working with async. I think splitting into two nodes is a very good idea. Unfortunately I don't have enough experience in programming.
Hi Oren, could you please merge and publish new version?
Sorry for the delay, published version 0.6.0: https://www.npmjs.com/package/node-red-contrib-unsafe-function . Hopefully there are no bugs because I didn't really test the changes.
Sync with 0.19 release, added env support from upcoming 0.20 node-red/node-red@0c5a76b (commented for now)