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

Let trigger node also send last payload to arrive

and add test for it.
This commit is contained in:
Dave Conway-Jones 2016-04-24 17:42:24 +01:00
parent b2923d0fc4
commit 8916cf273e
4 changed files with 39 additions and 5 deletions

View File

@ -41,19 +41,20 @@
</select> </select>
</span> </span>
</div> </div>
<div class="form-row node-type-wait">
<input type="checkbox" id="node-input-extend" style="margin-left: 0px; vertical-align: top; width: auto !important;"> <label style="width:auto !important;" for="node-input-extend" data-i18n="trigger.extend"></label>
</div>
<div class="form-row node-type-wait"> <div class="form-row node-type-wait">
<span data-i18n="trigger.then-send"></span> <span data-i18n="trigger.then-send"></span>
<select id="node-input-op2type" style="width:200px !important"> <select id="node-input-op2type" style="width:200px !important">
<option value="val" data-i18n="trigger.output.string"></option> <option value="val" data-i18n="trigger.output.string"></option>
<option value="num" data-i18n="trigger.output.number"></option> <option value="num" data-i18n="trigger.output.number"></option>
<option value="pay" data-i18n="trigger.output.existing"></option> <option value="pay" data-i18n="trigger.output.original"></option>
<option value="payl" data-i18n="trigger.output.latest"></option>
<option value="nul" data-i18n="trigger.output.nothing"></option> <option value="nul" data-i18n="trigger.output.nothing"></option>
</select> </select>
<input style="width: 145px !important" type="text" id="node-input-op2"> <input style="width: 145px !important" type="text" id="node-input-op2">
</div> </div>
<div class="form-row node-type-wait">
<input type="checkbox" id="node-input-extend" style="margin-left: 0px; vertical-align: top; width: auto !important;"> <label style="width:auto !important;" for="node-input-extend" data-i18n="trigger.extend"></label>
</div>
<div class="form-row"> <div class="form-row">
<span data-i18n="trigger.label.reset"></span><input type="text" id="node-input-reset" style="width:240px" data-i18n="[placeholder]trigger.label.resetprompt"> <span data-i18n="trigger.label.reset"></span><input type="text" id="node-input-reset" style="width:240px" data-i18n="[placeholder]trigger.label.resetprompt">
</div> </div>

View File

@ -60,6 +60,7 @@ module.exports = function(RED) {
else { else {
if ((!tout) && (tout !== 0)) { if ((!tout) && (tout !== 0)) {
if (node.op2type === "pay") { m2 = msg.payload; } if (node.op2type === "pay") { m2 = msg.payload; }
else if (node.op2type === "payl") { m2 = msg.payload; }
else if (node.op2Templated) { m2 = mustache.render(node.op2,msg); } else if (node.op2Templated) { m2 = mustache.render(node.op2,msg); }
else { m2 = node.op2; } else { m2 = node.op2; }
if (node.op1type === "pay") { } if (node.op1type === "pay") { }
@ -79,6 +80,7 @@ module.exports = function(RED) {
} }
else if ((node.extend === "true" || node.extend === true) && (node.duration > 0)) { else if ((node.extend === "true" || node.extend === true) && (node.duration > 0)) {
clearTimeout(tout); clearTimeout(tout);
if (node.op2type === "payl") { m2 = msg.payload; }
tout = setTimeout(function() { tout = setTimeout(function() {
msg.payload = m2; msg.payload = m2;
if (node.op2type !== "nul") { node.send(msg); } if (node.op2type !== "nul") { node.send(msg); }

View File

@ -197,6 +197,8 @@
"string": "the string", "string": "the string",
"number": "the number", "number": "the number",
"existing": "the existing msg.payload", "existing": "the existing msg.payload",
"original": "the original msg.payload",
"latest": "the latest msg.payload",
"nothing": "nothing" "nothing": "nothing"
}, },
"wait-reset": "wait to be reset", "wait-reset": "wait to be reset",

View File

@ -235,6 +235,35 @@ describe('trigger Node', function() {
}); });
}); });
it('should be able to extend the delay and output the 2nd payload', function(done) {
var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", extend:"true", op1type:"nul", op2type:"payl", op1:"false", op2:"true", duration:200, 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) {
if (c === 0) {
msg.should.have.a.property("payload", "Goodbye");
c += 1;
}
else {
msg.should.have.a.property("payload", "World");
(Date.now() - ss).should.be.greaterThan(380);
done();
}
});
var ss = Date.now();
n1.emit("input", {payload:"Hello"});
setTimeout( function() {
n1.emit("input", {payload:"Goodbye"});
},100);
setTimeout( function() {
n1.emit("input", {payload:"World"});
},400);
});
});
it('should be able to apply mustache templates to payloads', function(done) { it('should be able to apply mustache templates to payloads', function(done) {
var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", op1type:"val", op2type:"val", op1:"{{payload}}", op2:"{{topic}}", duration:50, wires:[["n2"]] }, var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", op1type:"val", op2type:"val", op1:"{{payload}}", op2:"{{topic}}", duration:50, wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
@ -257,7 +286,7 @@ describe('trigger Node', function() {
}); });
it('should handle string null as null', function(done) { it('should handle string null as null', function(done) {
var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", op2type:"pay", op1:"null", op2:"null", duration:30, wires:[["n2"]] }, var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", op1type:"val", op2type:"pay", op1:"null", op2:"null", duration:40, wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(triggerNode, flow, function() { helper.load(triggerNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");