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

Pass a 'removed' parameter to node close handler

This commit is contained in:
Nick O'Leary 2017-04-21 23:36:21 +01:00
parent e13d410b4a
commit 4ea33ea482
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
5 changed files with 60 additions and 21 deletions

View File

@ -81,17 +81,21 @@ Node.prototype.on = function(event, callback) {
} }
}; };
Node.prototype.close = function() { Node.prototype.close = function(removed) {
//console.log(this.type,this.id,removed);
var promises = []; var promises = [];
var node = this; var node = this;
for (var i=0;i<this._closeCallbacks.length;i++) { for (var i=0;i<this._closeCallbacks.length;i++) {
var callback = this._closeCallbacks[i]; var callback = this._closeCallbacks[i];
if (callback.length == 1) { if (callback.length > 0) {
promises.push( promises.push(
when.promise(function(resolve) { when.promise(function(resolve) {
callback.call(node, function() { var args = [];
resolve(); if (callback.length === 2) {
}); args.push(!!removed);
}
args.push(resolve);
callback.apply(node, args);
}) })
); );
} else { } else {

View File

@ -121,7 +121,7 @@ function Flow(global,flow) {
} }
} }
this.stop = function(stopList) { this.stop = function(stopList, removedList) {
return when.promise(function(resolve) { return when.promise(function(resolve) {
var i; var i;
if (stopList) { if (stopList) {
@ -135,6 +135,13 @@ function Flow(global,flow) {
} else { } else {
stopList = Object.keys(activeNodes); stopList = Object.keys(activeNodes);
} }
// Convert the list to a map to avoid multiple scans of the list
var removedMap = {};
removedList = removedList || [];
removedList.forEach(function(id) {
removedMap[id] = true;
});
var promises = []; var promises = [];
for (i=0;i<stopList.length;i++) { for (i=0;i<stopList.length;i++) {
var node = activeNodes[stopList[i]]; var node = activeNodes[stopList[i]];
@ -144,7 +151,7 @@ function Flow(global,flow) {
delete subflowInstanceNodes[stopList[i]]; delete subflowInstanceNodes[stopList[i]];
} }
try { try {
var p = node.close(); var p = node.close(removedMap[stopList[i]]);
if (p) { if (p) {
promises.push(p); promises.push(p);
} }

View File

@ -107,9 +107,7 @@ function setFlows(_config,type,muteLog) {
} else { } else {
config = clone(_config); config = clone(_config);
newFlowConfig = flowUtil.parseConfig(clone(config)); newFlowConfig = flowUtil.parseConfig(clone(config));
if (type !== 'full') { diff = flowUtil.diffConfigs(activeFlowConfig,newFlowConfig);
diff = flowUtil.diffConfigs(activeFlowConfig,newFlowConfig);
}
credentials.clean(config); credentials.clean(config);
var credsDirty = credentials.dirty(); var credsDirty = credentials.dirty();
configSavePromise = credentials.export().then(function(creds) { configSavePromise = credentials.export().then(function(creds) {
@ -252,14 +250,14 @@ function start(type,diff,muteLog) {
return when.resolve(); return when.resolve();
} }
if (!muteLog) { if (!muteLog) {
if (diff) { if (type !== "full") {
log.info(log._("nodes.flows.starting-modified-"+type)); log.info(log._("nodes.flows.starting-modified-"+type));
} else { } else {
log.info(log._("nodes.flows.starting-flows")); log.info(log._("nodes.flows.starting-flows"));
} }
} }
var id; var id;
if (!diff) { if (type === "full") {
if (!activeFlows['global']) { if (!activeFlows['global']) {
log.debug("red/nodes/flows.start : starting flow : global"); log.debug("red/nodes/flows.start : starting flow : global");
activeFlows['global'] = Flow.create(activeFlowConfig); activeFlows['global'] = Flow.create(activeFlowConfig);
@ -310,7 +308,7 @@ function start(type,diff,muteLog) {
events.emit("runtime-event",{id:"runtime-state"}); events.emit("runtime-event",{id:"runtime-state"});
if (!muteLog) { if (!muteLog) {
if (diff) { if (type !== "full") {
log.info(log._("nodes.flows.started-modified-"+type)); log.info(log._("nodes.flows.started-modified-"+type));
} else { } else {
log.info(log._("nodes.flows.started-flows")); log.info(log._("nodes.flows.started-flows"));
@ -321,8 +319,15 @@ function start(type,diff,muteLog) {
function stop(type,diff,muteLog) { function stop(type,diff,muteLog) {
type = type||"full"; type = type||"full";
diff = diff||{
added:[],
changed:[],
removed:[],
rewired:[],
linked:[]
};
if (!muteLog) { if (!muteLog) {
if (diff) { if (type !== "full") {
log.info(log._("nodes.flows.stopping-modified-"+type)); log.info(log._("nodes.flows.stopping-modified-"+type));
} else { } else {
log.info(log._("nodes.flows.stopping-flows")); log.info(log._("nodes.flows.stopping-flows"));
@ -331,16 +336,18 @@ function stop(type,diff,muteLog) {
started = false; started = false;
var promises = []; var promises = [];
var stopList; var stopList;
var removedList = diff.removed;
if (type === 'nodes') { if (type === 'nodes') {
stopList = diff.changed.concat(diff.removed); stopList = diff.changed.concat(diff.removed);
} else if (type === 'flows') { } else if (type === 'flows') {
stopList = diff.changed.concat(diff.removed).concat(diff.linked); stopList = diff.changed.concat(diff.removed).concat(diff.linked);
} }
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);
promises = promises.concat(activeFlows[id].stop(flowStateChanged?null:stopList)); promises = promises.concat(activeFlows[id].stop(flowStateChanged?null:stopList,removedList));
if (!diff || flowStateChanged || diff.removed.indexOf(id)!==-1) { if (type === "full" || flowStateChanged || diff.removed.indexOf(id)!==-1) {
delete activeFlows[id]; delete activeFlows[id];
} }
} }
@ -366,7 +373,7 @@ function stop(type,diff,muteLog) {
// in start() // in start()
subflowInstanceNodeMap = {}; subflowInstanceNodeMap = {};
if (!muteLog) { if (!muteLog) {
if (diff) { if (type !== "full") {
log.info(log._("nodes.flows.stopped-modified-"+type)); log.info(log._("nodes.flows.stopped-modified-"+type));
} else { } else {
log.info(log._("nodes.flows.stopped-flows")); log.info(log._("nodes.flows.stopped-flows"));

View File

@ -187,6 +187,12 @@ module.exports = {
var wires; var wires;
var j,k; var j,k;
if (!oldConfig) {
oldConfig = {
flows:{},
allNodes:{}
}
}
var changedSubflows = {}; var changedSubflows = {};
var added = {}; var added = {};

View File

@ -63,7 +63,7 @@ describe('Node', function() {
n.on('close',function(done) { n.on('close',function(done) {
setTimeout(function() { setTimeout(function() {
done(); done();
},200); },50);
}); });
var p = n.close(); var p = n.close();
should.exist(p); should.exist(p);
@ -71,7 +71,22 @@ describe('Node', function() {
testdone(); testdone();
}); });
}); });
it('accepts a callback with "removed" and "done" parameters', function(testdone) {
var n = new RedNode({id:'123',type:'abc'});
var receivedRemoved;
n.on('close',function(removed,done) {
receivedRemoved = removed;
setTimeout(function() {
done();
},50);
});
var p = n.close(true);
should.exist(p);
(receivedRemoved).should.be.true();
p.then(function() {
testdone();
});
})
it('allows multiple close handlers to be registered',function(testdone) { it('allows multiple close handlers to be registered',function(testdone) {
var n = new RedNode({id:'123',type:'abc'}); var n = new RedNode({id:'123',type:'abc'});
var callbacksClosed = 0; var callbacksClosed = 0;
@ -79,13 +94,13 @@ describe('Node', function() {
setTimeout(function() { setTimeout(function() {
callbacksClosed++; callbacksClosed++;
done(); done();
},200); },50);
}); });
n.on('close',function(done) { n.on('close',function(done) {
setTimeout(function() { setTimeout(function() {
callbacksClosed++; callbacksClosed++;
done(); done();
},200); },75);
}); });
n.on('close',function() { n.on('close',function() {
callbacksClosed++; callbacksClosed++;