utils/src/emitter.ts
ztimson e4c7dea9a5
All checks were successful
Build / Build NPM Project (push) Successful in 31s
Build / Tag Version (push) Successful in 7s
Build / Publish (push) Successful in 12s
Added typed emitter
2024-02-23 21:20:44 -05:00

34 lines
1.2 KiB
TypeScript

export type Listener = (...args: any[]) => any;
export type TypedEvents = {[k in string | symbol]: Listener} & {'*': (event: string, ...args: any[]) => any};
export class TypedEmitter<T extends TypedEvents = TypedEvents> {
private listeners: { [key in keyof T]?: Function[] } = {};
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));
};
off<K extends keyof T = string>(event: K, listener: T[K]): this {
console.log('cleared');
this.listeners[event] = (this.listeners[event] || []).filter(l => l === listener);
return this;
}
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);
}
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();
}));
});
}
}