type Listener = ( data : any ) => void ; export interface Handle { remove ( ) : void ; } export class EventEmitter { events : { [ key ] : Listener [ ] ; } = null ; constructor ( ) { this . events = { } ; } emit ( eventName , data ) { var listeners = this . events [ eventName ] ; if ( listeners ) { listeners . forEach ( ( listener ) => { listener ( data ) ; } ) ; } } on ( eventName , listener ) { return this . addEventListener ( eventName , listener ) ; } addEventListener ( eventName , listener ) { var listeners = this . events [ eventName ] ; if ( listeners ) { listeners . push ( listener ) ; } else { this . events [ eventName ] = [ listener ] ; } var handle = { } ; handle . remove = ( ) => this . removeEventListener ( eventName , listener ) ; return handle ; } removeEventListener ( eventName , listener ) { var listeners = this . events [ eventName ] ; if ( listeners ) { var index1 = listeners . indexOf ( listener ) ; if ( index1 >= 0 ) { listeners . splice ( index1 , 1 ) ; } } } removeAllEventListeners ( eventName ? ) { if ( eventName ) { this . events [ eventName ] = [ ] ; } else { this . events = { } ; } } } ;