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

Fix this context when calling multiple event listeners (fixes #2692).

This commit is contained in:
Melvin Groenhoff 2020-09-04 10:20:30 +02:00
parent 0c5eae2349
commit d84c2b780b
2 changed files with 31 additions and 1 deletions

View File

@ -214,7 +214,8 @@ Node.prototype._emitInput = function(arg) {
c++; c++;
} }
try { try {
node._inputCallbacks[i]( cb.call(
node,
arg, arg,
function() { node.send.apply(node,arguments) }, function() { node.send.apply(node,arguments) },
function(err) { function(err) {

View File

@ -216,6 +216,7 @@ describe('Node', function() {
n2.on('input',function(msg) { n2.on('input',function(msg) {
// msg equals message, and is not a new copy // msg equals message, and is not a new copy
messageReceived = true; messageReceived = true;
should.strictEqual(this,n2);
should.deepEqual(msg,message); should.deepEqual(msg,message);
should.strictEqual(msg,message); should.strictEqual(msg,message);
done(); done();
@ -224,6 +225,34 @@ describe('Node', function() {
messageReceived.should.be.false(); messageReceived.should.be.false();
}); });
it('emits a single message - multiple input event listeners', function(done) {
var flow = {
getNode: (id) => { return {'n1':n1,'n2':n2}[id]},
};
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 = 0;
n2.on('input',function(msg) {
// msg equals message, and is not a new copy
messageReceived++;
messageReceived.should.be.exactly(1);
should.strictEqual(this,n2);
should.deepEqual(msg,message);
should.strictEqual(msg,message);
});
n2.on('input',function() {
messageReceived++;
messageReceived.should.be.exactly(2);
should.strictEqual(this,n2);
should.deepEqual(msg,message);
should.strictEqual(msg,message);
done();
});
n1.send(message);
messageReceived.should.be.exactly(0);
});
it('emits a single message - synchronous mode', function(done) { it('emits a single message - synchronous mode', function(done) {
var flow = { var flow = {
getNode: (id) => { return {'n1':n1,'n2':n2}[id]}, getNode: (id) => { return {'n1':n1,'n2':n2}[id]},