UNPKG

rich-domain

Version:

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

62 lines 2.79 kB
import { Event, EventManager } from "../types"; /** * @class ServerEventManager * @description A singleton class to manage server-side events. This class ensures that event listeners are properly managed and provides utilities for subscribing, dispatching, and removing events. * @implements {EventManager} */ export default class ServerEventManager implements EventManager { private events; private static _instance; private readonly emitter; /** * @private * @constructor * @throws {Error} If the runtime environment does not support EventEmitter or Node.js process. */ private constructor(); /** * @static * @method instance * @description Returns the singleton instance of the ServerEventManager. If it doesn't exist, it initializes a new instance. * @returns {ServerEventManager} The singleton instance. */ static instance(): ServerEventManager; /** * @private * @method getEvent * @description Retrieves the event handler associated with a given event name. * @param {string} eventName The name of the event. * @returns {EventType | null} The event type if found, otherwise null. */ private getEvent; /** * @method subscribe * @description Subscribes a callback function to a specific event. If the event already exists, it won't be added again. * @param {string} eventName The name of the event. * @param {(event: Event) => void | Promise<void>} fn The callback function to handle the event. * @throws {Error} If the event name does not follow the context:event pattern. */ subscribe(eventName: string, fn: (event: Event) => void | Promise<void>): void; /** * @method exists * @description Checks if an event with the given name is already registered. * @param {string} eventName The name of the event to check. * @returns {boolean} True if the event exists, false otherwise. */ exists(eventName: string): boolean; /** * @method removerEvent * @description Removes an event and its associated callback from the manager and EventEmitter. * @param {string} eventName The name of the event to remove. * @returns {boolean} True if the event was successfully removed, false otherwise. */ removerEvent(eventName: string): boolean; /** * @method dispatchEvent * @description Dispatches an event, optionally supporting wildcard patterns to emit multiple events. * @param {string} eventName The name of the event to dispatch. Supports wildcard patterns using `*`. * @param {...any[]} args Arguments to pass to the event handler. */ dispatchEvent(eventName: string, ...args: any[]): void; } //# sourceMappingURL=server-event-manager.d.ts.map