34 lines
950 B
TypeScript
34 lines
950 B
TypeScript
type EventCallback = (...args: any[]) => void;
|
|
|
|
class EventEmitter {
|
|
private events: Map<string, EventCallback[]> = new Map();
|
|
|
|
on(event: string, callback: EventCallback) {
|
|
if (!this.events.has(event)) {
|
|
this.events.set(event, []);
|
|
}
|
|
this.events.get(event)!.push(callback);
|
|
}
|
|
|
|
off(event: string, callback: EventCallback) {
|
|
if (!this.events.has(event)) return;
|
|
const callbacks = this.events.get(event)!;
|
|
const index = callbacks.indexOf(callback);
|
|
if (index > -1) {
|
|
callbacks.splice(index, 1);
|
|
}
|
|
}
|
|
|
|
emit(event: string, ...args: any[]) {
|
|
if (!this.events.has(event)) return;
|
|
this.events.get(event)!.forEach(callback => callback(...args));
|
|
}
|
|
}
|
|
|
|
export const eventEmitter = new EventEmitter();
|
|
|
|
export const EVENT_TYPES = {
|
|
USER_INFO_UPDATED: 'USER_INFO_UPDATED',
|
|
MATERIAL_IMPORTED: 'MATERIAL_IMPORTED'
|
|
} as const;
|