/** * EventEmitter implementation, similar to the one used in CodeMirror */ export abstract class EventEmitter { private handlers: Partial[] = []; /** * Subscribe to events * @param handlers */ on(handlers: Partial) { this.handlers.push(handlers); } /** * Unsubscribe from events * @param handlers */ off(handlers: Partial) { this.handlers = this.handlers.filter((h) => h !== handlers); } /** * Broadcast an event to all subscribers * @param eventName * @param args */ async emit(eventName: keyof HandlerT, ...args: any[]): Promise { for (const handler of this.handlers) { const fn: any = handler[eventName]; if (fn) { await Promise.resolve(fn(...args)); } } } }