From ce1e1139a59c95be3918f03a1a28cdca7185c44e Mon Sep 17 00:00:00 2001 From: mschlgl Date: Tue, 10 Dec 2024 11:03:37 +0100 Subject: [PATCH 1/7] configurable watch node --- .../nodes/core/storage/23-watch.html | 28 +++++++-- .../@node-red/nodes/core/storage/23-watch.js | 58 ++++++++++++++----- 2 files changed, 67 insertions(+), 19 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/storage/23-watch.html b/packages/node_modules/@node-red/nodes/core/storage/23-watch.html index b2bd12f80..69d3a14d9 100644 --- a/packages/node_modules/@node-red/nodes/core/storage/23-watch.html +++ b/packages/node_modules/@node-red/nodes/core/storage/23-watch.html @@ -18,6 +18,8 @@
+ +
@@ -37,11 +39,15 @@ defaults: { name: {value:""}, files: {value:"",required:true, - label:RED._("node-red:watch.label.files")}, - recursive: {value:""} + label:RED._("node-red:watch.label.files")}, + // property added to enable context type setup + filesType: {value: "str"}, + recursive: {value:""}, + // property added to enable number of inputs change + inputs: {value:0} }, color:"BurlyWood", - inputs:0, + inputs: 0, outputs:1, icon: "watch.svg", label: function() { @@ -49,6 +55,20 @@ }, labelStyle: function() { return this.name?"node_label_italic":""; + }, + // function added to enable context type setup + oneditprepare: function() { + $("#node-input-files").typedInput({ + default: 'str', + types: ['str','flow','global','env','msg'], + typeField: $("#node-input-filesType") + }); + }, + // function added to set node input number + oneditsave: function() { + // if controlled by msg there has to be a input + this.inputs = $("#node-input-filesType")[0].attributes.value.value == "msg"?1:0; + return; } }); - + \ No newline at end of file diff --git a/packages/node_modules/@node-red/nodes/core/storage/23-watch.js b/packages/node_modules/@node-red/nodes/core/storage/23-watch.js index ccbdeba9f..2ee5770aa 100644 --- a/packages/node_modules/@node-red/nodes/core/storage/23-watch.js +++ b/packages/node_modules/@node-red/nodes/core/storage/23-watch.js @@ -20,20 +20,17 @@ module.exports = function(RED) { const fs = require("fs") const path = require("path") - function WatchNode(n) { - RED.nodes.createNode(this,n); - - this.recursive = n.recursive || false; - this.files = (n.files || "").split(","); - for (var f=0; f < this.files.length; f++) { - this.files[f] = this.files[f].trim(); + function configureWatcher(node, n) { + if (node.filesType != 'msg') node.files = (n.files || ""); + node.files = node.files.split(","); + for (var f=0; f < node.files.length; f++) { + node.files[f] = node.files[f].trim(); } - this.p = (this.files.length === 1) ? this.files[0] : JSON.stringify(this.files); - const node = this; + node.p = (node.files.length === 1) ? node.files[0] : JSON.stringify(node.files); - const watcher = watch(this.files, { recursive: this.recursive }); + node.watcher = watch(node.files, { recursive: node.recursive }); - watcher.on('change', function (event, fpath) { + node.watcher.on('change', function (event, fpath) { const file = path.basename(fpath) let stat; try { @@ -60,14 +57,45 @@ module.exports = function(RED) { node.send(msg); }); - watcher.on('error', function (error) { + node.watcher.on('error', function (error) { const msg = { payload: "" }; node.error(error,msg); }); + }; + + function WatchNode(n) { + RED.nodes.createNode(this,n); + var node = this; + node.recursive = n.recursive || false; + node.files = n.files || ""; + node.filesType = n.filesType || "str"; + node.inputs = n.inputs || 0; + // added to enable context setup + switch (n.filesType) { + case "global": + n.files = node.context().global.get(`${n.files}`); + break; + case "flow": + n.files = node.context().flow.get(`${n.files}`); + break; + case "env": + n.files = node.context().env.get(`${n.files}`); + case "msg": + node.on('input', function(msg) { + if (msg[n.files] != node.files) { // new path/files list received + if (node.watcher) node.watcher.close(); // if watcher active close it + node.files = msg[n.files]; // save new path/files list + configureWatcher(node, n); // reconfigure watcher + }; + }); + break; + }; - this.close = function() { - watcher.close(); + if (n.filesType != 'msg') configureWatcher(node, n); + + node.close = function() { + node.watcher.close(); } } RED.nodes.registerType("watch", WatchNode); -} +} \ No newline at end of file From 9bd8ec9f62341a70a1fd5b9d00c5cf2f67e00227 Mon Sep 17 00:00:00 2001 From: mschlgl Date: Tue, 10 Dec 2024 13:23:39 +0100 Subject: [PATCH 2/7] configurable watch node --- packages/node_modules/@node-red/nodes/core/storage/23-watch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/node_modules/@node-red/nodes/core/storage/23-watch.js b/packages/node_modules/@node-red/nodes/core/storage/23-watch.js index 2ee5770aa..69fa77e87 100644 --- a/packages/node_modules/@node-red/nodes/core/storage/23-watch.js +++ b/packages/node_modules/@node-red/nodes/core/storage/23-watch.js @@ -94,7 +94,7 @@ module.exports = function(RED) { if (n.filesType != 'msg') configureWatcher(node, n); node.close = function() { - node.watcher.close(); + if (node.watcher) node.watcher.close(); } } RED.nodes.registerType("watch", WatchNode); From bc0268481b4e29feb12829e6cd6759b3ef1f72bf Mon Sep 17 00:00:00 2001 From: mschlgl Date: Thu, 12 Dec 2024 15:41:50 +0100 Subject: [PATCH 3/7] configurable watch node --- .../node_modules/@node-red/nodes/core/storage/23-watch.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/node_modules/@node-red/nodes/core/storage/23-watch.html b/packages/node_modules/@node-red/nodes/core/storage/23-watch.html index 69d3a14d9..946398ad5 100644 --- a/packages/node_modules/@node-red/nodes/core/storage/23-watch.html +++ b/packages/node_modules/@node-red/nodes/core/storage/23-watch.html @@ -67,7 +67,7 @@ // function added to set node input number oneditsave: function() { // if controlled by msg there has to be a input - this.inputs = $("#node-input-filesType")[0].attributes.value.value == "msg"?1:0; + this.inputs = $("#node-input-filesType").typedInput("type") === "msg" ? 1 : 0; return; } }); From d479b6c359602e93c3e068b2b78398c10df71c3e Mon Sep 17 00:00:00 2001 From: mschlgl Date: Thu, 21 Aug 2025 11:32:52 +0200 Subject: [PATCH 4/7] changed according comment Jlian-Sz --- .../node_modules/@node-red/nodes/core/storage/23-watch.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/node_modules/@node-red/nodes/core/storage/23-watch.html b/packages/node_modules/@node-red/nodes/core/storage/23-watch.html index 946398ad5..efc6a98ed 100644 --- a/packages/node_modules/@node-red/nodes/core/storage/23-watch.html +++ b/packages/node_modules/@node-red/nodes/core/storage/23-watch.html @@ -67,7 +67,7 @@ // function added to set node input number oneditsave: function() { // if controlled by msg there has to be a input - this.inputs = $("#node-input-filesType").typedInput("type") === "msg" ? 1 : 0; + this.inputs = $("#node-input-files").typedInput("type") === "msg" ? 1 : 0; return; } }); From 051e61b04553f978d2368e66b0771ebc1dcad5d3 Mon Sep 17 00:00:00 2001 From: mschlgl Date: Mon, 8 Sep 2025 09:54:18 +0200 Subject: [PATCH 5/7] reduced options to str, env, msg --- .../nodes/core/storage/23-watch.html | 4 +- .../@node-red/nodes/core/storage/23-watch.js | 38 +++++++++---------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/storage/23-watch.html b/packages/node_modules/@node-red/nodes/core/storage/23-watch.html index efc6a98ed..9dc945c30 100644 --- a/packages/node_modules/@node-red/nodes/core/storage/23-watch.html +++ b/packages/node_modules/@node-red/nodes/core/storage/23-watch.html @@ -42,7 +42,7 @@ label:RED._("node-red:watch.label.files")}, // property added to enable context type setup filesType: {value: "str"}, - recursive: {value:""}, + recursive: {value: ""}, // property added to enable number of inputs change inputs: {value:0} }, @@ -60,7 +60,7 @@ oneditprepare: function() { $("#node-input-files").typedInput({ default: 'str', - types: ['str','flow','global','env','msg'], + types: ['str','env','msg'], typeField: $("#node-input-filesType") }); }, diff --git a/packages/node_modules/@node-red/nodes/core/storage/23-watch.js b/packages/node_modules/@node-red/nodes/core/storage/23-watch.js index 69fa77e87..ad0405562 100644 --- a/packages/node_modules/@node-red/nodes/core/storage/23-watch.js +++ b/packages/node_modules/@node-red/nodes/core/storage/23-watch.js @@ -71,27 +71,25 @@ module.exports = function(RED) { node.filesType = n.filesType || "str"; node.inputs = n.inputs || 0; // added to enable context setup - switch (n.filesType) { - case "global": - n.files = node.context().global.get(`${n.files}`); + // if n.filesType not str reconfigure watcher on receiving a message + node.on('input', function(msg) { + switch (n.filesType) { + case "msg": + n.files = msg[n.files]; break; - case "flow": - n.files = node.context().flow.get(`${n.files}`); - break; - case "env": - n.files = node.context().env.get(`${n.files}`); - case "msg": - node.on('input', function(msg) { - if (msg[n.files] != node.files) { // new path/files list received - if (node.watcher) node.watcher.close(); // if watcher active close it - node.files = msg[n.files]; // save new path/files list - configureWatcher(node, n); // reconfigure watcher - }; - }); - break; - }; - - if (n.filesType != 'msg') configureWatcher(node, n); + default: + n.files = node.files; + } + if (n.files != node.files) { // new path/files list received or variable changed + if (node.watcher) node.watcher.close(); // if watcher active close it + node.files = n.files; // save new path/files list + configureWatcher(node, n); // reconfigure watcher + }; + }); + // n.filesType not msg configure watcher immediately + // n.filesType env, get env variable value + if (n.filesTyoe == 'env') n.files = node.context().env.get(`${n.files}`); + if (n.filesType == 'str') configureWatcher(node, n); node.close = function() { if (node.watcher) node.watcher.close(); From 57c4a487ebb7f54d66ed6b0a4a40096fbbe85b0d Mon Sep 17 00:00:00 2001 From: mschlgl Date: Wed, 10 Sep 2025 10:40:35 +0200 Subject: [PATCH 6/7] reduced config options to str,.env,msg --- .../@node-red/nodes/core/storage/23-watch.js | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/storage/23-watch.js b/packages/node_modules/@node-red/nodes/core/storage/23-watch.js index ad0405562..bb06dcb8b 100644 --- a/packages/node_modules/@node-red/nodes/core/storage/23-watch.js +++ b/packages/node_modules/@node-red/nodes/core/storage/23-watch.js @@ -71,26 +71,28 @@ module.exports = function(RED) { node.filesType = n.filesType || "str"; node.inputs = n.inputs || 0; // added to enable context setup - // if n.filesType not str reconfigure watcher on receiving a message - node.on('input', function(msg) { - switch (n.filesType) { - case "msg": - n.files = msg[n.files]; - break; - default: - n.files = node.files; - } - if (n.files != node.files) { // new path/files list received or variable changed - if (node.watcher) node.watcher.close(); // if watcher active close it - node.files = n.files; // save new path/files list - configureWatcher(node, n); // reconfigure watcher - }; - }); + // n.filesType env, get env variable value, use as path/files list + if (n.filesType == 'env') n.files = RED.util.evaluateNodeProperty(n.files, 'env', node); // n.filesType not msg configure watcher immediately - // n.filesType env, get env variable value - if (n.filesTyoe == 'env') n.files = node.context().env.get(`${n.files}`); - if (n.filesType == 'str') configureWatcher(node, n); - + if (n.filesType != 'msg') configureWatcher(node, n) + // if n.filesType msg reconfigure watcher on receiving a message + else { + node.on('input', function(msg) { + switch (n.filesType) { + case "msg": + n.files = msg[n.files]; + break; + default: + n.files = node.files; + } + if (n.files != node.files) { // new path/files list received or variable changed + if (node.watcher) node.watcher.close(); // if watcher active close it + node.files = n.files; // save new path/files list + configureWatcher(node, n); // reconfigure watcher + }; + }); + } + node.close = function() { if (node.watcher) node.watcher.close(); } From de0a1a3802e558832f11cc1138a5cb48db0a499d Mon Sep 17 00:00:00 2001 From: mschlgl Date: Wed, 10 Sep 2025 13:13:55 +0200 Subject: [PATCH 7/7] added latest comments --- .../node_modules/@node-red/nodes/core/storage/23-watch.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/storage/23-watch.js b/packages/node_modules/@node-red/nodes/core/storage/23-watch.js index bb06dcb8b..2927bc12d 100644 --- a/packages/node_modules/@node-red/nodes/core/storage/23-watch.js +++ b/packages/node_modules/@node-red/nodes/core/storage/23-watch.js @@ -80,14 +80,13 @@ module.exports = function(RED) { node.on('input', function(msg) { switch (n.filesType) { case "msg": - n.files = msg[n.files]; + node.files = msg[n.files]; break; default: - n.files = node.files; + node.files = n.files; } if (n.files != node.files) { // new path/files list received or variable changed if (node.watcher) node.watcher.close(); // if watcher active close it - node.files = n.files; // save new path/files list configureWatcher(node, n); // reconfigure watcher }; });