1
0
silverbullet/plugos/event.ts
Zef Hemel 561aa6891f
Migrate to Deno (#86)
Big bang migration to Deno 🤯
2022-10-10 14:50:21 +02:00

21 lines
514 B
TypeScript

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));
}
}
}
}