1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add flows:* events and deprecate nodes-* events

This commit is contained in:
Nick O'Leary 2020-09-29 16:29:10 +01:00
parent 605177dcf0
commit 22a301b55e
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
3 changed files with 43 additions and 10 deletions

View File

@ -14,9 +14,34 @@
* limitations under the License. * 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 * Runtime events emitter

View File

@ -28,6 +28,7 @@ var flowUtil = require("./util");
var log; var log;
var events = require("../events"); var events = require("../events");
var redUtil = require("@node-red/util").util; var redUtil = require("@node-red/util").util;
const hooks = require("../hooks");
var storage = null; var storage = null;
var settings = 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; var id;
if (type === "full") { if (type === "full") {
// A full start means everything should // 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"); events.emit("nodes-started");
if (credentialsPendingReset === true) { if (credentialsPendingReset === true) {
@ -399,6 +404,8 @@ function stop(type,diff,muteLog) {
stopList = diff.changed.concat(diff.removed).concat(diff.linked); stopList = diff.changed.concat(diff.removed).concat(diff.linked);
} }
events.emit("flows:stopping",{config: activeConfig, type: type, diff: diff})
for (var id in activeFlows) { for (var id in activeFlows) {
if (activeFlows.hasOwnProperty(id)) { if (activeFlows.hasOwnProperty(id)) {
var flowStateChanged = diff && (diff.added.indexOf(id) !== -1 || diff.removed.indexOf(id) !== -1); 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")); log.info(log._("nodes.flows.stopped-flows"));
} }
} }
events.emit("flows:stopped",{config: activeConfig, type: type, diff: diff});
// Deprecated event
events.emit("nodes-stopped"); events.emit("nodes-stopped");
}); });
} }

View File

@ -209,8 +209,7 @@ describe('flows/index', function() {
storage.getFlows = function() { storage.getFlows = function() {
return when.resolve({flows:originalConfig}); return when.resolve({flows:originalConfig});
} }
events.once('flows:started',function() {
events.once('nodes-started',function() {
flows.setFlows(newConfig,"nodes").then(function() { flows.setFlows(newConfig,"nodes").then(function() {
flows.getFlows().flows.should.eql(newConfig); flows.getFlows().flows.should.eql(newConfig);
flowCreate.flows['t1'].update.called.should.be.true(); flowCreate.flows['t1'].update.called.should.be.true();
@ -239,7 +238,7 @@ describe('flows/index', function() {
return when.resolve({flows:originalConfig}); return when.resolve({flows:originalConfig});
} }
events.once('nodes-started',function() { events.once('flows:started',function() {
flows.setFlows(newConfig,"nodes").then(function() { flows.setFlows(newConfig,"nodes").then(function() {
flows.getFlows().flows.should.eql(newConfig); flows.getFlows().flows.should.eql(newConfig);
flowCreate.flows['t1'].update.called.should.be.true(); flowCreate.flows['t1'].update.called.should.be.true();
@ -301,7 +300,7 @@ describe('flows/index', function() {
return when.resolve({flows:originalConfig}); return when.resolve({flows:originalConfig});
} }
events.once('nodes-started',function() { events.once('flows:started',function() {
Object.keys(flowCreate.flows).should.eql(['_GLOBAL_','t1']); Object.keys(flowCreate.flows).should.eql(['_GLOBAL_','t1']);
done(); done();
}); });
@ -398,7 +397,7 @@ describe('flows/index', function() {
// return when.resolve({flows:originalConfig}); // return when.resolve({flows:originalConfig});
// } // }
// //
// events.once('nodes-started',function() { // events.once('flows:started',function() {
// flows.handleError(originalConfig[0],"message",{}); // flows.handleError(originalConfig[0],"message",{});
// flowCreate.flows['t1'].handleError.called.should.be.true(); // flowCreate.flows['t1'].handleError.called.should.be.true();
// done(); // done();
@ -423,7 +422,7 @@ describe('flows/index', function() {
// return when.resolve({flows:originalConfig}); // return when.resolve({flows:originalConfig});
// } // }
// //
// events.once('nodes-started',function() { // events.once('flows:started',function() {
// flows.handleError(originalConfig[0],"message",{}); // flows.handleError(originalConfig[0],"message",{});
// try { // try {
// flowCreate.flows['t1'].handleError.called.should.be.true(); // flowCreate.flows['t1'].handleError.called.should.be.true();
@ -451,7 +450,7 @@ describe('flows/index', function() {
// return when.resolve({flows:originalConfig}); // return when.resolve({flows:originalConfig});
// } // }
// //
// events.once('nodes-started',function() { // events.once('flows:started',function() {
// flows.handleStatus(originalConfig[0],"message"); // flows.handleStatus(originalConfig[0],"message");
// flowCreate.flows['t1'].handleStatus.called.should.be.true(); // flowCreate.flows['t1'].handleStatus.called.should.be.true();
// done(); // done();
@ -477,7 +476,7 @@ describe('flows/index', function() {
// return when.resolve({flows:originalConfig}); // return when.resolve({flows:originalConfig});
// } // }
// //
// events.once('nodes-started',function() { // events.once('flows:started',function() {
// flows.handleStatus(originalConfig[0],"message"); // flows.handleStatus(originalConfig[0],"message");
// try { // try {
// flowCreate.flows['t1'].handleStatus.called.should.be.true(); // flowCreate.flows['t1'].handleStatus.called.should.be.true();