Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 5x 8x | type EventObject<T = unknown> = {
id: string;
action: (args: T) => void;
};
export class EventEmitter<EventsMap = unknown> {
private events = {} as Record<keyof EventsMap, EventObject<any>[]>;
on<MapKey extends keyof EventsMap>(type: MapKey, event: EventObject<EventsMap[MapKey]>) {
if (this.events[type]) {
this.events[type].push(event);
} else {
this.events[type] = [event];
}
}
off<MapKey extends keyof EventsMap>(type: MapKey, eventId: string) {
if (this.events[type]) {
this.events[type] = this.events[type].filter(({id}) => id !== eventId);
}
}
dispatch<MapKey extends keyof EventsMap>(type: MapKey, args: EventsMap[MapKey]) {
Iif (this.events[type]) {
this.events[type].forEach(({action}) => {
action(args);
});
}
}
}
|