|
@ -191,90 +191,90 @@ EventEmitter.prototype.once = function once(type, listener) { |
|
|
// emits a 'removeListener' event iff the listener was removed
|
|
|
// emits a 'removeListener' event iff the listener was removed
|
|
|
EventEmitter.prototype.removeListener = |
|
|
EventEmitter.prototype.removeListener = |
|
|
function removeListener(type, listener) { |
|
|
function removeListener(type, listener) { |
|
|
var list, position, length, i; |
|
|
var list, position, length, i; |
|
|
|
|
|
|
|
|
if (!util.isFunction(listener)) |
|
|
if (!util.isFunction(listener)) |
|
|
throw TypeError('listener must be a function'); |
|
|
throw TypeError('listener must be a function'); |
|
|
|
|
|
|
|
|
if (!this._events || !this._events[type]) |
|
|
if (!this._events || !this._events[type]) |
|
|
return this; |
|
|
return this; |
|
|
|
|
|
|
|
|
list = this._events[type]; |
|
|
list = this._events[type]; |
|
|
length = list.length; |
|
|
length = list.length; |
|
|
position = -1; |
|
|
position = -1; |
|
|
|
|
|
|
|
|
if (list === listener || |
|
|
if (list === listener || |
|
|
(util.isFunction(list.listener) && list.listener === listener)) { |
|
|
(util.isFunction(list.listener) && list.listener === listener)) { |
|
|
delete this._events[type]; |
|
|
delete this._events[type]; |
|
|
if (this._events.removeListener) |
|
|
if (this._events.removeListener) |
|
|
this.emit('removeListener', type, listener); |
|
|
this.emit('removeListener', type, listener); |
|
|
|
|
|
|
|
|
} else if (util.isObject(list)) { |
|
|
} else if (util.isObject(list)) { |
|
|
for (i = length; i-- > 0;) { |
|
|
for (i = length; i-- > 0;) { |
|
|
if (list[i] === listener || |
|
|
if (list[i] === listener || |
|
|
(list[i].listener && list[i].listener === listener)) { |
|
|
(list[i].listener && list[i].listener === listener)) { |
|
|
position = i; |
|
|
position = i; |
|
|
break; |
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (position < 0) |
|
|
|
|
|
return this; |
|
|
|
|
|
|
|
|
|
|
|
if (list.length === 1) { |
|
|
|
|
|
list.length = 0; |
|
|
|
|
|
delete this._events[type]; |
|
|
|
|
|
} else { |
|
|
|
|
|
spliceOne(list, position); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (this._events.removeListener) |
|
|
|
|
|
this.emit('removeListener', type, listener); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (position < 0) |
|
|
|
|
|
return this; |
|
|
return this; |
|
|
|
|
|
}; |
|
|
if (list.length === 1) { |
|
|
|
|
|
list.length = 0; |
|
|
|
|
|
delete this._events[type]; |
|
|
|
|
|
} else { |
|
|
|
|
|
spliceOne(list, position); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (this._events.removeListener) |
|
|
|
|
|
this.emit('removeListener', type, listener); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return this; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
EventEmitter.prototype.removeAllListeners = |
|
|
EventEmitter.prototype.removeAllListeners = |
|
|
function removeAllListeners(type) { |
|
|
function removeAllListeners(type) { |
|
|
var key, listeners; |
|
|
var key, listeners; |
|
|
|
|
|
|
|
|
if (!this._events) |
|
|
if (!this._events) |
|
|
return this; |
|
|
return this; |
|
|
|
|
|
|
|
|
// not listening for removeListener, no need to emit
|
|
|
// not listening for removeListener, no need to emit
|
|
|
if (!this._events.removeListener) { |
|
|
if (!this._events.removeListener) { |
|
|
if (arguments.length === 0) |
|
|
if (arguments.length === 0) |
|
|
this._events = {}; |
|
|
this._events = {}; |
|
|
else if (this._events[type]) |
|
|
else if (this._events[type]) |
|
|
delete this._events[type]; |
|
|
delete this._events[type]; |
|
|
return this; |
|
|
return this; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// emit removeListener for all listeners on all events
|
|
|
// emit removeListener for all listeners on all events
|
|
|
if (arguments.length === 0) { |
|
|
if (arguments.length === 0) { |
|
|
for (key in this._events) { |
|
|
for (key in this._events) { |
|
|
if (key === 'removeListener') continue; |
|
|
if (key === 'removeListener') continue; |
|
|
this.removeAllListeners(key); |
|
|
this.removeAllListeners(key); |
|
|
} |
|
|
} |
|
|
this.removeAllListeners('removeListener'); |
|
|
this.removeAllListeners('removeListener'); |
|
|
this._events = {}; |
|
|
this._events = {}; |
|
|
return this; |
|
|
return this; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
listeners = this._events[type]; |
|
|
listeners = this._events[type]; |
|
|
|
|
|
|
|
|
if (util.isFunction(listeners)) { |
|
|
if (util.isFunction(listeners)) { |
|
|
this.removeListener(type, listeners); |
|
|
this.removeListener(type, listeners); |
|
|
} else if (Array.isArray(listeners)) { |
|
|
} else if (Array.isArray(listeners)) { |
|
|
// LIFO order
|
|
|
// LIFO order
|
|
|
while (listeners.length) |
|
|
while (listeners.length) |
|
|
this.removeListener(type, listeners[listeners.length - 1]); |
|
|
this.removeListener(type, listeners[listeners.length - 1]); |
|
|
} |
|
|
} |
|
|
delete this._events[type]; |
|
|
delete this._events[type]; |
|
|
|
|
|
|
|
|
return this; |
|
|
return this; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
EventEmitter.prototype.listeners = function listeners(type) { |
|
|
EventEmitter.prototype.listeners = function listeners(type) { |
|
|
var ret; |
|
|
var ret; |
|
|