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
|
// To avoid recursion in the case that type == "newListeners"! Before
|
||||||
// adding it to the listeners, first emit "newListeners".
|
// adding it to the listeners, first emit "newListeners".
|
||||||
|
if (this._events.newListener) {
|
||||||
this.emit('newListener', type, typeof listener.listener === 'function' ?
|
this.emit('newListener', type, typeof listener.listener === 'function' ?
|
||||||
listener.listener : listener);
|
listener.listener : listener);
|
||||||
|
}
|
||||||
|
|
||||||
if (!this._events[type]) {
|
if (!this._events[type]) {
|
||||||
// Optimize the case of one listener. Don't need the extra array object.
|
// Optimize the case of one listener. Don't need the extra array object.
|
||||||
@ -217,13 +219,19 @@ EventEmitter.prototype.removeListener = function(type, listener) {
|
|||||||
list.splice(position, 1);
|
list.splice(position, 1);
|
||||||
if (list.length == 0)
|
if (list.length == 0)
|
||||||
delete this._events[type];
|
delete this._events[type];
|
||||||
|
|
||||||
|
if (this._events.removeListener) {
|
||||||
this.emit('removeListener', type, listener);
|
this.emit('removeListener', type, listener);
|
||||||
|
}
|
||||||
} else if (list === listener ||
|
} else if (list === listener ||
|
||||||
(list.listener && list.listener === listener))
|
(list.listener && list.listener === listener))
|
||||||
{
|
{
|
||||||
delete this._events[type];
|
delete this._events[type];
|
||||||
|
|
||||||
|
if (this._events.removeListener) {
|
||||||
this.emit('removeListener', type, listener);
|
this.emit('removeListener', type, listener);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user