/** * Subject implements a simple observable pattern. * It stores a value, allows subscriptions, and notifies observers and listeners * when the value changes. */ declare class Subject { private listeners; private name; value: T[K]; constructor(name: K, value: T[K]); addListener(listener: Listener, autoCallListener?: boolean): void; removeListener(listener: Listener): void; /** * Update the value and notify subscribers. * @param value - new value * @param isAlwaysNotify - notify listiners always * @returns undefined */ notify(value: T[K], isAlwaysNotify: boolean): void; } /** * Internal store representation: * each property of the state is wrapped into a Subject. * * If T has no keys, Store becomes never. */ type Values = keyof T extends never ? never : { [K in keyof T]: Subject; }; /** * Listener signature for store updates. * Receives the key name and the new value. */ type Listener = (name: K, value: T[K]) => void; /** * Observer pattern based store */ declare class Store { /** * Map of Subjects. * * Each Subject represents a single key of the state * and notifies its listeners when the value changes. */ values: Values; /** * Cached list of state keys. * * Initialized once from the initial state: * this.keys = Object.keys(state) as (keyof T)[]; */ keys: (keyof T)[]; /** * Current store state. * * T is expected to be a plain object. */ state: T; /** * Flag that defines whether the store * should mutate the state object on updates. */ isMutateState: boolean; /** * Store constructor. * * Each property of the initial state is converted into a Subject, * allowing independent subscriptions per key. * * @param state - Initial store state * @param isMutateState - [optional] mutate state on value updates */ constructor(state: T, isMutateState?: boolean); /** * Returns the current value for a given key. * * @param key K - key * @returns T[K] - value */ get(key: K): T[K]; /** * Updates the value for a given key * and notifies all registered listeners. * * @param key - K - key * @param value - T[K] - value * @param isAlwaysNotify - notify listiners always */ set(key: K, value: T[K], isAlwaysNotify?: boolean): void; /** * return state * * @returns state */ getState(): T; /** * Set state * * @param state - Initial store state * @param isMutateState - [optional]is mutate state * @param isAlwaysNotify - notify listiners always */ setState(state: T, isMutateState?: boolean, isAlwaysNotify?: boolean): void; /** * Check state relevance * * @returns boolean */ isStateActual(): boolean; /** * Subscribes a listener to changes of a specific key. * * @param key Store key to subscribe to * @param listener Callback invoked on value changes * @param autoCallListener [default: false] - Whether to call the listener immediately * with the current value * @returns Unsubscribe function */ addListener(key: K, listener: Listener, isAutoCallListener?: boolean): () => void; /** * Removes a previously registered listener for a key. * * @param key Store key to subscribe to * @param listener Callback invoked on value changes */ removeListener(key: K, listener: Listener): void; } /** * Creates a new isolated store instance. * * Each property of the initial state is converted into a Subject, * allowing independent subscriptions per key. * * @param state - Initial store state * @param isMutateState - [optional] is mutate state * @returns Store API with get/set and subscription methods */ declare const createStore: (state: T, isMutateState?: boolean) => Store; export { type Listener, Store, createStore };