1
0
silverbullet/plugos/event.ts

21 lines
514 B
TypeScript
Raw Normal View History

export abstract class EventEmitter<HandlerT> {
private handlers: Partial<HandlerT>[] = [];
on(handlers: Partial<HandlerT>) {
this.handlers.push(handlers);
}
off(handlers: Partial<HandlerT>) {
this.handlers = this.handlers.filter((h) => h !== handlers);
}
async emit(eventName: keyof HandlerT, ...args: any[]): Promise<void> {
for (const handler of this.handlers) {
const fn: any = handler[eventName];
if (fn) {
await Promise.resolve(fn(...args));
}
}
}
}