From 9bc72c1a06d63273b18ff2af3fa40429f482ebd1 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Wed, 17 Jan 2018 09:51:53 +0000 Subject: [PATCH] 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 --- nodes/core/core/89-trigger.js | 7 ++- test/nodes/core/core/89-trigger_spec.js | 59 +++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/nodes/core/core/89-trigger.js b/nodes/core/core/89-trigger.js index 72cdf59aa..f1df7ea8b 100644 --- a/nodes/core/core/89-trigger.js +++ b/nodes/core/core/89-trigger.js @@ -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); } diff --git a/test/nodes/core/core/89-trigger_spec.js b/test/nodes/core/core/89-trigger_spec.js index 53b13564a..9faff3bb3 100644 --- a/test/nodes/core/core/89-trigger_spec.js +++ b/test/nodes/core/core/89-trigger_spec.js @@ -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"} ];