/**
 * Type-safe event bus for stream lifecycle events.
 *
 * Observers subscribe to specific events and receive typed arguments.
 * Handlers are called in parallel, and errors in one handler don't affect others.
 *
 * @example
 * ```typescript
 * const events = new StreamEventBus();
 *
 * // Subscribe to events
 * const unsubscribe = events.on("chunk", (chunk, session) => {
 *   console.log(`Received chunk: ${chunk.type}`);
 * });
 *
 * // Emit events
 * await events.emit("chunk", myChunk, mySession);
 *
 * // Unsubscribe when done
 * unsubscribe();
 * ```
 */
export class StreamEventBus {
    listeners = new Map();
    on(event, handler) {
        let handlers = this.listeners.get(event);
        if (!handlers) {
            handlers = new Set();
            this.listeners.set(event, handlers);
        }
        handlers.add(handler);
        return () => {
            handlers?.delete(handler);
        };
    }
    async emit(event, ...args) {
        const handlers = this.listeners.get(event);
        if (!handlers || handlers.size === 0)
            return;
        const results = await Promise.allSettled([...handlers].map(async (handler) => handler(...args)));
        for (const result of results) {
            if (result.status === "rejected") {
                console.error(`Observer error on "${event}" event:`, result.reason);
            }
        }
    }
    clear() {
        this.listeners.clear();
    }
}
//# sourceMappingURL=event-bus.js.map