import { Events } from '../types' ; export type Handler < T = any > = ( event : T ) => void ; interface Emitter { on < T = any > ( event , handler ) : void ; emit < T = any > ( event , args ? ) : void ; off ( ) : void ; } export const emitter = ( ( ) : Emitter => { const events = new Map ( ) ; return { param { Events } event param { Handler } callback on < T = any > ( event , callback ) { if ( ! events . has ( event ) ) events . set ( event , [ ] ) ; events . get ( event ) . push ( callback ) ; } , param { Events } event param { Any } args emit < T = any > ( event , args ) { if ( ! events . has ( event ) ) return ; events . get ( event ) . forEach ( ( callback ) => callback ( args ) ) ; } , /** Remove all events. */ off ( ) { events . clear ( ) ; } } ; } ) ( ) ;