2022-03-20 08:56:28 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-10-10 12:50:21 +00:00
|
|
|
async emit(eventName: keyof HandlerT, ...args: any[]): Promise<void> {
|
|
|
|
for (const handler of this.handlers) {
|
|
|
|
const fn: any = handler[eventName];
|
2022-03-20 08:56:28 +00:00
|
|
|
if (fn) {
|
2022-10-10 12:50:21 +00:00
|
|
|
await Promise.resolve(fn(...args));
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|