interface Listeners { [ eventName ] : Function [ ] ; } export class EventEmitter { private listeners : Listeners = { } ; public on ( name , callback ) { const listeners = this . listeners [ name ] || [ ] ; this . listeners [ name ] = [ ... listeners , callback ] ; return ( ) => { this . off ( name , callback ) ; } ; } public off ( name , callback ) { const listeners = this . listeners [ name ] || [ ] ; this . listeners [ name ] = listeners . filter ( ( l ) => l !== callback ) ; } public emit ( name , data ) { const listeners = this . listeners [ name ] || [ ] ; listeners . forEach ( ( l ) => l ( data ) ) ; } public destroy ( ) { this . listeners = { } ; } }