export interface EventEmitterOptions < EventPayload = any > { logger ? : ( log : string , payload ? : EventPayload ) => void ; } export type EventHandler < EventPayload > = ( ( payload : EventPayload ) => Promise < void > | void ) | null | undefined ; export class EventEmitter < EventPayload > { private handlerCount = 0 ; private handlers : Array < EventHandler < EventPayload > > = [ ] ; constructor ( private options ? ) { } public get numberOfHandlers ( ) { return this . handlers . filter ( h => ! ! h ) . length ; } public async emit ( payload ) { const promises = [ ] ; this . options ?. logger ?. ( 'emit' , payload ) ; for ( const handler of this . handlers ) { if ( handler ) { const res = handler ( payload ) as Promise < void > ; if ( typeof res ?. then === 'function' ) { promises . push ( res ) ; } } } await Promise . all ( promises ) ; } public on ( handler ) { this . options ?. logger ?. ( 'on' ) ; this . handlers . push ( handler ) ; return this . handlerCount ++ ; } public off ( handlerId ) { this . delete ( handlerId ) ; } public delete ( handlerId ) { this . options ?. logger ?. ( 'off' ) ; this . handlers [ handlerId ] = null ; } }