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

Tidy up when usage in Flow and Node

This commit is contained in:
Nick O'Leary 2019-01-17 13:18:26 +00:00
parent 901b32297e
commit 490903ca25
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
5 changed files with 93 additions and 102 deletions

View File

@ -16,7 +16,6 @@
var util = require("util");
var EventEmitter = require("events").EventEmitter;
var when = require("when");
var redUtil = require("@node-red/util").util;
var Log = require("@node-red/util").log; // TODO: separate module
@ -92,21 +91,34 @@ Node.prototype.close = function(removed) {
var callback = this._closeCallbacks[i];
if (callback.length > 0) {
promises.push(
when.promise(function(resolve) {
new Promise((resolve) => {
try {
var args = [];
if (callback.length === 2) {
args.push(!!removed);
}
args.push(resolve);
args.push(() => {
resolve();
});
callback.apply(node, args);
} catch(err) {
// TODO: error thrown in node async close callback
// We've never logged this properly.
resolve();
}
})
);
} else {
try {
callback.call(node);
} catch(err) {
// TODO: error thrown in node sync close callback
// We've never logged this properly.
}
}
}
if (promises.length > 0) {
return when.settle(promises).then(function() {
return Promise.all(promises).then(function() {
if (this._context) {
return context.delete(this._alias||this.id,this.z);
}
@ -115,7 +127,7 @@ Node.prototype.close = function(removed) {
if (this._context) {
return context.delete(this._alias||this.id,this.z);
}
return;
return Promise.resolve();
}
};
@ -138,8 +150,6 @@ Node.prototype.send = function(msg) {
/* istanbul ignore else */
if (node) {
node.receive(msg);
} else {
console.log("trying to send to a node not on this flow",this._wire);
}
return;
} else {
@ -187,8 +197,6 @@ Node.prototype.send = function(msg) {
}
}
}
} else {
console.log("trying to send to a node not on this flow",this._wire);
}
}
}
@ -295,4 +303,5 @@ Node.prototype.metric = function(eventname, msg, metricValue) {
Node.prototype.status = function(status) {
this._flow.handleStatus(this,status);
};
module.exports = Node;

View File

@ -14,14 +14,14 @@
* limitations under the License.
**/
var when = require("when");
var clone = require("clone");
var Subflow;
var Log;
var redUtil = require("@node-red/util").util;
var flowUtil = require("./util");
var events = require("../../events");
var Subflow;
var Log;
var nodeCloseTimeout = 15000;
/**
@ -227,19 +227,9 @@ class Flow {
* @return {[type]} [description]
*/
stop(stopList, removedList) {
return new Promise((resolve,reject) => {
this.trace("stop "+this.TYPE);
var i;
if (stopList) {
// for (i=0;i<stopList.length;i++) {
// if (this.subflowInstanceNodes[stopList[i]]) {
// console.log("NEED TO STOP A SUBFLOW",stopList[i]);
// // The first in the list is the instance node we already
// // know about
// // stopList = stopList.concat(this.subflowInstanceNodes[stopList[i]].slice(1))
// }
// }
} else {
if (!stopList) {
stopList = Object.keys(this.activeNodes);
}
// Convert the list to a map to avoid multiple scans of the list
@ -265,17 +255,14 @@ class Flow {
} else {
try {
var removed = removedMap[stopList[i]];
promises.push(stopNode(node,removed));
promises.push(stopNode(node,removed).catch(()=>{}));
} catch(err) {
node.error(err);
}
}
}
}
when.settle(promises).then(function(results) {
resolve();
});
});
return Promise.all(promises);
}
@ -455,22 +442,20 @@ class Flow {
* @return {[type]} [description]
*/
function stopNode(node,removed) {
return when.promise(function(resolve, reject) {
var start;
when.promise(function(resolve) {
Log.trace("Stopping node "+node.type+":"+node.id+(removed?" removed":""));
start = Date.now();
resolve(node.close(removed));
}).timeout(nodeCloseTimeout).then(function(){
const start = Date.now();
const closePromise = node.close(removed);
const closeTimeout = new Promise((resolve,reject) => {
setTimeout(() => {
reject("Close timed out");
}, nodeCloseTimeout);
});
return Promise.race([closePromise,closeTimeout]).then(() => {
var delta = Date.now() - start;
Log.trace("Stopped node "+node.type+":"+node.id+" ("+delta+"ms)" );
resolve(delta);
},function(err) {
var delta = Date.now() - start;
}).catch(err => {
node.error(Log._("nodes.flows.stopping-error",{message:err}));
Log.debug(err.stack);
reject(err);
});
})
}

View File

@ -357,7 +357,7 @@ function start(type,diff,muteLog) {
function stop(type,diff,muteLog) {
if (!started) {
return when.resolve();
return Promise.resolve();
}
type = type||"full";
diff = diff||{
@ -395,8 +395,7 @@ function stop(type,diff,muteLog) {
}
}
return when.promise(function(resolve,reject) {
when.settle(promises).then(function() {
return Promise.resolve(promises).then(function() {
for (id in activeNodesToFlow) {
if (activeNodesToFlow.hasOwnProperty(id)) {
if (!activeFlows[activeNodesToFlow[id]]) {
@ -416,8 +415,7 @@ function stop(type,diff,muteLog) {
log.info(log._("nodes.flows.stopped-flows"));
}
}
resolve();
});
events.emit("nodes-stopped");
});
}

View File

@ -55,8 +55,7 @@ describe('Node', function() {
n.on('close',function() {
done();
});
var p = n.close();
should.not.exist(p);
n.close();
});
it('returns a promise when provided a callback with a done parameter',function(testdone) {

View File

@ -346,7 +346,7 @@ describe('Flow', function() {
stoppedNodes.should.have.a.property("2");
stoppedNodes.should.have.a.property("3");
done();
});
}).catch(done);
});
it("stops specified nodes",function(done) {