UNPKG

typescript-event-emitter

Version:

Versatile and feature-rich TypeScript library for event management, providing a solid foundation for building event-driven applications in TypeScript.

122 lines (121 loc) 5.71 kB
import { EventHistory, EventInfo, GlobalOption, Hook, Option } from './Interfaces'; import { ListenerManager } from './ListenerManager'; import { AsyncListener, Listener } from './Types'; export declare class EventEmitter { private listenerManager; private globalOption; /** * Creates an instance of EventEmitter. * @param globalOption - Global options for the class. * @param globalOption.separator - The separator used across all listeners unless listener contains custom separator. */ constructor(globalOption?: GlobalOption); /** * Sets global options for the EventEmitter. * @param options - Global options, such as the separator. */ setGlobalOptions(options: GlobalOption): void; /** * Retrieves the current global options set for the EventEmitter instance. * * @returns {GlobalOption} - The current global options object containing properties such as the separator used across all listeners. */ getGlobalOptions(): GlobalOption; /** * Retrieves the current instance of the `ListenerManager`. * * @returns {ListenerManager} The current instance of `ListenerManager`. */ getListenerManager(): ListenerManager; /** * Retrieves the event history based on a specific event name. * * @param event - The name of the event to filter by. This can include a namespace (e.g., 'namespace.eventName'). * @returns {Array<{ event: string, listenerId: string, timestamp: number, args: unknown[] }>} * - An array of objects that match the specified event name, each containing the event name, listener ID, timestamp, and arguments. */ getSpecificEventHistory(event: string): Array<EventHistory>; /** * Retrieves all recorded event histories. * * @returns {Array<{ event: string, listenerId: string, timestamp: number, args: unknown[] }>} * - An array of objects, each containing the event name, listener ID, timestamp, and the arguments passed to the listener. */ getAllEventHistory(): Array<EventHistory>; /** * Adds a listener for the specified event, optionally applying filters, throttling, debouncing, and setting priority. * @param event - The event name, possibly with a namespace. * @param listener - The function to be called when the event is emitted. * @param options - An optional object containing properties like `filter`, `throttle`, `debounce`, and `priority`. * @param options.filter - A filter function to determine whether to emit the event. * @param options.throttle - The time delay (in milliseconds) for throttling the listener's execution. * @param options.debounce - The time delay (in milliseconds) for debouncing the listener's execution. * @param options.priority - The priority of the listener, higher values execute first (default is 0). * @param options.concurrency - Maximum number of listeners executed in parallel (default is unlimited). * @param options.separator - Separator used for parsing the event (if applicable, default is '.'). */ on(event: string, listener: Listener | AsyncListener, option?: Option): void; /** * Removes a previously added listener for the specified event. * @param event - The event name, possibly with a namespace. * @param listener - The listener function to be removed. */ off(event: string, listener: Listener | AsyncListener): void; /** * Emits the specified event, calling all associated listeners. * @param event - The event name, possibly with a namespace. * @param args - Additional arguments to be passed to the listeners. * @returns A promise that resolves when all listeners have been executed. */ emit(event: string, ...args: unknown[]): Promise<void>; /** * Lists all event subscriptions, including event names and listener count. * * @returns {Array<{ event: string, listenerCount: number }>} - An array of objects, each containing event name and count of listeners. */ subscriptions(): Array<{ event: string; listenerCount: number; }>; /** * Inspects a specific event subscription, showing details of listeners. * * @param event - The event name, possibly with a namespace (e.g., 'namespace.eventName'). * @returns {Array<Object>} - An array of objects, each containing listener details (id, priority, concurrency, eventInfo, listener). */ inspectSubscription(event: string): Array<{ id: string; eventInfo: EventInfo; listener: Listener; priority: number; concurrency: number; }>; /** * Remove a subscription from the ListenerManager. * * @param event - The event name which can include a namespace (e.g., 'namespace.eventName'). * @param listenerId - The unique identifier of the listener to be removed. */ removeSubscription(event: string, listenerId: string): void; /** * Registers a hook to be executed before any listeners for an event are called. * @param hook - The function to execute before the event. */ before(hook: Hook): this; /** * Registers a hook to be executed after all listeners for an event have completed. * @param hook - The function to execute after the event. */ after(hook: Hook): this; /** * Removes a 'before' hook. * @param hook - The hook function to remove. */ removeBefore(hook: Hook): this; /** * Removes an 'after' hook. * @param hook - The hook function to remove. */ removeAfter(hook: Hook): this; } export declare const globalEventBus: EventEmitter;