@sovgut/state
Version:
<p align="center"> <b>A lightweight, type-safe, and reactive state management library for modern web applications</b> </p>
68 lines (67 loc) • 2.71 kB
TypeScript
import { IStorageEventData } from '../../types.ts';
/**
* Callback function type for event listeners
* @template T - The type of data passed to the callback
*/
export type Callback<T = unknown> = (event: IStorageEventData<T>) => void;
/**
* Base observer class that provides event emitter functionality for state management classes.
* Uses a registry pattern to maintain separate event systems for each inheriting class.
*/
export declare class Observer {
private static eventRegistry;
/**
* Gets or creates the event registry for a specific class
* @param target - The class constructor function
* @returns The event registry for the target class
*/
private static getRegistry;
/**
* Adds an event listener for the specified event
* @template T - The type of data passed to the callback
* @param event - The name of the event to listen for
* @param callback - The callback function to execute when the event is emitted
*/
static on<T = unknown>(event: string, callback: Callback<T>): void;
/**
* Adds a one-time event listener for the specified event
* @template T - The type of data passed to the callback
* @param event - The name of the event to listen for
* @param callback - The callback function to execute once when the event is emitted
*/
static once<T = unknown>(event: string, callback: Callback<T>): void;
/**
* Removes an event listener for the specified event
* @template T - The type of data passed to the callback
* @param event - The name of the event
* @param callback - The callback function to remove
*/
static off<T = unknown>(event: string, callback: Callback<T>): void;
/**
* Emits an event with the given data to all registered listeners
* @template T - The type of data being emitted
* @param event - The name of the event to emit
* @param data - The data to pass to the event listeners
*/
static emit<T = unknown>(event: string, data: IStorageEventData<T>): void;
/**
* Removes all event listeners for all events
*/
static removeAllListeners(): void;
/**
* Removes all listeners for a specific event
* @param event - The name of the event
*/
static removeListener(event: string): void;
/**
* Gets the number of listeners for a specific event
* @param event - The name of the event
* @returns The total number of listeners (regular + once)
*/
static listenerCount(event: string): number;
/**
* Gets an array of all event names that have listeners
* @returns Array of event names
*/
static eventNames(): (string | symbol)[];
}