mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00: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:
		
				
					committed by
					
						
						Nick O'Leary
					
				
			
			
				
	
			
			
			
						parent
						
							8d7c157751
						
					
				
				
					commit
					9bc72c1a06
				
			@@ -80,7 +80,7 @@ module.exports = function(RED) {
 | 
				
			|||||||
            var topic = msg.topic || "_none";
 | 
					            var topic = msg.topic || "_none";
 | 
				
			||||||
            if (node.bytopic === "all") { topic = "_none"; }
 | 
					            if (node.bytopic === "all") { topic = "_none"; }
 | 
				
			||||||
            node.topics[topic] = node.topics[topic] || {};
 | 
					            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); }
 | 
					                if (node.loop === true) { clearInterval(node.topics[topic].tout); }
 | 
				
			||||||
                else { clearTimeout(node.topics[topic].tout); }
 | 
					                else { clearTimeout(node.topics[topic].tout); }
 | 
				
			||||||
                delete node.topics[topic];
 | 
					                delete node.topics[topic];
 | 
				
			||||||
@@ -104,7 +104,9 @@ module.exports = function(RED) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                    if (node.duration === 0) { node.topics[topic].tout = 0; }
 | 
					                    if (node.duration === 0) { node.topics[topic].tout = 0; }
 | 
				
			||||||
                    else if (node.loop === true) {
 | 
					                    else if (node.loop === true) {
 | 
				
			||||||
 | 
					                        /* istanbul ignore else  */
 | 
				
			||||||
                        if (node.topics[topic].tout) { clearInterval(node.topics[topic].tout); }
 | 
					                        if (node.topics[topic].tout) { clearInterval(node.topics[topic].tout); }
 | 
				
			||||||
 | 
					                        /* istanbul ignore else  */
 | 
				
			||||||
                        if (node.op1type !== "nul") {
 | 
					                        if (node.op1type !== "nul") {
 | 
				
			||||||
                            var msg2 = RED.util.cloneMessage(msg);
 | 
					                            var msg2 = RED.util.cloneMessage(msg);
 | 
				
			||||||
                            node.topics[topic].tout = setInterval(function() { node.send(RED.util.cloneMessage(msg2)); }, node.duration);
 | 
					                            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:" "});
 | 
					                    node.status({fill:"blue",shape:"dot",text:" "});
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                else if ((node.extend === "true" || node.extend === true) && (node.duration > 0)) {
 | 
					                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); }
 | 
					                    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); }
 | 
					                    if (node.topics[topic].tout) { clearTimeout(node.topics[topic].tout); }
 | 
				
			||||||
                    node.topics[topic].tout = setTimeout(function() {
 | 
					                    node.topics[topic].tout = setTimeout(function() {
 | 
				
			||||||
                        var msg2 = null;
 | 
					                        var msg2 = null;
 | 
				
			||||||
@@ -153,6 +157,7 @@ module.exports = function(RED) {
 | 
				
			|||||||
        });
 | 
					        });
 | 
				
			||||||
        this.on("close", function() {
 | 
					        this.on("close", function() {
 | 
				
			||||||
            for (var t in node.topics) {
 | 
					            for (var t in node.topics) {
 | 
				
			||||||
 | 
					                /* istanbul ignore else  */
 | 
				
			||||||
                if (node.topics[t]) {
 | 
					                if (node.topics[t]) {
 | 
				
			||||||
                    if (node.loop === true) { clearInterval(node.topics[t].tout); }
 | 
					                    if (node.loop === true) { clearInterval(node.topics[t].tout); }
 | 
				
			||||||
                    else { clearTimeout(node.topics[t].tout); }
 | 
					                    else { clearTimeout(node.topics[t].tout); }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -609,11 +609,70 @@ describe('trigger node', function() {
 | 
				
			|||||||
            n1.emit("input", {payload:null});   // trigger
 | 
					            n1.emit("input", {payload:null});   // trigger
 | 
				
			||||||
            n1.emit("input", {payload:null});   // blocked
 | 
					            n1.emit("input", {payload:null});   // blocked
 | 
				
			||||||
            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:"boo"});  // clear the blockage
 | 
				
			||||||
            n1.emit("input", {payload:null});   // trigger
 | 
					            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) {
 | 
					    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"]] },
 | 
					        var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", reset:"boo", op1:"", op1type:"pay", duration:-25, wires:[["n2"]] },
 | 
				
			||||||
            {id:"n2", type:"helper"} ];
 | 
					            {id:"n2", type:"helper"} ];
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user