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

Merge pull request #2693 from mgroenhoff/master

Fix `this` context when calling multiple event listeners (fixes #2692).
This commit is contained in:
Nick O'Leary 2020-09-04 09:33:23 +01:00 committed by GitHub
commit 44b1819926
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

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

View File

@ -216,6 +216,7 @@ describe('Node', function() {
n2.on('input',function(msg) {
// msg equals message, and is not a new copy
messageReceived = true;
should.strictEqual(this,n2);
should.deepEqual(msg,message);
should.strictEqual(msg,message);
done();
@ -224,6 +225,34 @@ describe('Node', function() {
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) {
var flow = {
getNode: (id) => { return {'n1':n1,'n2':n2}[id]},