/**
 * Lightweight pub/sub event bus for extension-to-extension communication.
 *
 * Wraps Node's EventEmitter with error-safe handlers (errors are caught and
 * logged rather than crashing the process) and returns unsubscribe functions.
 */
import { EventEmitter } from 'node:events';
/** Create a new event bus instance with error-safe handler wrapping. */
export function createEventBus() {
    const emitter = new EventEmitter();
    return {
        emit: (channel, data) => {
            emitter.emit(channel, data);
        },
        on: (channel, handler) => {
            const safeHandler = async (data) => {
                try {
                    await handler(data);
                }
                catch (err) {
                    console.error(`Event handler error (${channel}):`, err);
                }
            };
            emitter.on(channel, safeHandler);
            return () => emitter.off(channel, safeHandler);
        },
        clear: () => {
            emitter.removeAllListeners();
        }
    };
}
//# sourceMappingURL=event-bus.js.map