UNPKG

rich-domain

Version:

This package provide utils file and interfaces to assistant build a complex application with domain driving design

86 lines 3.88 kB
import { Metrics, Options, Handler } from "../types"; /** * @description TsEvents manages a collection of events for a given aggregate. * It allows adding, removing, and dispatching events, as well as retrieving event-related metrics. */ export declare class TsEvents<T> { private readonly aggregate; private _metrics; private _events; private totalDispatched; /** * @description Creates an instance of TsEvents associated with a given aggregate. * @param aggregate The aggregate instance to which these events belong. */ constructor(aggregate: T); /** * @description Provides metrics related to the currently managed events, such as the total number of * dispatched events and the total number of events pending. * @returns {Metrics} An object containing functions to retrieve event metrics. */ get metrics(): Metrics; /** * @description Determines a priority value based on the number of currently stored events. * If there is one or zero events, the priority is set to 2. Otherwise, it equals the total number of events. * @returns The computed priority value. * @private */ private getPriority; /** * @description Dispatches a single event by its name. If the event is found, it is executed and then removed. * If the event does not exist, this method does nothing. * @param eventName The name of the event to dispatch. * @param args Additional arguments to pass to the event's handler function. * @returns A void or Promise<void> depending on whether the event handler returns a promise. */ dispatchEvent(eventName: string, ...args: any[]): void | Promise<void>; /** * @description Returns default options for an event if none are provided. * This typically sets the event priority based on the current number of events. * @returns The default event options. * @private */ private getDefaultOptions; /** * @description Adds a new event to the manager. If an event with the same name already exists, * the old event is removed before adding the new one. * @param eventName The name of the event to add. * @param handler The function to handle the event when dispatched. * @param options Optional configuration for the event, such as priority. * * @throws Will throw an error if `eventName` is invalid (not a string or too short) or if `handler` is not a function. */ addEvent(eventName: string, handler: Handler<T>, options?: Options): void; /** * @description Validates that the handler is a function. If not, it throws an error. * @param handler The event handler to validate. * @param eventName The name of the event associated with this handler. * @private */ private validateHandler; /** * @description Validates the event name. It must be a string and have at least 3 characters. * @param eventName The event name to validate. * @throws Will throw an error if the event name is invalid. * @private */ private validateEventName; /** * @description Removes all currently registered events. */ clearEvents(): void; /** * @description Removes a specific event by its name. * @param eventName The name of the event to remove. */ removeEvent(eventName: string): void; /** * @description Dispatches all currently stored events in order of their priority (lowest priority number first). * Once dispatched, events are removed from the collection. * @returns A Promise that resolves once all promise-based event handlers have been completed. * In case of errors during promise resolution, they are logged to the console. */ dispatchEvents(): Promise<void>; } export default TsEvents; //# sourceMappingURL=events.d.ts.map