export interface Listener < T > { ( event ) : any ; } export type Unsubscribe = ( ) => void ; see https export class TypedEmitter < T > { private listeners : Listener < T > [ ] = [ ] ; private listenersOncer : Listener < T > [ ] = [ ] ; on = ( listener ) : Unsubscribe => { this . listeners . push ( listener ) ; return ( ) => this . off ( listener ) ; } ; once = ( listener ) : void => { this . listenersOncer . push ( listener ) ; } ; off = ( listener ) => { var callbackIndex = this . listeners . indexOf ( listener ) ; if ( callbackIndex > - 1 ) this . listeners . splice ( callbackIndex , 1 ) ; } ; emit = ( event ) => { this . listeners . forEach ( ( listener ) => listener ( event ) ) ; if ( this . listenersOncer . length > 0 ) { const toCall = this . listenersOncer ; this . listenersOncer = [ ] ; toCall . forEach ( ( listener ) => listener ( event ) ) ; } } ; pipe = ( typedEmitter ) : Unsubscribe => { return this . on ( ( e ) => typedEmitter . emit ( e ) ) ; } ; }