/**
 * EventBus 观察者模式事件监听
 */ type MapArgsType = string | [ ] | object | symbol | null | undefined ; export interface MapArgs { [ key ] : MapArgsType ; } class EventBus { public static instance : EventBus = new EventBus ( ) ; private eventsMap : Map < string , MapArgsType [ ] > ; constructor ( ) { this . eventsMap = new Map ( ) ; } param name param args example public Emit ( name , args ? ) { if ( toString . call ( args ) === "[object Function]" ) { throw TypeError ( `args type is Function, don't use Function` ) ; } this . eventsMap . set ( name , args ) ; } param name param fn public On ( name , fn ) { const getNameFn = this . eventsMap . get ( name ) ! ; const getNameFnType = toString . call ( getNameFn ) ; switch ( getNameFnType ) { case "[object Array]" : case "[object Object]" : fn . apply ( this , ... getNameFn ) ; break ; default : fn . call ( this , getNameFn ) ; break ; } } public Off ( name ) { this . eventsMap . delete ( name ) ; } } export default EventBus . instance ;