fine-true
Version:
A small and beautiful Vue3 version of the UI Library
30 lines (27 loc) • 746 B
text/typescript
type EmitterEventsMap = Map<string, ((...args: any[]) => any)[]>;
class Emitter {
eventsMap: EmitterEventsMap;
constructor() {
this.eventsMap = new Map();
}
emit(action: string, ...args: any[]) {
if (!action) return;
const methods = this.eventsMap.get(action);
if (!methods) return;
methods.forEach((method) => {
method.call(this, ...args);
});
}
on(action: string, method: (...args: any[]) => any) {
if (!action || !method) return;
let methods = this.eventsMap.get(action);
if (!methods) {
methods = [];
this.eventsMap.set(action, methods);
}
if (!methods.includes(method)) {
methods.push(method);
}
}
}
export default Emitter;