export interface Listener < T > { ( event ) : any ; } export interface Disposable { dispose ( ) ; } export class CompositeDisposible implements Disposable { private disposibles : Disposable [ ] = [ ] ; add = ( disposible ) => { this . disposibles . push ( disposible ) ; } ; dispose = ( ) => { this . disposibles . forEach ( d => d . dispose ( ) ) ; } ; } /** passes through events as they happen. You will not get events from before you start listening */ export class TypedEvent < T > { private listeners : Listener < T > [ ] = [ ] ; private listenersOncer : Listener < T > [ ] = [ ] ; on = ( listener ) : Disposable => { this . listeners . push ( listener ) ; return { dispose : ( ) => 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 ) => { /** Update any general listeners */ this . listeners . forEach ( ( listener ) => listener ( event ) ) ; /** Clear the `once` queue */ this . listenersOncer . forEach ( ( listener ) => listener ( event ) ) ; this . listenersOncer = [ ] ; } ; pipe = ( te ) : Disposable => { return this . on ( ( e ) => te . emit ( e ) ) ; } ; }