-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
23 lines (23 loc) · 832 Bytes
/
Copy pathindex.ts
File metadata and controls
23 lines (23 loc) · 832 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
type EventCallback<T = unknown> = (...payload: T[]) => void;
type KeyOf<T> = keyof T & string;
export function emitter<Events extends Record<string, any> = any, Keys extends KeyOf<Events> = KeyOf<Events>>() {
const map = new Map<Keys, EventCallback<Events[Keys]>[]>();
return {
on<K extends Keys = Keys>(name: K, cb: EventCallback<Events[K]>) {
const cbs = map.get(name) ?? [];
(cbs as any[]).push(cb);
map.set(name, cbs);
},
off(name: Keys | "*", cb: EventCallback<Events[Keys]> | undefined = undefined) {
if (name !== "*") {
const cbs = map.get(name as Keys);
cbs && cbs.splice(cbs?.indexOf(cb!!!) >>> 0, 1);
}
map.clear();
},
dispatch(name: Keys, ...data: Events[Keys][]) {
const cbs = map.get(name) ?? [];
for (let c of cbs!!! as EventCallback<Events[Keys]>[]) c(...data);
},
};
}