/** * tiny event emitter * modify from mitt */ type EventHandler = (...data: unknown[]) => void; type EventMap = Record; type HandlersMap = { [K in keyof T]: T[K][]; }; class EventEmitter { private handlersMap: HandlersMap = Object.create(null); on(type: T, handler: U[T]) { if (!this.handlersMap[type]) { this.handlersMap[type] = []; } this.handlersMap[type].push(handler); return this; } off(type: T, handler: U[T]) { if (this.handlersMap[type]) { this.handlersMap[type].splice(this.handlersMap[type].indexOf(handler) >>> 0, 1); } return this; } emit(type: T, ...data: Parameters) { if (this.handlersMap[type]) { this.handlersMap[type].slice().forEach(handler => { handler(...data); }); } return this; } } export default EventEmitter;