19
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
22
- var isArray = Array . isArray ;
23
22
var domain ;
24
23
25
24
exports . usingDomains = false ;
@@ -33,7 +32,7 @@ function EventEmitter() {
33
32
this . domain = domain . active ;
34
33
}
35
34
}
36
- this . _events = this . _events || null ;
35
+ this . _events = this . _events || { } ;
37
36
this . _maxListeners = this . _maxListeners || defaultMaxListeners ;
38
37
}
39
38
exports . EventEmitter = EventEmitter ;
@@ -57,8 +56,9 @@ var PROCESS;
57
56
EventEmitter . prototype . emit = function ( type ) {
58
57
// If there is no 'error' event listener then throw.
59
58
if ( type === 'error' ) {
60
- if ( ! this . _events || ! this . _events . error ||
61
- ( isArray ( this . _events . error ) && ! this . _events . error . length ) ) {
59
+ if ( ! this . _events . error ||
60
+ ( typeof this . _events . error === 'object' &&
61
+ ! this . _events . error . length ) ) {
62
62
if ( this . domain ) {
63
63
var er = arguments [ 1 ] ;
64
64
er . domainEmitter = this ;
@@ -77,7 +77,6 @@ EventEmitter.prototype.emit = function(type) {
77
77
}
78
78
}
79
79
80
- if ( ! this . _events ) return false ;
81
80
var handler = this . _events [ type ] ;
82
81
if ( ! handler ) return false ;
83
82
@@ -111,7 +110,7 @@ EventEmitter.prototype.emit = function(type) {
111
110
}
112
111
return true ;
113
112
114
- } else if ( isArray ( handler ) ) {
113
+ } else if ( typeof handler === 'object' ) {
115
114
if ( this . domain ) {
116
115
PROCESS = PROCESS || process ;
117
116
if ( this !== PROCESS ) {
@@ -153,7 +152,7 @@ EventEmitter.prototype.addListener = function(type, listener) {
153
152
if ( ! this . _events [ type ] ) {
154
153
// Optimize the case of one listener. Don't need the extra array object.
155
154
this . _events [ type ] = listener ;
156
- } else if ( isArray ( this . _events [ type ] ) ) {
155
+ } else if ( typeof this . _events [ type ] === 'object' ) {
157
156
158
157
// If we've already got an array, just append.
159
158
this . _events [ type ] . push ( listener ) ;
@@ -165,7 +164,7 @@ EventEmitter.prototype.addListener = function(type, listener) {
165
164
}
166
165
167
166
// Check for listener leak
168
- if ( isArray ( this . _events [ type ] ) && ! this . _events [ type ] . warned ) {
167
+ if ( typeof this . _events [ type ] === 'object' && ! this . _events [ type ] . warned ) {
169
168
var m ;
170
169
m = this . _maxListeners ;
171
170
@@ -219,11 +218,11 @@ EventEmitter.prototype.removeListener = function(type, listener) {
219
218
220
219
if ( list === listener ||
221
220
( typeof list . listener === 'function' && list . listener === listener ) ) {
222
- this . _events [ type ] = null ;
221
+ this . _events [ type ] = undefined ;
223
222
if ( this . _events . removeListener )
224
223
this . emit ( 'removeListener' , type , listener ) ;
225
224
226
- } else if ( isArray ( list ) ) {
225
+ } else if ( typeof list === 'object' ) {
227
226
for ( i = 0 ; i < length ; i ++ ) {
228
227
if ( list [ i ] === listener ||
229
228
( list [ i ] . listener && list [ i ] . listener === listener ) ) {
@@ -237,7 +236,7 @@ EventEmitter.prototype.removeListener = function(type, listener) {
237
236
238
237
if ( list . length === 1 ) {
239
238
list . length = 0 ;
240
- this . _events [ type ] = null ;
239
+ this . _events [ type ] = undefined ;
241
240
} else {
242
241
list . splice ( position , 1 ) ;
243
242
}
@@ -261,9 +260,9 @@ EventEmitter.prototype.removeAllListeners = function(type) {
261
260
// not listening for removeListener, no need to emit
262
261
if ( ! this . _events . removeListener ) {
263
262
if ( arguments . length === 0 )
264
- this . _events = null ;
263
+ this . _events = { } ;
265
264
else if ( this . _events [ type ] )
266
- this . _events [ type ] = null ;
265
+ this . _events [ type ] = undefined ;
267
266
return this ;
268
267
}
269
268
@@ -274,7 +273,7 @@ EventEmitter.prototype.removeAllListeners = function(type) {
274
273
this . removeAllListeners ( key ) ;
275
274
}
276
275
this . removeAllListeners ( 'removeListener' ) ;
277
- this . _events = null ;
276
+ this . _events = { } ;
278
277
return this ;
279
278
}
280
279
@@ -287,7 +286,7 @@ EventEmitter.prototype.removeAllListeners = function(type) {
287
286
while ( listeners . length )
288
287
this . removeListener ( type , listeners [ listeners . length - 1 ] ) ;
289
288
}
290
- this . _events [ type ] = null ;
289
+ this . _events [ type ] = undefined ;
291
290
292
291
return this ;
293
292
} ;
0 commit comments