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 node = this;
for (var i=0;i<this._closeCallbacks.length;i++) {
var callback = this._closeCallbacks[i];
if (callback.length == 1) {
if (callback.length > 0) {
promises.push(
when.promise(function(resolve) {
callback.call(node, function() {
resolve();
});
var args = [];
if (callback.length === 2) {
args.push(!!removed);
}
args.push(resolve);
callback.apply(node, args);
})
);
} 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) {
var i;
if (stopList) {
@ -135,6 +135,13 @@ function Flow(global,flow) {
} else {
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 = [];
for (i=0;i<stopList.length;i++) {
var node = activeNodes[stopList[i]];
@ -144,7 +151,7 @@ function Flow(global,flow) {
delete subflowInstanceNodes[stopList[i]];
}
try {
var p = node.close();
var p = node.close(removedMap[stopList[i]]);
if (p) {
promises.push(p);
}

View File

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

View File

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

View File

@ -63,7 +63,7 @@ describe('Node', function() {
n.on('close',function(done) {
setTimeout(function() {
done();
},200);
},50);
});
var p = n.close();
should.exist(p);
@ -71,7 +71,22 @@ describe('Node', function() {
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) {
var n = new RedNode({id:'123',type:'abc'});
var callbacksClosed = 0;
@ -79,13 +94,13 @@ describe('Node', function() {
setTimeout(function() {
callbacksClosed++;
done();
},200);
},50);
});
n.on('close',function(done) {
setTimeout(function() {
callbacksClosed++;
done();
},200);
},75);
});
n.on('close',function() {
callbacksClosed++;