From 1ccc6fbe05653053bce015e24dd9aaa78b4ecc5b Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Tue, 26 Feb 2013 10:20:44 -0800 Subject: [PATCH] events: additional type check for addListener Check both passed args to addListener. Place var at beginning. --- lib/events.js | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/lib/events.js b/lib/events.js index 45577c8c602..eedd10aa9eb 100644 --- a/lib/events.js +++ b/lib/events.js @@ -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 ' +