2024-10-14 13:52:03 -04:00
|
|
|
export type TypedListener = (...args: any[]) => any;
|
|
|
|
export type TypedEvents = {[k in string | symbol]: TypedListener} & {'*': (event: string, ...args: any[]) => any};
|
2024-02-07 01:33:07 -05:00
|
|
|
|
2024-09-30 21:43:13 -04:00
|
|
|
export type NamespaceEvents<Namespace extends string, Events extends TypedEvents> = {
|
|
|
|
[K in keyof Events as `${Namespace}:${Extract<K, string>}`]: Events[K];
|
|
|
|
};
|
|
|
|
|
2024-02-23 21:20:44 -05:00
|
|
|
export class TypedEmitter<T extends TypedEvents = TypedEvents> {
|
2024-10-14 13:52:03 -04:00
|
|
|
private static listeners: {[key: string]: TypedListener[]} = {};
|
2024-02-28 19:19:24 -05:00
|
|
|
|
2024-10-14 13:52:03 -04:00
|
|
|
private listeners: { [key in keyof T]?: TypedListener[] } = {};
|
2024-02-28 19:19:24 -05:00
|
|
|
|
|
|
|
static emit(event: any, ...args: any[]) {
|
|
|
|
(this.listeners['*'] || []).forEach(l => l(event, ...args));
|
|
|
|
(this.listeners[event.toString()] || []).forEach(l => l(...args));
|
|
|
|
};
|
|
|
|
|
2024-10-14 13:52:03 -04:00
|
|
|
static off(event: any, listener: TypedListener) {
|
2024-02-28 19:19:24 -05:00
|
|
|
const e = event.toString();
|
|
|
|
this.listeners[e] = (this.listeners[e] || []).filter(l => l === listener);
|
|
|
|
}
|
|
|
|
|
2024-10-14 13:52:03 -04:00
|
|
|
static on(event: any, listener: TypedListener) {
|
2024-02-28 19:19:24 -05:00
|
|
|
const e = event.toString();
|
|
|
|
if(!this.listeners[e]) this.listeners[e] = [];
|
|
|
|
this.listeners[e]?.push(listener);
|
|
|
|
return () => this.off(event, listener);
|
|
|
|
}
|
|
|
|
|
2024-10-14 13:52:03 -04:00
|
|
|
static once(event: any, listener?: TypedListener): Promise<any> {
|
2024-02-28 19:19:24 -05:00
|
|
|
return new Promise(res => {
|
|
|
|
const unsubscribe = this.on(event, <any>((...args: any) => {
|
|
|
|
res(args.length == 1 ? args[0] : args);
|
|
|
|
if(listener) listener(...args);
|
|
|
|
unsubscribe();
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
2024-02-07 01:33:07 -05:00
|
|
|
|
2024-02-23 21:20:44 -05:00
|
|
|
emit<K extends keyof T>(event: K, ...args: Parameters<T[K]>) {
|
|
|
|
(this.listeners['*'] || []).forEach(l => l(event, ...args));
|
|
|
|
(this.listeners[event] || []).forEach(l => l(...args));
|
|
|
|
};
|
2024-02-07 01:33:07 -05:00
|
|
|
|
2024-02-28 19:19:24 -05:00
|
|
|
off<K extends keyof T = string>(event: K, listener: T[K]) {
|
2024-02-23 21:20:44 -05:00
|
|
|
this.listeners[event] = (this.listeners[event] || []).filter(l => l === listener);
|
2024-02-07 01:33:07 -05:00
|
|
|
}
|
|
|
|
|
2024-02-23 21:20:44 -05:00
|
|
|
on<K extends keyof T = string>(event: K, listener: T[K]) {
|
|
|
|
if(!this.listeners[event]) this.listeners[event] = [];
|
|
|
|
this.listeners[event]?.push(listener);
|
|
|
|
return () => this.off(event, listener);
|
2024-02-07 01:33:07 -05:00
|
|
|
}
|
|
|
|
|
2024-02-23 21:20:44 -05:00
|
|
|
once<K extends keyof T = string>(event: K, listener?: T[K]): Promise<any> {
|
|
|
|
return new Promise(res => {
|
|
|
|
const unsubscribe = this.on(event, <any>((...args: any) => {
|
|
|
|
res(args.length == 1 ? args[0] : args);
|
|
|
|
if(listener) listener(...args);
|
|
|
|
unsubscribe();
|
|
|
|
}));
|
2024-02-07 01:33:07 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|