UNPKG

cqrs-eda

Version:

Lightweight CQRS and Event-Driven Architecture library using TypeScript decorators, handlers and typings. Perfect for scalable event-driven apps.

37 lines 1.39 kB
import { Constructor, IObserverHandler } from "../types/base"; /** * Handles the registration and execution of observers for events. * Supports instantiation of observer classes, optionally via a custom factory. * * @template O - A mapping of event names to their payload types. */ export declare class ObserverHandler<O extends Record<string, any>> implements IObserverHandler<O> { private factory?; private observers; /** * @param factory Optional factory function to instantiate observer classes. * This can be used to: * - Integrate with a dependency injection library, e.g., tsyringe or inversify. * - Customize instance creation with initialization logic. * * Example: * ```ts * const handler = new ObserverHandler((cls) => container.resolve(cls)); * ``` */ constructor(factory?: ((cls: Constructor) => any) | undefined); /** * Publishes an event to all registered observers. * * @param eventName - The event name to publish. * @param payload - The payload to send to each observer. * @throws Error if no observers are registered for the event. */ publish<K extends keyof O>(eventName: K, payload: O[K]): Promise<void>; /** * Returns all events names registered. * */ getRegisteredEventNames(): (keyof O)[]; } //# sourceMappingURL=handler.d.ts.map