-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
events: _events should be an own property #2046
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) { | ||
| if (!emitter._events || | ||
| emitter._events === Object.getPrototypeOf(emitter)._events) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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);
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bnoordhuis in this example instances of A and B would have own
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
|
@@ -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; | ||
| }; | ||
|
|
@@ -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". | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ensureXxxfunction should not have a side-effect (no modified state).I feel
initializeXxxis better or remove side-effects like:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disagree.
ensureimplies idempotency, nothing more