import { EventEmitter } from 'node:events'; import { A as AbstractEventDispatcher, E as EventDispatcherInterface, a as EventListener } from './AbstractEventDispatcher-CdpR1_nX.mjs'; /** * Node.js-optimized EventDispatcher. * * Philosophy: * ───────────────────────────────────────────────────────────── * dispatch() is the single entry point. It does two things in order: * * 1. Iterates the internal Map → calls subscribers/listeners registered * via addListener() / addSubscriber(), in priority order. * * 2. Fires emitter.emit() on the native EventEmitter (dispatchNative) → * any code that used emitter.on() / emitter.addListener() DIRECTLY * (without going through this dispatcher) will also receive the event * automatically. * * This means the developer never needs to call dispatchNative() manually — * a single dispatcher.dispatch() notifies both worlds. * * No double-call: addListener() stores listeners in the Map only, NOT on * the native EventEmitter. The emitter is reserved for external listeners. * * Use dispatchAsync() when subscribers perform async work and results must * be read from the event object after dispatch. * * @author AGBOKOUDJO Franck */ declare class NodeEventDispatcher extends AbstractEventDispatcher implements EventDispatcherInterface { private readonly emitter; private readonly listeners; private readonly sorted; constructor(emitter?: EventEmitter); /** * Dispatches an event synchronously. * * Step 1 — Internal Map loop (priority order): * Calls all listeners registered via addListener() / addSubscriber(). * stopPropagation() is honoured between each listener. * Async listeners are fire-and-forget (errors logged, not thrown). * Use dispatchAsync() if you need to await async listeners. * * Step 2 — Native EventEmitter (dispatchNative): * Fires emitter.emit() so that any external listener attached via * emitter.on() / getEmitter().on() also receives the event — * without ever registering through this dispatcher. */ dispatch(event: T, eventName?: string | null): T; /** * Dispatches an event and awaits all listeners sequentially (priority order). * * Step 1 — awaits each async listener in the internal Map. * Step 2 — fires emitter.emit() for external listeners (synchronous Node.js * EventEmitter — not awaited). * * Use this when subscribers perform async work (HTTP, DB, file I/O…) and * you need to read results from the event object after dispatch. * * @example * const event = new InitializingUploadEvent(options); * await dispatcher.dispatchAsync(event, HttpFileUploaderEvents.INITIALIZE_UPLOAD); * const mediaId = event.mediaId; // safely populated by the subscriber */ dispatchAsync(event: T, eventName?: string | null): Promise; /** * Registers a listener in the internal Map only. * * NOT attached to the native EventEmitter — that emitter is reserved for * external listeners (emitter.on(), etc.) to avoid double-call. */ addListener(eventName: string, listener: EventListener, priority?: number): void; /** * Removes a listener from the internal Map only. * Has no effect on listeners attached directly via emitter.on(). * Does NOT call removeAllListeners() — external emitter listeners are untouched. */ removeListener(eventName: string, listener: EventListener): void; getListeners(eventName?: string | null): EventListener[] | Map; getListenerPriority(eventName: string, listener: EventListener): number | null; hasListeners(eventName?: string | null): boolean; /** * Returns the underlying EventEmitter. * * Pass a shared EventEmitter in the constructor to integrate with other * modules — any emitter.on() call on that emitter will automatically * receive events dispatched via dispatch(). * * @example * const sharedEmitter = new EventEmitter(); * const dispatcher = new NodeEventDispatcher(sharedEmitter); * * // Somewhere else in your app — no addListener() needed * sharedEmitter.on('file.processed', (event) => { * console.log(event.fileName); * }); * * // This notifies both your subscribers AND the emitter listener * dispatcher.dispatch(new FileProcessedEvent('video.mp4'), 'file.processed'); */ getEmitter(): EventEmitter; /** * Set the maximum number of listeners before warning. * Default is 100. */ setMaxListeners(n: number): void; /** * Get the current max listeners limit. */ getMaxListeners(): number; private sortListeners; /** * Fires emitter.emit() on the native EventEmitter. * * Called automatically by dispatch() and dispatchAsync(). * Can also be called manually if you want to notify native listeners * without going through the internal Map. * * External listeners receive the original typed event object directly. * * @example * // External code — no addListener() needed, works automatically * dispatcher.getEmitter().on('file.processed', (event) => { * console.log(event.fileName); * }); * * dispatcher.dispatch(new FileProcessedEvent('video.mp4'), 'file.processed'); * // ↑ emitter listener receives the event automatically via dispatchNative */ protected dispatchNative(event: T, eventName?: string | null): void; } export { NodeEventDispatcher };