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: handle inherited properties properly #2350

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
14 changes: 7 additions & 7 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ EventEmitter.init = function() {
}

if (!this._events || this._events === Object.getPrototypeOf(this)._events) {
this._events = {};
this._events = Object.create(null);
this._eventsCount = 0;
}

Expand Down Expand Up @@ -198,7 +198,7 @@ EventEmitter.prototype.addListener = function addListener(type, listener) {

events = this._events;
if (!events) {
events = this._events = {};
events = this._events = Object.create(null);
this._eventsCount = 0;
} else {
// To avoid recursion in the case that type === "newListener"! Before
Expand Down Expand Up @@ -285,7 +285,7 @@ EventEmitter.prototype.removeListener =

if (list === listener || (list.listener && list.listener === listener)) {
if (--this._eventsCount === 0)
this._events = {};
this._events = Object.create(null);
else {
delete events[type];
if (events.removeListener)
Expand All @@ -308,7 +308,7 @@ EventEmitter.prototype.removeListener =
if (list.length === 1) {
list[0] = undefined;
if (--this._eventsCount === 0) {
this._events = {};
this._events = Object.create(null);
return this;
} else {
delete events[type];
Expand All @@ -335,11 +335,11 @@ EventEmitter.prototype.removeAllListeners =
// not listening for removeListener, no need to emit
if (!events.removeListener) {
if (arguments.length === 0) {
this._events = {};
this._events = Object.create(null);
this._eventsCount = 0;
} else if (events[type]) {
if (--this._eventsCount === 0)
this._events = {};
this._events = Object.create(null);
else
delete events[type];
}
Expand All @@ -355,7 +355,7 @@ EventEmitter.prototype.removeAllListeners =
this.removeAllListeners(key);
}
this.removeAllListeners('removeListener');
this._events = {};
this._events = Object.create(null);
this._eventsCount = 0;
return this;
}
Expand Down
28 changes: 22 additions & 6 deletions test/parallel/test-event-emitter-listener-count.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
'use strict';

const common = require('../common');
require('../common');
const assert = require('assert');
const EventEmitter = require('events');

const emitter = new EventEmitter();
emitter.on('foo', function() {});
emitter.on('foo', function() {});
emitter.on('baz', function() {});

function noop() {}

emitter.on('foo', noop);
emitter.on('foo', noop);
emitter.on('baz', noop);
// Allow any type
emitter.on(123, function() {});
emitter.on(123, noop);

assert.strictEqual(EventEmitter.listenerCount(emitter, 'foo'), 2);
assert.strictEqual(emitter.listenerCount('foo'), 2);
assert.strictEqual(emitter.listenerCount('bar'), 0);
assert.strictEqual(emitter.listenerCount('baz'), 1);
assert.strictEqual(emitter.listenerCount(123), 1);

// The inherited properties should not be counted towards the actual
// listeners count
assert.strictEqual(EventEmitter.listenerCount(emitter, 'toString'), 0);

// when we add a new listener with the name of an inherited property, it should
// accept it
emitter.on('toString', noop);
assert.strictEqual(EventEmitter.listenerCount(emitter, 'toString'), 1);

// after removing a listener with the name of an inherited property, the count
// should reduce by one
emitter.removeListener('toString', noop);
assert.strictEqual(EventEmitter.listenerCount(emitter, 'toString'), 0);
2 changes: 1 addition & 1 deletion test/parallel/test-event-emitter-listeners-side-effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ assert(fl.length === 1);
assert(fl[0] === assert.fail);

e.listeners('bar');
assert(!e._events.hasOwnProperty('bar'));
assert(!e._events['bar']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a necessary change?


e.on('foo', assert.ok);
fl = e.listeners('foo');
Expand Down