@signaldb/core
Version:
SignalDB is a client-side database that provides a simple MongoDB-like interface to the data with first-class typescript support to achieve an optimistic UI. Data persistence can be achieved by using storage providers that store the data through a JSON in
72 lines (71 loc) • 3.18 kB
TypeScript
/**
* A strongly‑typed EventEmitter.
*/
export default class EventEmitter<Events extends Record<string | symbol, any>> {
private _maxListeners;
/**
* We store a set of the listeners for each event.
*/
private _listenerStore;
setMaxListeners(max: number): this;
/**
* Subscribe to an event with a listener function.
* @param eventName The event name (key of E).
* @param listener A function that receives the emitted arguments.
* @returns The emitter instance (for chaining).
*/
on<K extends keyof Events>(eventName: K, listener: Events[K]): this;
/**
* Subscribe to an event with a listener function.
* @param eventName The event name (key of E).
* @param listener A function that receives the emitted arguments.
* @returns The emitter instance (for chaining).
*/
addListener<K extends keyof Events>(eventName: K, listener: Events[K]): this;
/**
* Subscribe to an event, handling it only once. Automatically removes
* the listener after it fires the first time.
* @param eventName The event name (key of E).
* @param listener A function that receives the emitted arguments.
* @returns The emitter instance (for chaining).
*/
once<K extends keyof Events>(eventName: K, listener: Events[K]): this;
/**
* Unsubscribe a previously subscribed listener.
* @param eventName The event name (key of E).
* @param listener The original function passed to `on` or `once`.
* @returns The emitter instance (for chaining).
*/
off<K extends keyof Events>(eventName: K, listener: Events[K]): this;
/**
* Unsubscribe a previously subscribed listener.
* @param eventName The event name (key of E).
* @param listener The original function passed to `on` or `once`.
* @returns The emitter instance (for chaining).
*/
removeListener<K extends keyof Events>(eventName: K, listener: Events[K]): this;
/**
* Emit (dispatch) an event with a variable number of arguments.
* @param eventName The event name (key of E).
* @param args The arguments to pass to subscribed listeners.
*/
emit<K extends keyof Events>(eventName: K, ...args: Parameters<Events[K]>): void;
/**
* Returns the array of listener functions currently registered for a given event.
* @param eventName The event name (key of E).
* @returns An array of listener functions.
*/
listeners<K extends keyof Events>(eventName: K): Array<(...args: Parameters<Events[K]>) => void>;
/**
* Returns the number of listeners for a given event.
* @param eventName The event name (key of E).
* @returns The number of listeners.
*/
listenerCount<K extends keyof Events>(eventName: K): number;
/**
* Removes all listeners for a given event, or all events if none is specified.
* @param eventName Optional. If omitted, clears all events’ listeners.
* @returns The emitter instance (for chaining).
*/
removeAllListeners<K extends keyof Events>(eventName?: K): this;
}