type Callback < TEvent > = ( event : TEvent ) => void ; export interface EventSubscription { unsubscribe ( ) : void ; } export class EventEmitter < TEvent > { private readonly registry : { [ key ] : Callback < TEvent > ; } = { } ; private latest : TEvent ; private counter = 0 ; constructor ( first ? ) { this . latest = first ; } public get ( ) { return this . latest ; } public next ( event ) { Object . getOwnPropertyNames ( this . registry ) . forEach ( key => { const fn = this . registry [ key ] ; if ( fn ) { fn ( event ) ; } } ) ; this . latest = event ; } public on ( callback ) { const id = ++ this . counter ; this . registry [ id ] = callback ; return { unsubscribe : ( ) => delete this . registry [ id ] } ; } }