* Subscribe to an event * @param event - Event name to listen for * @param callback - Function to call when event is emitted * @returns Unsubscribe function
(
event: T,
callback: (event: TEvents[T]) => void,
)
| 15 | * @returns Unsubscribe function |
| 16 | */ |
| 17 | on<T extends keyof TEvents>( |
| 18 | event: T, |
| 19 | callback: (event: TEvents[T]) => void, |
| 20 | ): () => void { |
| 21 | if (!this.listeners.has(event)) { |
| 22 | this.listeners.set(event, new Set()) |
| 23 | } |
| 24 | this.listeners.get(event)!.add(callback as (event: any) => void) |
| 25 | |
| 26 | return () => { |
| 27 | this.listeners.get(event)?.delete(callback as (event: any) => void) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Subscribe to an event once (automatically unsubscribes after first emission) |