From 84d2b8ad6db06c34b678c65b12b856f10548c02f Mon Sep 17 00:00:00 2001 From: Hiroyasu Nishiyama Date: Sat, 7 Mar 2020 01:55:45 +0900 Subject: [PATCH] add support of initialization & finalization to function node --- .../editor-client/src/js/ui/library.js | 28 ++- .../nodes/core/function/10-function.html | 224 ++++++++++++++++-- .../nodes/core/function/10-function.js | 6 + .../locales/en-US/function/10-function.html | 3 +- .../nodes/locales/en-US/messages.json | 2 + .../locales/ja/function/10-function.html | 3 +- .../@node-red/nodes/locales/ja/messages.json | 2 + test/nodes/core/function/10-function_spec.js | 27 ++- 8 files changed, 269 insertions(+), 26 deletions(-) diff --git a/packages/node_modules/@node-red/editor-client/src/js/ui/library.js b/packages/node_modules/@node-red/editor-client/src/js/ui/library.js index e25223f94..2520c16a9 100755 --- a/packages/node_modules/@node-red/editor-client/src/js/ui/library.js +++ b/packages/node_modules/@node-red/editor-client/src/js/ui/library.js @@ -50,6 +50,18 @@ RED.library = (function() { ''+ '' + function toSingleLine(text) { + var result = text.replace(/\\/g, "\\\\").replace(/\n/g, "\\n"); + return result; + } + + function fromSingleLine(text) { + var result = text.replace(/\\[\\n]/g, function(s) { + return ((s === "\\\\") ? "\\" : "\n"); + }); + return result; + } + function saveToLibrary() { var elementPrefix = activeLibrary.elementPrefix || "node-input-"; var name = $("#"+elementPrefix+"name").val().trim(); @@ -68,6 +80,10 @@ RED.library = (function() { var field = activeLibrary.fields[i]; if (field == "name") { data.name = name; + } else if(field == "initialize") { + data.initialize = toSingleLine(activeLibrary.initEditor.getValue()); + } else if(field == "finalize") { + data.finalize = toSingleLine(activeLibrary.finalizeEditor.getValue()); } else { data[field] = $("#"+elementPrefix+field).val(); } @@ -523,7 +539,17 @@ RED.library = (function() { var elementPrefix = activeLibrary.elementPrefix || "node-input-"; for (var i=0; i
-
- - - + +
+
    -
    -
    -
    -
    -
    - - + +
    + + + + + + +
    + diff --git a/packages/node_modules/@node-red/nodes/core/function/10-function.js b/packages/node_modules/@node-red/nodes/core/function/10-function.js index 65a1b4a61..7bdeada65 100644 --- a/packages/node_modules/@node-red/nodes/core/function/10-function.js +++ b/packages/node_modules/@node-red/nodes/core/function/10-function.js @@ -62,6 +62,8 @@ module.exports = function(RED) { var node = this; this.name = n.name; this.func = n.func; + this.ini = n.initialize; + this.fin = n.finalize; var handleNodeDoneCall = true; // Check to see if the Function appears to call `node.done()`. If so, @@ -89,6 +91,8 @@ module.exports = function(RED) { "};\n"+ this.func+"\n"+ "})(msg,send,done);"; + var iniText = "(function () {\n"+this.ini +"\n})();"; + var finText = "(function () {\n"+this.fin +"\n})();"; this.topic = n.topic; this.outstandingTimers = []; this.outstandingIntervals = []; @@ -229,6 +233,7 @@ module.exports = function(RED) { } var context = vm.createContext(sandbox); try { + vm.runInContext(iniText, context); this.script = vm.createScript(functionText, { filename: 'Function node:'+this.id+(this.name?' ['+this.name+']':''), // filename for stack traces displayErrors: true @@ -297,6 +302,7 @@ module.exports = function(RED) { } }); this.on("close", function() { + vm.runInContext(finText, context); while (node.outstandingTimers.length > 0) { clearTimeout(node.outstandingTimers.pop()); } diff --git a/packages/node_modules/@node-red/nodes/locales/en-US/function/10-function.html b/packages/node_modules/@node-red/nodes/locales/en-US/function/10-function.html index 28bc76f3a..c1c768f84 100644 --- a/packages/node_modules/@node-red/nodes/locales/en-US/function/10-function.html +++ b/packages/node_modules/@node-red/nodes/locales/en-US/function/10-function.html @@ -15,12 +15,13 @@ -->