Skip to content

Commit

Permalink
events: fix 'removeListener' event after #once
Browse files Browse the repository at this point in the history
When a user listens for an event using #once, their listener is wrapped
in a function which will remove it after being called. While this was
accounted for with the 'newListener' event, 'removeListener' would
receive the wrapped function.

Fixes nodejs#5551
  • Loading branch information
omsmith committed Apr 11, 2016
1 parent 59d23ad commit f33eed3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ EventEmitter.prototype.removeListener =
else {
delete events[type];
if (events.removeListener)
this.emit('removeListener', type, listener);
this.emit('removeListener', type,
listener.listener ? listener.listener : listener);
}
} else if (typeof list !== 'function') {
position = -1;
Expand Down Expand Up @@ -329,7 +330,8 @@ EventEmitter.prototype.removeListener =
}

if (events.removeListener)
this.emit('removeListener', type, listener);
this.emit('removeListener', type,
listener.listener ? listener.listener : listener);
}

return this;
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-event-emitter-remove-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,19 @@ e6.emit('hello');

// Interal listener array [listener3]
e6.emit('hello');

const e7 = new events.EventEmitter();
e7.on('hello', listener1);
e7.once('hello', listener2);
e7.once('removeListener', common.mustCall(function(name, cb) {
assert.equal(name, 'hello');
assert.equal(cb, listener2);
assert.deepEqual([listener1], e7.listeners('hello'));
e7.once('removeListener', common.mustCall(function(name, cb) {
assert.equal(name, 'hello');
assert.equal(cb, listener1);
assert.deepEqual([], e7.listeners('hello'));
}));
e7.removeListener('hello', listener1);
}));
e7.emit('hello');

0 comments on commit f33eed3

Please sign in to comment.