events: additional type check for addListener
Check both passed args to addListener. Place var at beginning.
This commit is contained in:
parent
4f7f8bbdf8
commit
1ccc6fbe05
@ -136,38 +136,35 @@ EventEmitter.prototype.emit = function(type) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
EventEmitter.prototype.addListener = function(type, listener) {
|
EventEmitter.prototype.addListener = function(type, listener) {
|
||||||
if ('function' !== typeof listener) {
|
var m;
|
||||||
throw TypeError('addListener only takes instances of Function');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this._events) this._events = {};
|
if (typeof type !== 'string')
|
||||||
|
throw TypeError('type must be a string');
|
||||||
|
if (typeof listener !== 'function')
|
||||||
|
throw TypeError('listener must be a function');
|
||||||
|
|
||||||
// To avoid recursion in the case that type == "newListener"! Before
|
if (!this._events)
|
||||||
|
this._events = {};
|
||||||
|
|
||||||
|
// To avoid recursion in the case that type === "newListener"! Before
|
||||||
// adding it to the listeners, first emit "newListener".
|
// adding it to the listeners, first emit "newListener".
|
||||||
if (this._events.newListener) {
|
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.
|
||||||
this._events[type] = listener;
|
this._events[type] = listener;
|
||||||
} else if (typeof this._events[type] === 'object') {
|
else if (typeof this._events[type] === 'object')
|
||||||
|
|
||||||
// If we've already got an array, just append.
|
// If we've already got an array, just append.
|
||||||
this._events[type].push(listener);
|
this._events[type].push(listener);
|
||||||
|
else
|
||||||
} else {
|
|
||||||
// Adding the second element, need to change to array.
|
// Adding the second element, need to change to array.
|
||||||
this._events[type] = [this._events[type], listener];
|
this._events[type] = [this._events[type], listener];
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for listener leak
|
// Check for listener leak
|
||||||
if (typeof this._events[type] === 'object' && !this._events[type].warned) {
|
if (typeof this._events[type] === 'object' && !this._events[type].warned) {
|
||||||
var m;
|
|
||||||
m = this._maxListeners;
|
m = this._maxListeners;
|
||||||
|
|
||||||
if (m && m > 0 && this._events[type].length > m) {
|
if (m && m > 0 && this._events[type].length > m) {
|
||||||
this._events[type].warned = true;
|
this._events[type].warned = true;
|
||||||
console.error('(node) warning: possible EventEmitter memory ' +
|
console.error('(node) warning: possible EventEmitter memory ' +
|
||||||
|
Loading…
x
Reference in New Issue
Block a user