diff options
Diffstat (limited to 'lib/events.js')
-rw-r--r-- | lib/events.js | 37 |
1 files changed, 18 insertions, 19 deletions
diff --git a/lib/events.js b/lib/events.js index 9c2ba5442a..b69dc8f20f 100644 --- a/lib/events.js +++ b/lib/events.js @@ -49,7 +49,7 @@ EventEmitter.defaultMaxListeners = 10; // Obviously not all Emitters should be limited to 10. This function allows // that to be increased. Set to zero for unlimited. EventEmitter.prototype.setMaxListeners = function(n) { - if (typeof n !== 'number' || n < 0) + if (!IS_NUMBER(n) || n < 0) throw TypeError('n must be a positive number'); this._maxListeners = n; return this; @@ -64,8 +64,7 @@ EventEmitter.prototype.emit = function(type) { // If there is no 'error' event listener then throw. if (type === 'error') { if (!this._events.error || - (typeof this._events.error === 'object' && - !this._events.error.length)) { + (IS_OBJECT(this._events.error) && !this._events.error.length)) { er = arguments[1]; if (this.domain) { if (!er) er = new TypeError('Uncaught, unspecified "error" event.'); @@ -84,13 +83,13 @@ EventEmitter.prototype.emit = function(type) { handler = this._events[type]; - if (typeof handler === 'undefined') + if (IS_UNDEFINED(handler)) return false; if (this.domain && this !== process) this.domain.enter(); - if (typeof handler === 'function') { + if (IS_FUNCTION(handler)) { switch (arguments.length) { // fast cases case 1: @@ -110,7 +109,7 @@ EventEmitter.prototype.emit = function(type) { args[i - 1] = arguments[i]; handler.apply(this, args); } - } else if (typeof handler === 'object') { + } else if (IS_OBJECT(handler)) { len = arguments.length; args = new Array(len - 1); for (i = 1; i < len; i++) @@ -131,7 +130,7 @@ EventEmitter.prototype.emit = function(type) { EventEmitter.prototype.addListener = function(type, listener) { var m; - if (typeof listener !== 'function') + if (!IS_FUNCTION(listener)) throw TypeError('listener must be a function'); if (!this._events) @@ -140,13 +139,13 @@ EventEmitter.prototype.addListener = function(type, listener) { // To avoid recursion in the case that type === "newListener"! Before // adding it to the listeners, first emit "newListener". if (this._events.newListener) - this.emit('newListener', type, typeof listener.listener === 'function' ? - listener.listener : listener); + this.emit('newListener', type, + IS_FUNCTION(listener.listener) ? listener.listener : listener); 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 (IS_OBJECT(this._events[type])) // If we've already got an array, just append. this._events[type].push(listener); else @@ -154,9 +153,9 @@ EventEmitter.prototype.addListener = function(type, listener) { this._events[type] = [this._events[type], listener]; // Check for listener leak - if (typeof this._events[type] === 'object' && !this._events[type].warned) { + if (IS_OBJECT(this._events[type]) && !this._events[type].warned) { var m; - if (this._maxListeners !== undefined) { + if (!IS_UNDEFINED(this._maxListeners)) { m = this._maxListeners; } else { m = EventEmitter.defaultMaxListeners; @@ -178,7 +177,7 @@ EventEmitter.prototype.addListener = function(type, listener) { EventEmitter.prototype.on = EventEmitter.prototype.addListener; EventEmitter.prototype.once = function(type, listener) { - if (typeof listener !== 'function') + if (!IS_FUNCTION(listener)) throw TypeError('listener must be a function'); function g() { @@ -196,7 +195,7 @@ EventEmitter.prototype.once = function(type, listener) { EventEmitter.prototype.removeListener = function(type, listener) { var list, position, length, i; - if (typeof listener !== 'function') + if (!IS_FUNCTION(listener)) throw TypeError('listener must be a function'); if (!this._events || !this._events[type]) @@ -207,12 +206,12 @@ EventEmitter.prototype.removeListener = function(type, listener) { position = -1; if (list === listener || - (typeof list.listener === 'function' && list.listener === listener)) { + (IS_FUNCTION(list.listener) && list.listener === listener)) { this._events[type] = undefined; if (this._events.removeListener) this.emit('removeListener', type, listener); - } else if (typeof list === 'object') { + } else if (IS_OBJECT(list)) { for (i = length; i-- > 0;) { if (list[i] === listener || (list[i].listener && list[i].listener === listener)) { @@ -266,7 +265,7 @@ EventEmitter.prototype.removeAllListeners = function(type) { listeners = this._events[type]; - if (typeof listeners === 'function') { + if (IS_FUNCTION(listeners)) { this.removeListener(type, listeners); } else { // LIFO order @@ -282,7 +281,7 @@ EventEmitter.prototype.listeners = function(type) { var ret; if (!this._events || !this._events[type]) ret = []; - else if (typeof this._events[type] === 'function') + else if (IS_FUNCTION(this._events[type])) ret = [this._events[type]]; else ret = this._events[type].slice(); @@ -293,7 +292,7 @@ EventEmitter.listenerCount = function(emitter, type) { var ret; if (!emitter._events || !emitter._events[type]) ret = 0; - else if (typeof emitter._events[type] === 'function') + else if (IS_FUNCTION(emitter._events[type])) ret = 1; else ret = emitter._events[type].length; |