flow-event
Version:
136 lines (123 loc) • 4.41 kB
TypeScript
declare class EventEmitter {
// Constructor
constructor();
// Constructor
private ee: Map<string, Map<symbol, Function>>;
private queues: Map<string, any[]>;
private processingStates: Map<string, boolean>;
private maxConcurrentPerEvent: number;
/**
* Checks if an event with the specified name exists.
* @param name The name of the event to check.
* @returns A boolean indicating whether the event exists.
*/
has(name: string): boolean;
/**
* Returns the number of listeners for the specified event name.
* @param name The name of the event.
* @returns The number of listeners for the event.
*/
listenerCount(name: string): number;
/**
* Returns the names of all currently registered events.
* @returns An array of event names.
*/
eventNames(): string[];
/**
* Clears the queue for the specified event.
* @param name The name of the event.
* @returns A boolean indicating whether the queue was successfully cleared.
*/
clearQueue(name: string): boolean;
/**
* Retrieves the current queue for the specified event.
* @param name The name of the event.
* @returns An array of queued data for the event.
*/
getQueue<T extends any[]>(name: string): T[];
/**
* Checks if the specified event is currently being processed.
* @param name The name of the event.
* @returns A boolean indicating whether the event is being processed.
*/
isProcessing(name: string): boolean;
/**
* Checks if an event with the specified name exists.
* @param name The name of the event to check.
* @returns A boolean indicating whether the event exists.
*/
has(name: string): boolean;
/**
* Returns the number of listeners for the specified event name.
* @param name The name of the event.
* @returns The number of listeners for the event.
*/
listenerCount(name: string): number;
/**
* Checks if the specified event is currently being processed.
* @param name The name of the event.
* @returns A boolean indicating whether the event is being processed.
*/
isProcessing(name: string): boolean;
/**
* Returns the names of all currently registered events.
* @returns An array of event names.
*/
eventNames(): string[];
/**
* Add a listener for a specific event.
* @param name - Event name
* @param listener - Callback function
* @returns An object with the listener ID and a `remove` method
*/
addListener<T = any>(name: string, listener: Listener<T>): { id: symbol, remove: () => void };
/**
* Remove a specific listener by its ID.
* @param name - Event name
* @param id - Listener ID
* @returns True if the listener was removed, otherwise false
*/
removeId(name: string, id: symbol): boolean;
/**
* Remove all listeners for a specific event.
* @param name - Event name
* @returns True if listeners were removed, otherwise false
*/
remove(name: string): boolean;
/**
* Remove all listeners for all events.
* @returns Void
*/
removeAll(): void;
/**
* Emit an event to all its listeners.
* @param name - Event name
* @param data - Data to be passed to the listeners
*/
emit<T>(name: string, ...data: T extends any[] ? T : [T]): void;
/**
* Add a listener that will be called only once.
* @param name - Event name
* @param listener - Callback function
* @returns An object with the listener ID and a `remove` method
*/
once<T = any>(name: string, listener: Listener<T>): { id: symbol, remove: () => void };
/**
* Emit an event with queued processing.
* @param name - Event name
* @param data - Data to be passed to the listeners
*/
emitQueue<T>(name: string, ...data: T extends any[] ? T : [T]): void;
/**
* Process the queue of a specific event.
* @param name - Event name
*/
processQueue(name: string): void;
// Aliases
on: EventEmitter['addListener'];
off: EventEmitter['remove'];
removeAllListeners: EventEmitter['removeAll'];
}
export default EventEmitter;
export type ISupcription = { remove: () => void, id: symbol }
export type Listener<T = any> = T extends any[] ? (...args: T) => void : (arg: T) => void;