export type EventListener < T > = ( event : T ) => void ; export class EventEmittable < TEvents extends { [ type ] : any ; } > { protected __eventListeners ? : Map < keyof TEvents , EventListener < any > [ ] > ; public on < TType extends keyof TEvents & string > ( type , listener ) { this . __eventListeners = this . __eventListeners || new Map ( ) ; let array = this . __eventListeners . get ( type ) ; if ( ! array ) { array = [ ] ; this . __eventListeners . set ( type , array ) ; } array . push ( listener ) ; return listener ; } public off < TType extends keyof TEvents & string > ( type , listener ) { this . __eventListeners = this . __eventListeners || new Map ( ) ; let array = this . __eventListeners . get ( type ) ; if ( ! array ) { array = [ ] ; this . __eventListeners . set ( type , array ) ; } const index = array . indexOf ( listener ) ; if ( index !== - 1 ) { array . splice ( index , 1 ) ; } } protected __emit < TType extends keyof TEvents > ( ... [ type , event ] ) { this . __eventListeners = this . __eventListeners || new Map ( ) ; this . __eventListeners . get ( type ) ?. forEach ( ( listener ) => listener ( event ) ) ; } }