import type DomainMessage from "../../Domain/Event/DomainMessage"; import type EventListener from "../EventBus/EventListener"; import type EventSubscriber from "../EventBus/EventSubscriber"; import type { DomainEventConstructor } from "../EventBus/IEventBus"; import type IIdempotencyStore from "./IIdempotencyStore"; import EventBus from "../EventBus/EventBus"; /** * Options for configuring the IdempotentEventBus. */ export interface IdempotentEventBusOptions { /** * Time-to-live for idempotency keys in milliseconds. * After this time, the same event can be processed again. * Default: undefined (keys persist indefinitely) */ ttl?: number; /** * Callback invoked when a duplicate event is detected. * Useful for logging or metrics. */ onDuplicate?: (message: DomainMessage) => void; } /** * An EventBus wrapper that provides idempotent event publishing. * Prevents duplicate event processing at the bus level. * * This is useful when you want all handlers to receive idempotency protection * without wrapping each handler individually. */ export default class IdempotentEventBus extends EventBus { private readonly idempotencyStore; private readonly options; constructor(idempotencyStore: IIdempotencyStore, options?: IdempotentEventBusOptions); publish(message: DomainMessage): Promise; /** * Attach a subscriber to an event type. * Overridden to return the correct type. */ attach(event: DomainEventConstructor, subscriber: EventSubscriber): IdempotentEventBus; /** * Add a global event listener. * Overridden to return the correct type. */ addListener(listener: EventListener): IdempotentEventBus; }