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

Add send to input handler signature

This commit is contained in:
Nick O'Leary 2019-07-09 11:40:55 +01:00
parent 3b5ea0f15f
commit f52289b2c3
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
4 changed files with 43 additions and 11 deletions

View File

@ -81,7 +81,7 @@ module.exports = function(RED) {
}
}
this.on("input", function(msg, done) {
this.on("input", function(msg, send, done) {
if (this.complete === "true") {
// debug complete msg object
if (this.console === "true") {

View File

@ -328,12 +328,12 @@ module.exports = function(RED) {
}
if (valid) {
this.on('input', function(msg, done) {
this.on('input', function(msg, send, done) {
applyRules(msg, 0, (err,msg) => {
if (err) {
node.error(err,msg);
} else if (msg) {
node.send(msg);
send(msg);
}
done();
})

View File

@ -137,7 +137,11 @@ Node.prototype._emitInput = function(arg) {
var node = this;
if (node._inputCallback) {
try {
node._inputCallback(arg,function(err) { node._complete(arg,err); });
node._inputCallback(
arg,
function() { node.send.apply(node,arguments) },
function(err) { node._complete(arg,err); }
);
} catch(err) {
node.error(err,arg);
}
@ -149,12 +153,16 @@ Node.prototype._emitInput = function(arg) {
c++;
}
try {
node._inputCallbacks[i](arg,function(err) {
c--;
if (c === 0) {
node._complete(arg,err);
node._inputCallbacks[i](
arg,
function() { node.send.apply(node,arguments) },
function(err) {
c--;
if (c === 0) {
node._complete(arg,err);
}
}
});
);
} catch(err) {
node.error(err,msg);
}

View File

@ -176,7 +176,7 @@ describe('Node', function() {
}});
var message = {payload:"hello world"};
n.on('input',function(msg, nodeDone) {
n.on('input',function(msg, nodeSend, nodeDone) {
nodeDone();
});
n.receive(message);
@ -186,7 +186,7 @@ describe('Node', function() {
sinon.stub(n,"error",function(err,msg) {});
var message = {payload:"hello world"};
n.on('input',function(msg, nodeDone) {
n.on('input',function(msg, nodeSend, nodeDone) {
nodeDone(new Error("test error"));
setTimeout(function() {
try {
@ -253,6 +253,30 @@ describe('Node', function() {
}
});
it('emits a message with callback provided send', function(done) {
var flow = {
getNode: (id) => { return {'n1':n1,'n2':n2}[id]},
handleComplete: (node,msg) => {}
};
var n1 = new RedNode({_flow:flow,id:'n1',type:'abc',wires:[['n2']]});
var n2 = new RedNode({_flow:flow,id:'n2',type:'abc'});
var message = {payload:"hello world"};
var messageReceived = false;
n1.on('input',function(msg,nodeSend,nodeDone) {
nodeSend(msg);
nodeDone();
});
n2.on('input',function(msg) {
// msg equals message, and is not a new copy
messageReceived = true;
should.deepEqual(msg,message);
should.strictEqual(msg,message);
done();
});
n1.receive(message);
messageReceived.should.be.false();
});
it('emits multiple messages on a single output', function(done) {
var flow = {
getNode: (id) => { return {'n1':n1,'n2':n2}[id]},