dralkh_spaceforge/utils/event-emitter.ts
dralkh 4cbc58b5fa refactor: consolidate styles and improve UI components
- Split styles.css into modular component files
- Update UI components to use new modular CSS structure
- Remove deprecated main.js file
- Add sleep utility function
- Enhance calendar and modal components
- Update navigation controller and event handling
2025-10-13 10:45:31 +03:00

66 lines
1.5 KiB
TypeScript

/**
* Simple event emitter implementation
*/
export class EventEmitter {
/**
* Event listeners by event name
*/
private listeners: Record<string, Function[]> = {};
/**
* Register a listener for an event
*
* @param event Event name
* @param callback Function to call when event is emitted
*/
on(event: string, callback: Function): void {
if (!this.listeners[event]) {
this.listeners[event] = [];
}
this.listeners[event].push(callback);
}
/**
* Emit an event
*
* @param event Event name
* @param args Arguments to pass to listeners
*/
emit(event: string, ...args: unknown[]): void {
if (!this.listeners[event]) {
return;
}
for (const callback of this.listeners[event]) {
callback(...args);
}
}
/**
* Remove a listener for an event
*
* @param event Event name
* @param callback Function to remove
*/
off(event: string, callback: Function): void {
if (!this.listeners[event]) {
return;
}
this.listeners[event] = this.listeners[event].filter(cb => cb !== callback);
}
/**
* Remove all listeners for an event
*
* @param event Event name
*/
removeAllListeners(event?: string): void {
if (event) {
delete this.listeners[event];
} else {
this.listeners = {};
}
}
}