rich-domain
Version:
This package provide utils file and interfaces to assistant build a complex application with domain driving design
62 lines • 3.02 kB
TypeScript
import { Event, EventManager } from "../types";
/**
* @description BrowserEventManager provides a global event management system
* utilizing the browser's Window and Event mechanisms. It allows subscribing to
* events that can be dispatched both synchronously and asynchronously, storing
* event states in sessionStorage and ensuring persistence across page reloads.
*/
export default class BrowserEventManager implements EventManager {
private readonly _window;
private events;
private static _instance;
/**
* @description Creates an instance of BrowserEventManager.
* Throws an error if the provided window object is not defined,
* as this manager is intended for browser environments.
* @param _window The browser's window object.
* @private
*/
private constructor();
/**
* @description Returns a singleton instance of BrowserEventManager.
* If an instance has already been created, it returns the existing one.
* @param window The browser's window object.
* @returns The singleton instance of BrowserEventManager.
*/
static instance(window: Window & typeof globalThis): BrowserEventManager;
/**
* @description Retrieves an event by name from the internal events list.
* @param eventName The name of the event to retrieve.
* @returns The matching EventType object if found, otherwise null.
* @private
*/
private getEvent;
/**
* @description Subscribes a callback function to a given event.
* Ensures that the event subscription is persisted in sessionStorage,
* and cleans up the subscription when the browser unloads.
* @param eventName The name of the event to subscribe to.
* @param fn The callback function to execute when the event is dispatched.
*/
subscribe(eventName: string, fn: (event: Event) => void | Promise<void>): void;
/**
* @description Checks if the given event name is already subscribed and persisted.
* @param eventName The name of the event.
* @returns `true` if the event is subscribed or exists in sessionStorage; otherwise, `false`.
*/
exists(eventName: string): boolean;
/**
* @description Removes a subscribed event by name, cleaning up any related listeners and sessionStorage entries.
* @param eventName The name of the event to remove.
* @returns `true` if the event was found and removed successfully; otherwise, `false`.
*/
removerEvent(eventName: string): boolean;
/**
* @description Dispatches a custom event with the given name and optional arguments.
* Supports wildcard patterns by replacing `'*'` with `'.*'` to dispatch multiple matching events.
* @param eventName The name of the event to dispatch. Can include a wildcard `'*'`.
* @param args Additional arguments passed as the `detail` property of the dispatched event.
*/
dispatchEvent(eventName: string, ...args: any[]): void;
}
//# sourceMappingURL=browser-event-manager.d.ts.map