class EventEmitter { protected callbacks: Map void>> constructor() { this.callbacks = new Map() } /** Emits an event on this event emitter */ emit (name: string, evt: any): this { const callbacks = this.callbacks.get(name) if (callbacks) { callbacks.forEach(cb => { cb(evt) }) } return this } /** Adds an event listener */ on (name: string, cb: (evt: any) => void): this { const set = this.callbacks.get(name) || new Set() this.callbacks.set(name, set) set.add(cb) return this } /** Removes an event listener */ off (name: string, cb?: (evt: any) => void): this { if (cb) { const set = this.callbacks.get(name) if (set) { set.delete(cb) } if (set.size === 0) { this.callbacks.delete(name) } } else { this.callbacks.delete(name) } return this } } export { EventEmitter }