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>
</span>
</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">
<span data-i18n="trigger.then-send"></span>
<select id="node-input-op2type" style="width:200px !important">
<option value="val" data-i18n="trigger.output.string"></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>
</select>
<input style="width: 145px !important" type="text" id="node-input-op2">
</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">
<span data-i18n="trigger.label.reset"></span><input type="text" id="node-input-reset" style="width:240px" data-i18n="[placeholder]trigger.label.resetprompt">
</div>

View File

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

View File

@ -197,6 +197,8 @@
"string": "the string",
"number": "the number",
"existing": "the existing msg.payload",
"original": "the original msg.payload",
"latest": "the latest msg.payload",
"nothing": "nothing"
},
"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) {
var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", op1type:"val", op2type:"val", op1:"{{payload}}", op2:"{{topic}}", duration:50, wires:[["n2"]] },
{id:"n2", type:"helper"} ];
@ -257,7 +286,7 @@ describe('trigger Node', function() {
});
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"} ];
helper.load(triggerNode, flow, function() {
var n1 = helper.getNode("n1");