Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let trigger node be reset by boolean message #1554

Merged
merged 2 commits into from Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion nodes/core/core/89-trigger.js
Expand Up @@ -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];
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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); }
Expand Down
59 changes: 59 additions & 0 deletions test/nodes/core/core/89-trigger_spec.js
Expand Up @@ -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"} ];
Expand Down