diff options
author | cjihrig <cjihrig@gmail.com> | 2015-01-28 20:05:53 -0500 |
---|---|---|
committer | cjihrig <cjihrig@gmail.com> | 2015-01-31 23:47:29 -0500 |
commit | 6ac8bdc0aba5f60f4b4f2da5abd36d664062aa40 (patch) | |
tree | 29a3b1ce92cfad3ae5d41a4ba7451846beace950 /lib/events.js | |
parent | bce7a2608eb198eee6ecd7991062efd6daeeb440 (diff) | |
download | android-node-v8-6ac8bdc0aba5f60f4b4f2da5abd36d664062aa40.tar.gz android-node-v8-6ac8bdc0aba5f60f4b4f2da5abd36d664062aa40.tar.bz2 android-node-v8-6ac8bdc0aba5f60f4b4f2da5abd36d664062aa40.zip |
lib: reduce util.is*() usage
Many of the util.is*() methods used to check data types
simply compare against a single value or the result of
typeof. This commit replaces calls to these methods with
equivalent checks. This commit does not touch calls to the
more complex methods (isRegExp(), isDate(), etc.).
Fixes: https://github.com/iojs/io.js/issues/607
PR-URL: https://github.com/iojs/io.js/pull/647
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'lib/events.js')
-rw-r--r-- | lib/events.js | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/lib/events.js b/lib/events.js index 672131deed..cece093d38 100644 --- a/lib/events.js +++ b/lib/events.js @@ -1,7 +1,6 @@ 'use strict'; var domain; -const util = require('util'); function EventEmitter() { EventEmitter.init.call(this); @@ -40,14 +39,14 @@ EventEmitter.init = function() { // 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 setMaxListeners(n) { - if (!util.isNumber(n) || n < 0 || isNaN(n)) + if (typeof n !== 'number' || n < 0 || isNaN(n)) throw TypeError('n must be a positive number'); this._maxListeners = n; return this; }; function $getMaxListeners(that) { - if (util.isUndefined(that._maxListeners)) + if (that._maxListeners === undefined) return EventEmitter.defaultMaxListeners; return that._maxListeners; } @@ -82,13 +81,13 @@ EventEmitter.prototype.emit = function emit(type) { handler = this._events[type]; - if (util.isUndefined(handler)) + if (handler === undefined) return false; if (this.domain && this !== process) this.domain.enter(); - if (util.isFunction(handler)) { + if (typeof handler === 'function') { switch (arguments.length) { // fast cases case 1: @@ -108,7 +107,7 @@ EventEmitter.prototype.emit = function emit(type) { args[i - 1] = arguments[i]; handler.apply(this, args); } - } else if (util.isObject(handler)) { + } else if (handler !== null && typeof handler === 'object') { len = arguments.length; args = new Array(len - 1); for (i = 1; i < len; i++) @@ -129,7 +128,7 @@ EventEmitter.prototype.emit = function emit(type) { EventEmitter.prototype.addListener = function addListener(type, listener) { var m; - if (!util.isFunction(listener)) + if (typeof listener !== 'function') throw TypeError('listener must be a function'); if (!this._events) @@ -139,13 +138,13 @@ EventEmitter.prototype.addListener = function addListener(type, listener) { // adding it to the listeners, first emit "newListener". if (this._events.newListener) this.emit('newListener', type, - util.isFunction(listener.listener) ? + typeof listener.listener === 'function' ? 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 (util.isObject(this._events[type])) + else if (typeof this._events[type] === 'object') // If we've already got an array, just append. this._events[type].push(listener); else @@ -153,7 +152,8 @@ EventEmitter.prototype.addListener = function addListener(type, listener) { this._events[type] = [this._events[type], listener]; // Check for listener leak - if (util.isObject(this._events[type]) && !this._events[type].warned) { + if (this._events[type] !== null && typeof this._events[type] === 'object' && + !this._events[type].warned) { var m = $getMaxListeners(this); if (m && m > 0 && this._events[type].length > m) { this._events[type].warned = true; @@ -171,7 +171,7 @@ EventEmitter.prototype.addListener = function addListener(type, listener) { EventEmitter.prototype.on = EventEmitter.prototype.addListener; EventEmitter.prototype.once = function once(type, listener) { - if (!util.isFunction(listener)) + if (typeof listener !== 'function') throw TypeError('listener must be a function'); var fired = false; @@ -196,7 +196,7 @@ EventEmitter.prototype.removeListener = function removeListener(type, listener) { var list, position, length, i; - if (!util.isFunction(listener)) + if (typeof listener !== 'function') throw TypeError('listener must be a function'); if (!this._events || !this._events[type]) @@ -207,12 +207,13 @@ EventEmitter.prototype.removeListener = position = -1; if (list === listener || - (util.isFunction(list.listener) && list.listener === listener)) { + (typeof list.listener === 'function' && + list.listener === listener)) { delete this._events[type]; if (this._events.removeListener) this.emit('removeListener', type, listener); - } else if (util.isObject(list)) { + } else if (list !== null && typeof list === 'object') { for (i = length; i-- > 0;) { if (list[i] === listener || (list[i].listener && list[i].listener === listener)) { @@ -267,7 +268,7 @@ EventEmitter.prototype.removeAllListeners = listeners = this._events[type]; - if (util.isFunction(listeners)) { + if (typeof listeners === 'function') { this.removeListener(type, listeners); } else if (Array.isArray(listeners)) { // LIFO order @@ -283,7 +284,7 @@ EventEmitter.prototype.listeners = function listeners(type) { var ret; if (!this._events || !this._events[type]) ret = []; - else if (util.isFunction(this._events[type])) + else if (typeof this._events[type] === 'function') ret = [this._events[type]]; else ret = this._events[type].slice(); @@ -294,7 +295,7 @@ EventEmitter.listenerCount = function(emitter, type) { var ret; if (!emitter._events || !emitter._events[type]) ret = 0; - else if (util.isFunction(emitter._events[type])) + else if (typeof emitter._events[type] === 'function') ret = 1; else ret = emitter._events[type].length; |