import { createLogger } from '@snowluma/common/logger'; import type { QQEventVariant } from './events'; type EventKind = QQEventVariant['kind']; type EventOf = Extract; export type EventHandler = (event: EventOf) => void | Promise; export type AnyEventHandler = (event: QQEventVariant) => void | Promise; export interface EventBusOptions { /** Logger label for handler failures. Defaults to `Bridge.Events`. */ loggerName?: string; /** Override the default error reporter (e.g. for tests). */ onError?: (kind: EventKind | '*', err: unknown) => void; } const DEFAULT_LOGGER = createLogger('Bridge.Events'); export class BridgeEventBus { // Per-kind subscribers are stored as `Set` so the container // doesn't carry a contravariant function type that fights TS variance // rules. The runtime invariant is straightforward: handlers stored under // key `K` only ever see `EventOf` events, which is what the single // narrowing cast at the dispatch site below relies on. private readonly handlers = new Map>(); private readonly anyHandlers = new Set(); private readonly onError: (kind: EventKind | '*', err: unknown) => void; constructor(opts: EventBusOptions = {}) { if (opts.onError) { this.onError = opts.onError; } else { const log = opts.loggerName ? createLogger(opts.loggerName) : DEFAULT_LOGGER; this.onError = (kind, err) => { log.error( 'event handler error [%s]: %s', kind, err instanceof Error ? (err.stack ?? err.message) : String(err), ); }; } } /** Subscribe to one kind. Returns an unsubscribe function. */ on(kind: K, handler: EventHandler): () => void { let set = this.handlers.get(kind); if (!set) { set = new Set(); this.handlers.set(kind, set); } set.add(handler); return () => { this.handlers.get(kind)?.delete(handler); }; } /** Subscribe to every kind (low-volume callers like loggers). */ onAny(handler: AnyEventHandler): () => void { this.anyHandlers.add(handler); return () => { this.anyHandlers.delete(handler); }; } off(kind: K, handler: EventHandler): void { this.handlers.get(kind)?.delete(handler); } has(kind: EventKind): boolean { return (this.handlers.get(kind)?.size ?? 0) + this.anyHandlers.size > 0; } /** Drop every subscriber. Mostly useful for tests. */ clear(): void { this.handlers.clear(); this.anyHandlers.clear(); } /** * Dispatch an event in parallel to every subscriber for its kind plus every * onAny handler. Rejections are isolated per-handler — one bad subscriber * never blocks the others, and the returned promise resolves once every * subscriber settles. */ async emit(event: EventOf): Promise { const kindHandlers = this.handlers.get(event.kind); if ((!kindHandlers || kindHandlers.size === 0) && this.anyHandlers.size === 0) { return; } const tasks: Promise[] = []; if (kindHandlers) { for (const handler of kindHandlers) { // Single narrowing cast — the only place we re-assert that handlers // stored under `event.kind` accept `EventOf` events. const typed = handler as EventHandler; tasks.push(invoke(this.onError, event.kind, () => typed(event))); } } for (const handler of this.anyHandlers) { tasks.push(invoke(this.onError, '*', () => handler(event))); } await Promise.allSettled(tasks); } } async function invoke( onError: (kind: EventKind | '*', err: unknown) => void, kind: EventKind | '*', fn: () => unknown, ): Promise { try { const ret = fn(); if (ret && typeof (ret as Promise).then === 'function') { await ret; } } catch (err) { onError(kind, err); } }