events: speed up newListener/removeListener events
This commit is contained in:
parent
84221fd1d6
commit
b7fd55e9a0
@ -133,8 +133,10 @@ EventEmitter.prototype.addListener = function(type, listener) {
|
||||
|
||||
// To avoid recursion in the case that type == "newListeners"! Before
|
||||
// adding it to the listeners, first emit "newListeners".
|
||||
this.emit('newListener', type, typeof listener.listener === 'function' ?
|
||||
listener.listener : listener);
|
||||
if (this._events.newListener) {
|
||||
this.emit('newListener', type, typeof listener.listener === 'function' ?
|
||||
listener.listener : listener);
|
||||
}
|
||||
|
||||
if (!this._events[type]) {
|
||||
// Optimize the case of one listener. Don't need the extra array object.
|
||||
@ -217,12 +219,18 @@ EventEmitter.prototype.removeListener = function(type, listener) {
|
||||
list.splice(position, 1);
|
||||
if (list.length == 0)
|
||||
delete this._events[type];
|
||||
this.emit('removeListener', type, listener);
|
||||
|
||||
if (this._events.removeListener) {
|
||||
this.emit('removeListener', type, listener);
|
||||
}
|
||||
} else if (list === listener ||
|
||||
(list.listener && list.listener === listener))
|
||||
{
|
||||
delete this._events[type];
|
||||
this.emit('removeListener', type, listener);
|
||||
|
||||
if (this._events.removeListener) {
|
||||
this.emit('removeListener', type, listener);
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
|
Loading…
x
Reference in New Issue
Block a user