Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: fix unwrapping of ee.once() listeners #5564

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,7 @@ EventEmitter.prototype.addListener = function addListener(type, listener) {
// To avoid recursion in the case that type === "newListener"! Before
// adding it to the listeners, first emit "newListener".
if (events.newListener) {
this.emit('newListener', type,
listener.listener ? listener.listener : listener);
this.emit('newListener', type, unwrapListener(listener));

// Re-assign `events` because a newListener handler could have caused the
// this._events to be assigned to a new object
Expand Down Expand Up @@ -294,20 +293,20 @@ EventEmitter.prototype.removeListener =
if (!list)
return this;

if (list === listener || (list.listener && list.listener === listener)) {
if (list === listener || unwrapListener(list) === listener) {
if (--this._eventsCount === 0)
this._events = {};
else {
delete events[type];
if (events.removeListener)
this.emit('removeListener', type, listener);
this.emit('removeListener', type, unwrapListener(listener));
}
} else if (typeof list !== 'function') {
position = -1;

for (i = list.length; i-- > 0;) {
if (list[i] === listener ||
(list[i].listener && list[i].listener === listener)) {
unwrapListener(list[i]) === listener) {
position = i;
break;
}
Expand All @@ -329,7 +328,7 @@ EventEmitter.prototype.removeListener =
}

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

return this;
Expand Down Expand Up @@ -397,9 +396,9 @@ EventEmitter.prototype.listeners = function listeners(type) {
if (!evlistener)
ret = [];
else if (typeof evlistener === 'function')
ret = [evlistener];
ret = [unwrapListener(evlistener)];
else
ret = arrayClone(evlistener, evlistener.length);
ret = arrayMap(evlistener, unwrapListener);
}

return ret;
Expand Down Expand Up @@ -452,3 +451,15 @@ function arrayClone(arr, i) {
copy[i] = arr[i];
return copy;
}

function arrayMap(arr, fn) {
var ret = new Array(arr.length);
for (var i = 0; i < ret.length; ++i) {
ret[i] = fn(arr[i]);
}
return ret;
}

function unwrapListener(listener) {
return listener.listener ? listener.listener : listener;
}
9 changes: 9 additions & 0 deletions test/parallel/test-event-emitter-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ var e3ListenersCopy = e3.listeners('foo');
e3.on('foo', listener2);
assert.deepEqual(e3.listeners('foo'), [listener, listener2]);
assert.deepEqual(e3ListenersCopy, [listener]);

var e4 = new events.EventEmitter();
e4.once('foo', listener);
assert.deepEqual(e4.listeners('foo'), [listener]);

var e5 = new events.EventEmitter();
e5.on('foo', listener);
e5.once('foo', listener2);
assert.deepEqual(e5.listeners('foo'), [listener, listener2]);
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');