Browse Source

events: removeListener add checks and cleanup

Remove unecessary splice for single listener events. Add type check for
"type" argument.
v0.9.12-release
Trevor Norris 12 years ago
committed by isaacs
parent
commit
b3ea8443bd
  1. 51
      lib/events.js

51
lib/events.js

@ -203,43 +203,48 @@ EventEmitter.prototype.once = function(type, listener) {
// emits a 'removeListener' event iff the listener was removed
EventEmitter.prototype.removeListener = function(type, listener) {
if ('function' !== typeof listener) {
throw TypeError('removeListener only takes instances of Function');
}
var list, position, length, i;
// does not use listeners(), so no side effect of creating _events[type]
if (!this._events || !this._events[type]) return this;
if (typeof type !== 'string')
throw TypeError('type must be a string');
if (typeof listener !== 'function')
throw TypeError('listener must be a function');
var list = this._events[type];
if (!this._events || !this._events[type])
return this;
list = this._events[type];
length = list.length;
position = -1;
if (list === listener ||
(typeof list.listener === 'function' && list.listener === listener)) {
this._events[type] = null;
if (this._events.removeListener)
this.emit('removeListener', type, listener);
if (isArray(list)) {
var position = -1;
for (var i = 0, length = list.length; i < length; i++) {
} else if (isArray(list)) {
for (i = 0; i < length; i++) {
if (list[i] === listener ||
(list[i].listener && list[i].listener === listener))
{
(list[i].listener && list[i].listener === listener)) {
position = i;
break;
}
}
if (position < 0) return this;
list.splice(position, 1);
if (list.length == 0)
this._events[type] = null;
if (position < 0)
return this;
if (this._events.removeListener) {
this.emit('removeListener', type, listener);
}
} else if (list === listener ||
(list.listener && list.listener === listener))
{
if (list.length === 1) {
list.length = 0;
this._events[type] = null;
} else {
list.splice(position, 1);
}
if (this._events.removeListener) {
if (this._events.removeListener)
this.emit('removeListener', type, listener);
}
}
return this;
};

Loading…
Cancel
Save