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.

77 lines (76 loc) 4.23 kB
import { GlobalOption, Option } from './Interfaces'; import { AsyncListener, Listener } from './Types'; export declare class EventEmitter { private eventNamespaces; private eventFilters; private readonly wildCardNamespace; 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; /** * 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.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>; /** * Executes specific listeners for the given namespace, event, and wildcard combination. * @param namespace - The namespace for which listeners should be executed. * @param checkEventName - The event name or wildcard to check for listeners. * @param eventName - The actual event name for emitting. * @param separator - Separator used for parsing the event (if applicable, default is '.'). * @param args - Additional arguments to be passed to the listeners. * @returns A promise that resolves when all specific listeners have been executed. */ private executeSpecificListeners; /** * Handles errors that occur during the execution of a listener. * @param eventName - The name of the event for which the listener encountered an error. * @param error - The error object representing the encountered error. */ private handleListenerError; /** * Creates a throttled version of the given listener function, ensuring it is called at most once within a specified time interval. * @param fn - The listener function to be throttled. * @param delay - The time delay (in milliseconds) between allowed invocations of the throttled function. * @param eventName - The name of the event associated with the listener. * @returns A throttled listener function. */ private throttle; /** * Creates a debounced version of the given listener function, ensuring it is called after a specified delay with no new invocations. * @param fn - The listener function to be debounced. * @param delay - The time delay (in milliseconds) before the debounced function is called after the last invocation. * @param eventName - The name of the event associated with the listener. * @returns A debounced listener function. */ private debounce; }