Skip to content

Commit

Permalink
events: get rid of _eventsCount property
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed May 24, 2015
1 parent 62315b9 commit ca75072
Showing 1 changed file with 10 additions and 29 deletions.
39 changes: 10 additions & 29 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ EventEmitter.init = function() {
}
}

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

this._maxListeners = this._maxListeners || undefined;
};
Expand Down Expand Up @@ -199,7 +197,6 @@ EventEmitter.prototype.addListener = function addListener(type, listener) {
events = this._events;
if (!events) {
events = this._events = new Map();
this._eventsCount = 0;
} else {
// To avoid recursion in the case that type === "newListener"! Before
// adding it to the listeners, first emit "newListener".
Expand All @@ -217,7 +214,6 @@ EventEmitter.prototype.addListener = function addListener(type, listener) {
if (!existing) {
// Optimize the case of one listener. Don't need the extra array object.
events.set(type, listener);
++this._eventsCount;
} else {
if (typeof existing === 'function') {
// Adding the second element, need to change to array.
Expand Down Expand Up @@ -285,13 +281,9 @@ EventEmitter.prototype.removeListener =
return this;

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

Expand All @@ -308,12 +300,7 @@ EventEmitter.prototype.removeListener =

if (list.length === 1) {
list[0] = undefined;
if (--this._eventsCount === 0) {
this._events.clear();
return this;
} else {
events.delete(type);
}
events.delete(type);
} else {
spliceOne(list, position);
}
Expand All @@ -335,15 +322,10 @@ EventEmitter.prototype.removeAllListeners =

// not listening for removeListener, no need to emit
if (!events.has('removeListener')) {
if (arguments.length === 0) {
this._events.clear();
this._eventsCount = 0;
} else if (events.has(type)) {
if (--this._eventsCount === 0)
this._events.clear();
else
events.delete(type);
}
if (arguments.length === 0)
events.clear();
else
events.delete(type);
return this;
}

Expand All @@ -354,8 +336,7 @@ EventEmitter.prototype.removeAllListeners =
this.removeAllListeners(key);
}
this.removeAllListeners('removeListener');
this._events.clear();
this._eventsCount = 0;
events.clear();
return this;
}

Expand Down

0 comments on commit ca75072

Please sign in to comment.