Added static events to the emitter & added it to the logger
All checks were successful
Build / Build NPM Project (push) Successful in 33s
Build / Tag Version (push) Successful in 10s
Build / Publish (push) Successful in 16s

This commit is contained in:
Zakary Timson 2024-02-28 19:19:24 -05:00
parent 74c89c3ed4
commit d4996201cf
2 changed files with 36 additions and 9 deletions

View File

@ -2,17 +2,44 @@ 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[] } = {};
private static listeners: {[key: string]: Listener[]} = {};
private listeners: { [key in keyof T]?: Listener[] } = {};
static emit(event: any, ...args: any[]) {
(this.listeners['*'] || []).forEach(l => l(event, ...args));
(this.listeners[event.toString()] || []).forEach(l => l(...args));
};
static off(event: any, listener: Listener) {
const e = event.toString();
this.listeners[e] = (this.listeners[e] || []).filter(l => l === listener);
}
static on(event: any, listener: Listener) {
const e = event.toString();
if(!this.listeners[e]) this.listeners[e] = [];
this.listeners[e]?.push(listener);
return () => this.off(event, listener);
}
static once(event: any, listener?: Listener): 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();
}));
});
}
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');
off<K extends keyof T = string>(event: K, listener: T[K]) {
this.listeners[event] = (this.listeners[event] || []).filter(l => l === listener);
return this;
}
on<K extends keyof T = string>(event: K, listener: T[K]) {

View File

@ -68,35 +68,35 @@ export class Logger extends TypedEmitter<LoggerEvents> {
debug(...args: string[]) {
if(LOG_LEVEL.VERBOSE >= Logger.LOG_LEVEL) {
this.emit('VERBOSE', ...args);
Logger.emit(LOG_LEVEL.VERBOSE, ...args);
console.debug(CliForeground.LIGHT_GREY + this.format(...args) + CliEffects.CLEAR);
}
}
error(...args: string[]) {
if(LOG_LEVEL.ERROR >= Logger.LOG_LEVEL) {
this.emit('ERROR', ...args);
Logger.emit(LOG_LEVEL.ERROR, ...args);
console.error(CliForeground.RED + this.format(...args) + CliEffects.CLEAR);
}
}
info(...args: string[]) {
if(LOG_LEVEL.INFO >= Logger.LOG_LEVEL) {
this.emit('INFO', ...args);
Logger.emit(LOG_LEVEL.INFO, ...args);
console.info(CliForeground.CYAN + this.format(...args) + CliEffects.CLEAR);
}
}
log(...args: string[]) {
if(LOG_LEVEL.INFO >= Logger.LOG_LEVEL) {
this.emit('INFO', ...args);
Logger.emit(LOG_LEVEL.INFO, ...args);
console.log(CliEffects.CLEAR + this.format(...args));
}
}
warn(...args: string[]) {
if(LOG_LEVEL.WARN >= Logger.LOG_LEVEL) {
this.emit('WARN', ...args);
Logger.emit(LOG_LEVEL.WARN, ...args);
console.warn(CliForeground.YELLOW + this.format(...args) + CliEffects.CLEAR);
}
}