Added static events to the emitter & added it to the logger
This commit is contained in:
parent
74c89c3ed4
commit
d4996201cf
@ -2,17 +2,44 @@ export type Listener = (...args: any[]) => any;
|
|||||||
export type TypedEvents = {[k in string | symbol]: Listener} & {'*': (event: string, ...args: any[]) => any};
|
export type TypedEvents = {[k in string | symbol]: Listener} & {'*': (event: string, ...args: any[]) => any};
|
||||||
|
|
||||||
export class TypedEmitter<T extends TypedEvents = TypedEvents> {
|
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]>) {
|
emit<K extends keyof T>(event: K, ...args: Parameters<T[K]>) {
|
||||||
(this.listeners['*'] || []).forEach(l => l(event, ...args));
|
(this.listeners['*'] || []).forEach(l => l(event, ...args));
|
||||||
(this.listeners[event] || []).forEach(l => l(...args));
|
(this.listeners[event] || []).forEach(l => l(...args));
|
||||||
};
|
};
|
||||||
|
|
||||||
off<K extends keyof T = string>(event: K, listener: T[K]): this {
|
off<K extends keyof T = string>(event: K, listener: T[K]) {
|
||||||
console.log('cleared');
|
|
||||||
this.listeners[event] = (this.listeners[event] || []).filter(l => l === listener);
|
this.listeners[event] = (this.listeners[event] || []).filter(l => l === listener);
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
on<K extends keyof T = string>(event: K, listener: T[K]) {
|
on<K extends keyof T = string>(event: K, listener: T[K]) {
|
||||||
|
@ -68,35 +68,35 @@ export class Logger extends TypedEmitter<LoggerEvents> {
|
|||||||
|
|
||||||
debug(...args: string[]) {
|
debug(...args: string[]) {
|
||||||
if(LOG_LEVEL.VERBOSE >= Logger.LOG_LEVEL) {
|
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);
|
console.debug(CliForeground.LIGHT_GREY + this.format(...args) + CliEffects.CLEAR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
error(...args: string[]) {
|
error(...args: string[]) {
|
||||||
if(LOG_LEVEL.ERROR >= Logger.LOG_LEVEL) {
|
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);
|
console.error(CliForeground.RED + this.format(...args) + CliEffects.CLEAR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
info(...args: string[]) {
|
info(...args: string[]) {
|
||||||
if(LOG_LEVEL.INFO >= Logger.LOG_LEVEL) {
|
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);
|
console.info(CliForeground.CYAN + this.format(...args) + CliEffects.CLEAR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log(...args: string[]) {
|
log(...args: string[]) {
|
||||||
if(LOG_LEVEL.INFO >= Logger.LOG_LEVEL) {
|
if(LOG_LEVEL.INFO >= Logger.LOG_LEVEL) {
|
||||||
this.emit('INFO', ...args);
|
Logger.emit(LOG_LEVEL.INFO, ...args);
|
||||||
console.log(CliEffects.CLEAR + this.format(...args));
|
console.log(CliEffects.CLEAR + this.format(...args));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
warn(...args: string[]) {
|
warn(...args: string[]) {
|
||||||
if(LOG_LEVEL.WARN >= Logger.LOG_LEVEL) {
|
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);
|
console.warn(CliForeground.YELLOW + this.format(...args) + CliEffects.CLEAR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user