Skip to content
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
21 changes: 14 additions & 7 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ EventEmitter.prototype._maxListeners = undefined;
// added to it. This is a useful default which helps finding memory leaks.
EventEmitter.defaultMaxListeners = 10;

function ensureEvents(emitter) {
Copy link
Member

Choose a reason for hiding this comment

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

ensureXxx function should not have a side-effect (no modified state).
I feel initializeXxx is better or remove side-effects like:

function ensureEvents(emitter) {
  if (!emitter._events ||
      emitter._events === Object.getPrototypeOf(emitter)._events) {
    return false;
  }
  return true;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Disagree. ensure implies idempotency, nothing more

if (!emitter._events ||
emitter._events === Object.getPrototypeOf(emitter)._events) {
Copy link
Member

Choose a reason for hiding this comment

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

This won't work for code like below, I think? Instances of B won't have an ._events property on their immediate prototype.

function A() { EventEmitter.call(this); }
A.prototype = Object.create(EventEmitter.prototype);
A.prototype._events = {};

function B() { A.call(this); }
B.prototype = Object.create(A.prototype);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bnoordhuis in this example instances of A and B would have own _events because of super constructor calls. If you omit those it should still be fine. Object.getPrototypeOf(emitter)._events returns inherited _events if immediate prototype has none.

Copy link
Member

Choose a reason for hiding this comment

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

Good point. I wonder though, what is the reason for not just using Object.hasOwnProperty(emitter, '_events')?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was a bit faster at the time

emitter._events = {};
emitter._eventsCount = 0;
return false;
}

return true;
}

EventEmitter.init = function() {
this.domain = null;
if (EventEmitter.usingDomains) {
Expand All @@ -30,10 +41,7 @@ EventEmitter.init = function() {
}
}

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

this._maxListeners = this._maxListeners || undefined;
};
Expand Down Expand Up @@ -197,9 +205,8 @@ EventEmitter.prototype.addListener = function addListener(type, listener) {
throw new TypeError('listener must be a function');

events = this._events;
if (!events) {
events = this._events = {};
this._eventsCount = 0;
if (!ensureEvents(this)) {
events = this._events;
} else {
// To avoid recursion in the case that type === "newListener"! Before
// adding it to the listeners, first emit "newListener".
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-event-emitter-subclass.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ process.on('exit', function() {
});


// Incorrect subclassing should work

function MyEE2() {
EventEmitter.call(this);
}
Expand All @@ -47,3 +49,14 @@ var ee2 = new MyEE2();
ee1.on('x', function() {});

assert.equal(EventEmitter.listenerCount(ee2, 'x'), 0);

function MyEE3() {}

MyEE3.prototype = new EventEmitter();

var ee1 = new MyEE3();
var ee2 = new MyEE3();

ee1.on('x', function() {});

assert.equal(EventEmitter.listenerCount(ee2, 'x'), 0);