From 22a301b55e78f9eb1d82c32e370e116a4058cde8 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Tue, 29 Sep 2020 16:29:10 +0100 Subject: [PATCH] Add flows:* events and deprecate nodes-* events --- .../@node-red/runtime/lib/events.js | 29 +++++++++++++++++-- .../@node-red/runtime/lib/flows/index.js | 9 ++++++ .../@node-red/runtime/lib/flows/index_spec.js | 15 +++++----- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/packages/node_modules/@node-red/runtime/lib/events.js b/packages/node_modules/@node-red/runtime/lib/events.js index ed280a618..8e6935222 100644 --- a/packages/node_modules/@node-red/runtime/lib/events.js +++ b/packages/node_modules/@node-red/runtime/lib/events.js @@ -14,9 +14,34 @@ * limitations under the License. **/ -var events = require("events"); +const events = new (require("events")).EventEmitter(); -module.exports = new events.EventEmitter(); + +const deprecatedEvents = { + "nodes-stopped": "flows:stopped", + "nodes-started": "flows:started" +} + +function wrapEventFunction(obj,func) { + events["_"+func] = events[func]; + return function(eventName, listener) { + if (deprecatedEvents.hasOwnProperty(eventName)) { + const log = require("@node-red/util").log; + const stack = (new Error().stack).split("\n")[2].split("(")[1].slice(0,-1); + log.warn(`[RED.events] Deprecated use of "${eventName}" event from "${stack}". Use "${deprecatedEvents[eventName]}" instead.`) + } + return events["_"+func].call(events,eventName,listener) + } +} + + +events.on = wrapEventFunction(events,"on"); +events.once = wrapEventFunction(events,"once"); +events.addListener = events.on; + + + +module.exports = events; /** * Runtime events emitter diff --git a/packages/node_modules/@node-red/runtime/lib/flows/index.js b/packages/node_modules/@node-red/runtime/lib/flows/index.js index ec46d8374..3ac4eddd4 100644 --- a/packages/node_modules/@node-red/runtime/lib/flows/index.js +++ b/packages/node_modules/@node-red/runtime/lib/flows/index.js @@ -28,6 +28,7 @@ var flowUtil = require("./util"); var log; var events = require("../events"); var redUtil = require("@node-red/util").util; +const hooks = require("../hooks"); var storage = null; var settings = null; @@ -292,6 +293,8 @@ function start(type,diff,muteLog) { } } + events.emit("flows:starting", {config: activeConfig, type: type, diff: diff}) + var id; if (type === "full") { // A full start means everything should @@ -352,6 +355,8 @@ function start(type,diff,muteLog) { } } } + events.emit("flows:started", {config: activeConfig, type: type, diff: diff}); + // Deprecated event events.emit("nodes-started"); if (credentialsPendingReset === true) { @@ -399,6 +404,8 @@ function stop(type,diff,muteLog) { stopList = diff.changed.concat(diff.removed).concat(diff.linked); } + events.emit("flows:stopping",{config: activeConfig, type: type, diff: diff}) + for (var id in activeFlows) { if (activeFlows.hasOwnProperty(id)) { var flowStateChanged = diff && (diff.added.indexOf(id) !== -1 || diff.removed.indexOf(id) !== -1); @@ -430,6 +437,8 @@ function stop(type,diff,muteLog) { log.info(log._("nodes.flows.stopped-flows")); } } + events.emit("flows:stopped",{config: activeConfig, type: type, diff: diff}); + // Deprecated event events.emit("nodes-stopped"); }); } diff --git a/test/unit/@node-red/runtime/lib/flows/index_spec.js b/test/unit/@node-red/runtime/lib/flows/index_spec.js index b8f3a25aa..df5012844 100644 --- a/test/unit/@node-red/runtime/lib/flows/index_spec.js +++ b/test/unit/@node-red/runtime/lib/flows/index_spec.js @@ -209,8 +209,7 @@ describe('flows/index', function() { storage.getFlows = function() { return when.resolve({flows:originalConfig}); } - - events.once('nodes-started',function() { + events.once('flows:started',function() { flows.setFlows(newConfig,"nodes").then(function() { flows.getFlows().flows.should.eql(newConfig); flowCreate.flows['t1'].update.called.should.be.true(); @@ -239,7 +238,7 @@ describe('flows/index', function() { return when.resolve({flows:originalConfig}); } - events.once('nodes-started',function() { + events.once('flows:started',function() { flows.setFlows(newConfig,"nodes").then(function() { flows.getFlows().flows.should.eql(newConfig); flowCreate.flows['t1'].update.called.should.be.true(); @@ -301,7 +300,7 @@ describe('flows/index', function() { return when.resolve({flows:originalConfig}); } - events.once('nodes-started',function() { + events.once('flows:started',function() { Object.keys(flowCreate.flows).should.eql(['_GLOBAL_','t1']); done(); }); @@ -398,7 +397,7 @@ describe('flows/index', function() { // return when.resolve({flows:originalConfig}); // } // - // events.once('nodes-started',function() { + // events.once('flows:started',function() { // flows.handleError(originalConfig[0],"message",{}); // flowCreate.flows['t1'].handleError.called.should.be.true(); // done(); @@ -423,7 +422,7 @@ describe('flows/index', function() { // return when.resolve({flows:originalConfig}); // } // - // events.once('nodes-started',function() { + // events.once('flows:started',function() { // flows.handleError(originalConfig[0],"message",{}); // try { // flowCreate.flows['t1'].handleError.called.should.be.true(); @@ -451,7 +450,7 @@ describe('flows/index', function() { // return when.resolve({flows:originalConfig}); // } // - // events.once('nodes-started',function() { + // events.once('flows:started',function() { // flows.handleStatus(originalConfig[0],"message"); // flowCreate.flows['t1'].handleStatus.called.should.be.true(); // done(); @@ -477,7 +476,7 @@ describe('flows/index', function() { // return when.resolve({flows:originalConfig}); // } // - // events.once('nodes-started',function() { + // events.once('flows:started',function() { // flows.handleStatus(originalConfig[0],"message"); // try { // flowCreate.flows['t1'].handleStatus.called.should.be.true();