export interface EmitListener < T > { ( value ) : void | Promise < void > ; } export interface EmitHandle { close ( ) : void ; } export interface Emitter < T > { on ( listener ) : EmitHandle ; } export interface EmitterHandler < T > extends Emitter < T > { emit ( value ) : void ; } export function emitter < T > ( ) { const listeners = [ ] ; return { on ( listener ) { listeners . push ( listener ) ; return { close ( ) { const index = listeners . indexOf ( listener ) ; if ( index >= 0 ) { listeners . splice ( index , 1 ) ; } } , } ; } , emit ( value ) { for ( const listener of listeners ) { listener ( value ) ; } } , } ; }