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: don't keep arrays with a single listener #12043

Closed
wants to merge 1 commit 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
18 changes: 11 additions & 7 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ EventEmitter.prototype.removeListener =
} else if (typeof list !== 'function') {
position = -1;

for (i = list.length; i-- > 0;) {
for (i = list.length - 1; i >= 0; i--) {
if (list[i] === listener || list[i].listener === listener) {
originalListener = list[i].listener;
position = i;
Expand All @@ -375,7 +375,6 @@ EventEmitter.prototype.removeListener =
return this;

if (list.length === 1) {
list[0] = undefined;
if (--this._eventsCount === 0) {
this._events = Object.create(null);
return this;
Expand All @@ -384,8 +383,12 @@ EventEmitter.prototype.removeListener =
}
} else if (position === 0) {
list.shift();
if (list.length === 1)
events[type] = list[0];
} else {
spliceOne(list, position);
if (list.length === 1)
events[type] = list[0];
}

if (events.removeListener)
Expand All @@ -397,7 +400,7 @@ EventEmitter.prototype.removeListener =

EventEmitter.prototype.removeAllListeners =
function removeAllListeners(type) {
var listeners, events;
var listeners, events, i;

events = this._events;
if (!events)
Expand All @@ -420,7 +423,8 @@ EventEmitter.prototype.removeAllListeners =
// emit removeListener for all listeners on all events
if (arguments.length === 0) {
var keys = Object.keys(events);
for (var i = 0, key; i < keys.length; ++i) {
var key;
for (i = 0; i < keys.length; ++i) {
key = keys[i];
if (key === 'removeListener') continue;
this.removeAllListeners(key);
Expand All @@ -437,9 +441,9 @@ EventEmitter.prototype.removeAllListeners =
this.removeListener(type, listeners);
} else if (listeners) {
// LIFO order
do {
this.removeListener(type, listeners[listeners.length - 1]);
} while (listeners[0]);
for (i = listeners.length - 1; i >= 0; i--) {
this.removeListener(type, listeners[i]);
}
}

return this;
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-event-emitter-remove-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,20 @@ assert.throws(() => {
const e = ee.removeListener('foo', listener);
assert.strictEqual(e, ee);
}

{
const ee = new EventEmitter();

ee.on('foo', listener1);
ee.on('foo', listener2);
assert.deepStrictEqual(ee.listeners('foo'), [listener1, listener2]);

ee.removeListener('foo', listener1);
assert.strictEqual(ee._events.foo, listener2);

ee.on('foo', listener1);
assert.deepStrictEqual(ee.listeners('foo'), [listener2, listener1]);

ee.removeListener('foo', listener1);
assert.strictEqual(ee._events.foo, listener2);
}