import { EventHandlerFn, IEventHandler, BaseEventBusOptions, EventBusMiddleware, IDomainEvent, IEventBus } from '@vytches/ddd-contracts'; /** * @internal */ export declare const CUSTOM_MIDDLEWARE_SYMBOL: unique symbol; /** * Options for {@link BaseEventBus.publishMany}. * * @public * @since 0.26.0 */ export interface PublishManyOptions { /** * When `true`, events are published one after another in array order — * event N+1 starts only after every handler of event N has completed. * Defaults to `false` (parallel `Promise.all`, no cross-event ordering). */ sequential?: boolean; } /** * Concrete in-process event bus with middleware pipeline and per-event * handler caps. Subclasses (e.g. {@link UnifiedEventBus}) extend this with * multi-bus routing, integration-event shimming, or persistence hooks. * * Default behavior: * * - Synchronous fan-out — all subscribers for an event type run in * parallel via `Promise.all` (use middleware to enforce ordering or * serialization). * - Run-all error semantics — a failing handler never prevents the * remaining handlers from running. Failures are collected during * fan-out and surfaced only after all handlers complete: routed to * `options.onError` when configured (publish resolves), otherwise * thrown as a single {@link AggregatedEventHandlerError}. * - Hard cap — `MAX_HANDLERS_PER_EVENT = 100` per event name to catch * leaks (forgetting to `unsubscribe`). Override the static in a * subclass for projection-heavy apps. * - Pluggable middleware — wrap publish with logging, retry, dedupe, * metrics. Configured via `options.middlewares`. * * @example Basic in-memory bus * ```typescript * import { BaseEventBus } from '@vytches/ddd-events'; * * class InMemoryBus extends BaseEventBus {} // expose protected as needed * const bus = new InMemoryBus({ enableLogging: true }); * * bus.subscribe('OrderPaid', async event => { * console.log('paid', event.payload); * }); * await bus.publish({ eventName: 'OrderPaid', payload: { id: 'o-1' } }); * ``` * * @example With error handler and middleware * ```typescript * const bus = new UnifiedEventBus({ * enableLogging: true, * onError: (err, type) => Sentry.captureException(err, { extra: { type } }), * middlewares: [retryMiddleware(3), metricsMiddleware()], * }); * ``` * * @public * @stable * @since 0.22.0 */ export declare abstract class BaseEventBus extends IEventBus { static readonly MAX_HANDLERS_PER_EVENT: number; /** * Map of event types to their handlers */ protected handlers: Map | IEventHandler>>; /** * Configuration options for this event bus */ protected options: BaseEventBusOptions; protected publishPipeline: (event: TEvent) => Promise; /** * Creates a new event bus with the specified options */ constructor(options?: BaseEventBusOptions); publish(event: TEvent): Promise; /** * Publish multiple events. * * **WARNING — no cross-event ordering by default.** Events are published * in parallel via `Promise.all`: handlers of `events[1]` may run before * handlers of `events[0]` have finished. For an aggregate's event batch * where projections depend on order (e.g. `OrderCreated` before * `ItemAdded`), pass `{ sequential: true }` to publish events strictly * one after another in array order. * * Error semantics follow {@link BaseEventBus.publish}: each event's * handler failures are routed to `onError` or thrown as an * {@link AggregatedEventHandlerError}. In sequential mode a throwing * event stops the remaining events from being published. */ publishMany(events: TEvent[], options?: PublishManyOptions): Promise; use(middleware: EventBusMiddleware): this; protected buildPublishPipeline(): (event: TEvent) => Promise; /** * Subscribe a function to handle events of a specific type */ subscribe(eventName: string | (new (...args: unknown[]) => T), handler: (event: T) => Promise | void): void; /** * Register a class-based handler for events of a specific type */ registerHandler(eventName: string | (new (...args: unknown[]) => T), handler: { handle(event: T): Promise | void; }): void; /** * Unsubscribe a handler from events of a specific type */ unsubscribe(eventName: string | (new (...args: unknown[]) => T), handler: ((event: T) => Promise | void) | { handle(event: T): Promise | void; }): void; /** * Logs a message if logging is enabled */ protected log(message: string): void; /** * Enforces the per-event handler cap, resolving the static through * `this.constructor` so subclass overrides of `MAX_HANDLERS_PER_EVENT` * take effect. */ protected assertHandlerCapacity(currentCount: number, eventName: string): void; /** * Surfaces handler failures collected during a completed fan-out. * * - With `options.onError` configured: each error is routed to the hook * and publish resolves (current contract — the hook owns the errors). * - Without: each error is logged and a single * {@link AggregatedEventHandlerError} carrying all failures is thrown. */ protected handleErrors(errors: readonly Error[], eventName: string): void; /** * Extracts the event name from a constructor or string */ protected getEventName(eventName: string | (new (...args: unknown[]) => T)): string; /** * Extracts the event type name from an event object */ protected getEventTypeName(event: TEvent): string; /** * Gets the registered handlers for a specific event type * Useful for testing and debugging */ getHandlers(eventName: string | (new (...args: unknown[]) => TEvent)): Set | IEventHandler> | undefined; /** * Gets all registered event types * Useful for inspection and debugging */ getRegisteredEventTypes(): string[]; /** * Clears all registered handlers * Useful for testing */ clearHandlers(): void; }