import { EventMiddleware, IDomainEvent, IEventProcessor, IAggregateWithEvents, IEventBus, IEnhancedEventDispatcher } from '@vytches/ddd-contracts'; /** * Repository-side event dispatcher — orchestrates middleware pipeline, * specialized processors, and the underlying `IEventBus` to flush an * aggregate's pending events safely. * * Three responsibilities: * * 1. **Drain aggregate** — `dispatchEventsForAggregate(aggregate)` reads * `getDomainEvents()`, runs them through the pipeline, and calls * `aggregate.commit()` only on success (failed dispatch leaves events * pending, so the next `save()` retries). * 2. **Middleware pipeline** — `use(middleware)` registers cross-cutting * concerns (correlation-id propagation, idempotency tags, retry, * metrics) that run *before* the event hits the bus. * 3. **Specialized processors** — `registerProcessor(p)` registers * domain-specific handlers that run *after* publish for events * matching `p.canProcess(event)`. Useful for projections that need * direct access to the event without subscribing through the bus. * * Use this in repository implementations of `save()` rather than calling * `eventBus.publishMany()` directly — middleware + processors give you * a uniform place to add observability and side effects without each * repository duplicating the logic. * * @example Repository wiring * ```typescript * import { UniversalEventDispatcher } from '@vytches/ddd-events'; * * class OrderRepository { * constructor(private dispatcher: UniversalEventDispatcher) {} * * async save(order: Order): Promise { * await this.persistTo(this.orm, order); * await this.dispatcher.dispatchEventsForAggregate(order); * } * } * * const dispatcher = new UniversalEventDispatcher(eventBus) * .use(correlationIdMiddleware) * .use(metricsMiddleware) * .registerProcessor(new ReadModelUpdater()); * ``` * * @public * @stable * @since 0.22.0 */ export declare class UniversalEventDispatcher extends IEnhancedEventDispatcher { private middlewares; private processors; private eventBus; /** * Create a new universal event dispatcher * @param eventBus Optional event bus instance (creates new UnifiedEventBus if not provided) */ constructor(eventBus?: IEventBus); /** * Add middleware to the event processing pipeline * Middleware runs before events are published to the UnifiedEventBus */ use(middleware: EventMiddleware): this; /** * Register an event processor for specialized event handling * Processors run after events are published but before aggregate commit */ registerProcessor(processor: IEventProcessor): this; /** * Dispatch all events from an aggregate and clear them * This is the main method used by IBaseRepository.save() */ dispatchEventsForAggregate(aggregate: IAggregateWithEvents): Promise; /** * Dispatch a single event through middleware pipeline */ dispatchEvent(event: IDomainEvent): Promise; /** * Dispatch multiple events */ dispatchEvents(...events: IDomainEvent[]): Promise; /** * Get direct access to event bus for advanced scenarios */ getEventBus(): IEventBus; /** * Get direct access to UnifiedEventBus for advanced scenarios (backward compatibility) * @deprecated Use getEventBus() instead */ getUnifiedEventBus(): IEventBus; /** * Replace the event bus (useful for testing) */ setEventBus(bus: IEventBus): void; /** * Replace the unified event bus (backward compatibility) * @deprecated Use setEventBus() instead */ setUnifiedEventBus(bus: IEventBus): void; /** * Build the middleware pipeline for event processing */ private buildPipeline; /** * Process an event through all registered processors */ private processEvent; }