UNPKG

rich-domain

Version:

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

93 lines 4.34 kB
import { EventHandler, Settings, Options, UID } from "../types"; import { EntityProps, EventMetrics, Handler, _Aggregate } from "../types"; import TsEvent from "./events"; import Entity from "./entity"; import Result from "./result"; import { EventManager } from "../types"; /** * @description Represents an aggregate identified by a unique ID, extending entity functionalities. * It manages domain events internally, facilitates event dispatching, and provides metrics * on event-related operations. */ export declare class Aggregate<Props extends EntityProps> extends Entity<Props> implements _Aggregate<Props> { private _domainEvents; private _dispatchEventsAmount; constructor(props: Props, config?: Settings, events?: TsEvent<_Aggregate<Props>>); /** * @description Generates a unique hash code for the aggregate, combining the class name and its ID. * Useful for identification in logs or diagnostic information. * @example * `[Aggregate@ClassName]:UUID` * @summary Class name is determined at runtime from the prototype. * @returns A `UID<string>` representing the aggregate's hash code. */ hashCode(): UID<string>; /** * @description Returns the global event manager for the current context, enabling * the registration (subscribe) and dispatching of events at the application level. * @returns The application's `EventManager` instance. */ context(): EventManager; /** * @description Provides event-related metrics for the aggregate. * @property current - The number of currently stored (undispatched) events. * @property total - The total number of events, including those already dispatched. * @property dispatch - The number of events already dispatched by the aggregate. * @returns An `EventMetrics` object containing event metrics. */ get eventsMetrics(): EventMetrics; /** * @description Creates a new aggregate instance based on the current one. * Allows overriding some properties. If no `id` is provided in the new properties, * a new one will be generated. * @param props Optional partial properties to override for the new instance. * The `copyEvents` property can be used to copy current events to the new instance. * @returns A new aggregate instance with updated properties. */ clone(props?: Partial<Props> & { copyEvents?: boolean; }): this; /** * @description Dispatches a specific event from the aggregate, incrementing the count of dispatched events. * @param eventName The name of the event to dispatch. * @param args Additional arguments passed to the event handler. * @returns `void` or `Promise<void>` if the event is asynchronous. */ dispatchEvent(eventName: string, ...args: any[]): void | Promise<void>; /** * @description Dispatches all currently stored events in the aggregate, marking them as dispatched * and updating the total count of dispatched events. * @returns A Promise that resolves to `void` after all events are dispatched. */ dispatchAll(): Promise<void>; /** * @description Removes all currently stored events in the aggregate. * @param config Optional configuration. If `resetMetrics` is `true`, * the count of previously dispatched events is reset to zero. * @returns `void`. */ clearEvents(config?: { resetMetrics: boolean; }): void; /** * @description Adds a new event to the aggregate. * @param event The event object containing the event name, handler, and options. */ addEvent(event: EventHandler<this>): void; /** * @description Adds a new event to the aggregate. * @param eventName The name of the event. * @param handler The event handler function. * @param options Additional options for the event. */ addEvent(eventName: string, handler: Handler<this>, options?: Options): void; /** * @description Removes all events matching the provided event name. * @param eventName The name of the event to remove. * @returns The number of events removed. */ deleteEvent(eventName: string): number; static create(props: any): Result<any, any, any>; } export default Aggregate; //# sourceMappingURL=aggregate.d.ts.map