@ -53,8 +53,8 @@ var PROCESS;
EventEmitter . prototype . emit = function ( type ) {
EventEmitter . prototype . emit = function ( type ) {
// If there is no 'error' event listener then throw.
// If there is no 'error' event listener then throw.
if ( type === 'error' ) {
if ( type === 'error' ) {
if ( ! this . _ events || ! this . _ events . eve rror ||
if ( ! this . _ events || ! this . _ events . error ||
( isArray ( this . _ events . eve rror ) && ! this . _ events . ev error. length ) )
( isArray ( this . _ events . error ) && ! this . _ events . error . length ) )
{
{
if ( this . domain ) {
if ( this . domain ) {
var er = arguments [ 1 ] ;
var er = arguments [ 1 ] ;
@ -75,8 +75,7 @@ EventEmitter.prototype.emit = function(type) {
}
}
if ( ! this . _ events ) return false ;
if ( ! this . _ events ) return false ;
var evtype = 'ev' + type ;
var handler = this . _ events [ type ] ;
var handler = this . _ events [ evtype ] ;
if ( ! handler ) return false ;
if ( ! handler ) return false ;
if ( typeof handler == 'function' ) {
if ( typeof handler == 'function' ) {
@ -143,37 +142,36 @@ EventEmitter.prototype.addListener = function(type, listener) {
// To avoid recursion in the case that type == "newListener"! Before
// To avoid recursion in the case that type == "newListener"! Before
// adding it to the listeners, first emit "newListener".
// adding it to the listeners, first emit "newListener".
if ( this . _ events . ev newListener) {
if ( this . _ events . newListener ) {
this . emit ( 'newListener' , type , typeof listener . listener === 'function' ?
this . emit ( 'newListener' , type , typeof listener . listener === 'function' ?
listener . listener : listener ) ;
listener . listener : listener ) ;
}
}
var evtype = 'ev' + type ;
if ( ! this . _ events [ type ] ) {
if ( ! this . _ events [ evtype ] ) {
// Optimize the case of one listener. Don't need the extra array object.
// Optimize the case of one listener. Don't need the extra array object.
this . _ events [ ev type] = listener ;
this . _ events [ type ] = listener ;
} else if ( isArray ( this . _ events [ ev type] ) ) {
} else if ( isArray ( this . _ events [ type ] ) ) {
// If we've already got an array, just append.
// If we've already got an array, just append.
this . _ events [ ev type] . push ( listener ) ;
this . _ events [ type ] . push ( listener ) ;
} else {
} else {
// Adding the second element, need to change to array.
// Adding the second element, need to change to array.
this . _ events [ ev type] = [ this . _ events [ ev type] , listener ] ;
this . _ events [ type ] = [ this . _ events [ type ] , listener ] ;
}
}
// Check for listener leak
// Check for listener leak
if ( isArray ( this . _ events [ ev type] ) && ! this . _ events [ ev type] . warned ) {
if ( isArray ( this . _ events [ type ] ) && ! this . _ events [ type ] . warned ) {
var m ;
var m ;
m = this . _ maxListeners ;
m = this . _ maxListeners ;
if ( m && m > 0 && this . _ events [ ev type] . length > m ) {
if ( m && m > 0 && this . _ events [ type ] . length > m ) {
this . _ events [ ev type] . warned = true ;
this . _ events [ type ] . warned = true ;
console . error ( '(node) warning: possible EventEmitter memory ' +
console . error ( '(node) warning: possible EventEmitter memory ' +
'leak detected. %d listeners added. ' +
'leak detected. %d listeners added. ' +
'Use emitter.setMaxListeners() to increase limit.' ,
'Use emitter.setMaxListeners() to increase limit.' ,
this . _ events [ ev type] . length ) ;
this . _ events [ type ] . length ) ;
console . trace ( ) ;
console . trace ( ) ;
}
}
}
}
@ -206,11 +204,10 @@ EventEmitter.prototype.removeListener = function(type, listener) {
throw new Error ( 'removeListener only takes instances of Function' ) ;
throw new Error ( 'removeListener only takes instances of Function' ) ;
}
}
var evtype = 'ev' + type ;
// does not use listeners(), so no side effect of creating _events[type]
// does not use listeners(), so no side effect of creating _events[evtype]
if ( ! this . _ events || ! this . _ events [ type ] ) return this ;
if ( ! this . _ events || ! this . _ events [ evtype ] ) return this ;
var list = this . _ events [ ev type] ;
var list = this . _ events [ type ] ;
if ( isArray ( list ) ) {
if ( isArray ( list ) ) {
var position = - 1 ;
var position = - 1 ;
@ -226,17 +223,17 @@ EventEmitter.prototype.removeListener = function(type, listener) {
if ( position < 0 ) return this ;
if ( position < 0 ) return this ;
list . splice ( position , 1 ) ;
list . splice ( position , 1 ) ;
if ( list . length == 0 )
if ( list . length == 0 )
this . _ events [ ev type] = null ;
this . _ events [ type ] = null ;
if ( this . _ events . ev removeListener) {
if ( this . _ events . removeListener ) {
this . emit ( 'removeListener' , type , listener ) ;
this . emit ( 'removeListener' , type , listener ) ;
}
}
} else if ( list === listener ||
} else if ( list === listener ||
( list . listener && list . listener === listener ) )
( list . listener && list . listener === listener ) )
{
{
this . _ events [ ev type] = null ;
this . _ events [ type ] = null ;
if ( this . _ events . ev removeListener) {
if ( this . _ events . removeListener ) {
this . emit ( 'removeListener' , type , listener ) ;
this . emit ( 'removeListener' , type , listener ) ;
}
}
}
}
@ -248,11 +245,11 @@ EventEmitter.prototype.removeAllListeners = function(type) {
if ( ! this . _ events ) return this ;
if ( ! this . _ events ) return this ;
// fast path
// fast path
if ( ! this . _ events . ev removeListener) {
if ( ! this . _ events . removeListener ) {
if ( arguments . length === 0 ) {
if ( arguments . length === 0 ) {
this . _ events = { } ;
this . _ events = { } ;
} else if ( type && this . _ events && this . _ events [ 'ev' + type ] ) {
} else if ( type && this . _ events && this . _ events [ type ] ) {
this . _ events [ 'ev' + type ] = null ;
this . _ events [ type ] = null ;
}
}
return this ;
return this ;
}
}
@ -260,16 +257,15 @@ EventEmitter.prototype.removeAllListeners = function(type) {
// slow(ish) path, emit 'removeListener' events for all removals
// slow(ish) path, emit 'removeListener' events for all removals
if ( arguments . length === 0 ) {
if ( arguments . length === 0 ) {
for ( var key in this . _ events ) {
for ( var key in this . _ events ) {
if ( key === 'ev removeListener' ) continue ;
if ( key === 'removeListener' ) continue ;
this . removeAllListeners ( key . slice ( 2 ) ) ;
this . removeAllListeners ( key ) ;
}
}
this . removeAllListeners ( 'removeListener' ) ;
this . removeAllListeners ( 'removeListener' ) ;
this . _ events = { } ;
this . _ events = { } ;
return this ;
return this ;
}
}
var evtype = 'ev' + type ;
var listeners = this . _ events [ type ] ;
var listeners = this . _ events [ evtype ] ;
if ( isArray ( listeners ) ) {
if ( isArray ( listeners ) ) {
while ( listeners . length ) {
while ( listeners . length ) {
// LIFO order
// LIFO order
@ -278,16 +274,15 @@ EventEmitter.prototype.removeAllListeners = function(type) {
} else if ( listeners ) {
} else if ( listeners ) {
this . removeListener ( type , listeners ) ;
this . removeListener ( type , listeners ) ;
}
}
this . _ events [ ev type] = null ;
this . _ events [ type ] = null ;
return this ;
return this ;
} ;
} ;
EventEmitter . prototype . listeners = function ( type ) {
EventEmitter . prototype . listeners = function ( type ) {
var evtype = 'ev' + type ;
if ( ! this . _ events || ! this . _ events [ type ] ) return [ ] ;
if ( ! this . _ events || ! this . _ events [ evtype ] ) return [ ] ;
if ( ! isArray ( this . _ events [ type ] ) ) {
if ( ! isArray ( this . _ events [ evtype ] ) ) {
return [ this . _ events [ type ] ] ;
return [ this . _ events [ evtype ] ] ;
}
}
return this . _ events [ ev type] . slice ( 0 ) ;
return this . _ events [ type ] . slice ( 0 ) ;
} ;
} ;