events: additional type check for addListener

Check both passed args to addListener.

Place var at beginning.
This commit is contained in:
Trevor Norris 2013-02-26 10:20:44 -08:00 committed by isaacs
parent 4f7f8bbdf8
commit 1ccc6fbe05

View File

@ -136,38 +136,35 @@ EventEmitter.prototype.emit = function(type) {
};
EventEmitter.prototype.addListener = function(type, listener) {
if ('function' !== typeof listener) {
throw TypeError('addListener only takes instances of Function');
}
var m;
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".
if (this._events.newListener) {
if (this._events.newListener)
this.emit('newListener', type, typeof listener.listener === 'function' ?
listener.listener : listener);
}
if (!this._events[type]) {
if (!this._events[type])
// Optimize the case of one listener. Don't need the extra array object.
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.
this._events[type].push(listener);
} else {
else
// Adding the second element, need to change to array.
this._events[type] = [this._events[type], listener];
}
// Check for listener leak
if (typeof this._events[type] === 'object' && !this._events[type].warned) {
var m;
m = this._maxListeners;
if (m && m > 0 && this._events[type].length > m) {
this._events[type].warned = true;
console.error('(node) warning: possible EventEmitter memory ' +