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

Better fix for trigegr 2nd message in last payload mode

Now works correctly in multiple topics mode.
And update tests
This commit is contained in:
Dave Conway-Jones 2020-02-25 22:15:53 +00:00
parent 3d6170be5e
commit 1868289b71
No known key found for this signature in database
GPG Key ID: 302A6725C594817F
2 changed files with 45 additions and 5 deletions

View File

@ -76,6 +76,7 @@ module.exports = function(RED) {
var node = this; var node = this;
node.topics = {}; node.topics = {};
var npay = {};
var pendingMessages = []; var pendingMessages = [];
var activeMessagePromise = null; var activeMessagePromise = null;
var processMessageQueue = function(msg) { var processMessageQueue = function(msg) {
@ -106,9 +107,7 @@ module.exports = function(RED) {
}); });
} }
var npay;
this.on('input', function(msg) { this.on('input', function(msg) {
if (node.op2type === "payl") { npay = RED.util.cloneMessage(msg); }
processMessageQueue(msg); processMessageQueue(msg);
}); });
@ -124,6 +123,7 @@ module.exports = function(RED) {
node.status({}); node.status({});
} }
else { else {
if (node.op2type === "payl") { npay[topic] = RED.util.cloneMessage(msg); }
if (((!node.topics[topic].tout) && (node.topics[topic].tout !== 0)) || (node.loop === true)) { if (((!node.topics[topic].tout) && (node.topics[topic].tout !== 0)) || (node.loop === true)) {
promise = Promise.resolve(); promise = Promise.resolve();
if (node.op2type === "pay") { node.topics[topic].m2 = RED.util.cloneMessage(msg.payload); } if (node.op2type === "pay") { node.topics[topic].m2 = RED.util.cloneMessage(msg.payload); }
@ -188,10 +188,15 @@ module.exports = function(RED) {
}); });
} }
promise.then(() => { promise.then(() => {
msg2.payload = node.topics[topic].m2; if (node.op2type === "payl") {
node.send(npay[topic]);
delete npay[topic];
}
else {
msg2.payload = node.topics[topic].m2;
node.send(msg2);
}
delete node.topics[topic]; delete node.topics[topic];
if (node.op2type === "payl") { node.send(npay); }
else { node.send(msg2); }
node.status({}); node.status({});
}).catch(err => { }).catch(err => {
node.error(err); node.error(err);

View File

@ -759,6 +759,41 @@ describe('trigger node', function() {
}); });
}); });
it('should be able output the 2nd payload and handle multiple topics', function(done) {
var flow = [{"id":"n1", "type":"trigger", "name":"triggerNode", extend:"false", op1type:"nul", op2type:"payl", op1:"false", op2:"true", duration:"80", bytopic:"topic", 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 {
if (c === 0) {
msg.should.have.a.property("payload", "Goodbye1");
msg.should.have.a.property("topic", "test1");
c += 1;
}
else {
msg.should.have.a.property("payload", "Goodbye2");
msg.should.have.a.property("topic", "test2");
done();
}
}
catch(err) { done(err); }
});
n1.emit("input", {payload:"Hello1", topic:"test1"});
setTimeout( function() {
n1.emit("input", {payload:"Hello2", topic:"test2"});
},20);
setTimeout( function() {
n1.emit("input", {payload:"Goodbye2", topic:"test2"});
},20);
setTimeout( function() {
n1.emit("input", {payload:"Goodbye1", topic:"test1"});
},20);
});
});
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"} ];