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

let trigger node be reset by boolean message (#1554)

* let trigger node be reset by boolean message

with test

* fix trigger node boolean reset check to work with false

and add test
This commit is contained in:
Dave Conway-Jones 2018-01-17 09:51:53 +00:00 committed by Nick O'Leary
parent 8d7c157751
commit 9bc72c1a06
2 changed files with 65 additions and 1 deletions

View File

@ -80,7 +80,7 @@ module.exports = function(RED) {
var topic = msg.topic || "_none";
if (node.bytopic === "all") { topic = "_none"; }
node.topics[topic] = node.topics[topic] || {};
if (msg.hasOwnProperty("reset") || ((node.reset !== '') && (msg.payload == node.reset)) ) {
if (msg.hasOwnProperty("reset") || ((node.reset !== '') && msg.hasOwnProperty("payload") && (msg.payload !== null) && msg.payload.toString && (msg.payload.toString() == node.reset)) ) {
if (node.loop === true) { clearInterval(node.topics[topic].tout); }
else { clearTimeout(node.topics[topic].tout); }
delete node.topics[topic];
@ -104,7 +104,9 @@ module.exports = function(RED) {
if (node.duration === 0) { node.topics[topic].tout = 0; }
else if (node.loop === true) {
/* istanbul ignore else */
if (node.topics[topic].tout) { clearInterval(node.topics[topic].tout); }
/* istanbul ignore else */
if (node.op1type !== "nul") {
var msg2 = RED.util.cloneMessage(msg);
node.topics[topic].tout = setInterval(function() { node.send(RED.util.cloneMessage(msg2)); }, node.duration);
@ -128,7 +130,9 @@ module.exports = function(RED) {
node.status({fill:"blue",shape:"dot",text:" "});
}
else if ((node.extend === "true" || node.extend === true) && (node.duration > 0)) {
/* istanbul ignore else */
if (node.op2type === "payl") { node.topics[topic].m2 = RED.util.cloneMessage(msg.payload); }
/* istanbul ignore else */
if (node.topics[topic].tout) { clearTimeout(node.topics[topic].tout); }
node.topics[topic].tout = setTimeout(function() {
var msg2 = null;
@ -153,6 +157,7 @@ module.exports = function(RED) {
});
this.on("close", function() {
for (var t in node.topics) {
/* istanbul ignore else */
if (node.topics[t]) {
if (node.loop === true) { clearInterval(node.topics[t].tout); }
else { clearTimeout(node.topics[t].tout); }

View File

@ -609,11 +609,70 @@ describe('trigger node', function() {
n1.emit("input", {payload:null}); // trigger
n1.emit("input", {payload:null}); // blocked
n1.emit("input", {payload:null}); // blocked
n1.emit("input", {payload:"foo"}); // don't clear the blockage
n1.emit("input", {payload:"boo"}); // clear the blockage
n1.emit("input", {payload:null}); // trigger
});
});
it('should be able to set infinite timeout, and clear timeout by boolean true', function(done) {
var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", reset:"true", duration:"0", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(triggerNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var c = 0;
n2.on("input", function(msg) {
try {
c += 1;
msg.should.have.a.property("payload", "1");
}
catch(err) { done(err); }
});
setTimeout( function() {
if (c === 2) { done(); }
else {
done(new Error("Too many messages received"));
}
},20);
n1.emit("input", {payload:null}); // trigger
n1.emit("input", {payload:null}); // blocked
n1.emit("input", {payload:null}); // blocked
n1.emit("input", {payload:false}); // don't clear the blockage
n1.emit("input", {payload:true}); // clear the blockage
n1.emit("input", {payload:null}); // trigger
});
});
it('should be able to set infinite timeout, and clear timeout by boolean false', function(done) {
var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", reset:"false", duration:"0", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(triggerNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var c = 0;
n2.on("input", function(msg) {
try {
c += 1;
msg.should.have.a.property("payload", "1");
}
catch(err) { done(err); }
});
setTimeout( function() {
if (c === 2) { done(); }
else {
done(new Error("Too many messages received"));
}
},20);
n1.emit("input", {payload:null}); // trigger
n1.emit("input", {payload:null}); // blocked
n1.emit("input", {payload:null}); // blocked
n1.emit("input", {payload:"foo"}); // don't clear the blockage
n1.emit("input", {payload:false}); // clear the blockage
n1.emit("input", {payload:null}); // trigger
});
});
it('should be able to set a repeat, and clear loop by reset', function(done) {
var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", reset:"boo", op1:"", op1type:"pay", duration:-25, wires:[["n2"]] },
{id:"n2", type:"helper"} ];