jotai-controller
Version:
A powerful state management library built on top of Jotai with controller pattern for React applications
75 lines • 2.57 kB
TypeScript
import { createStore } from 'jotai';
import { StateObject, StateChangeListener } from './types';
/**
* Abstract base class for state and storage controllers
* Extracts common functionality for DRY principle
*/
export declare abstract class BaseController<T extends StateObject> {
protected store: ReturnType<typeof createStore>;
protected initialState: T;
protected listeners: Map<keyof T, Set<StateChangeListener<T, keyof T>>>;
constructor(initialState: T, customStore?: ReturnType<typeof createStore>);
/**
* Abstract method to get the current value of a key
* Must be implemented by subclasses
*/
protected abstract getKeyValue<K extends keyof T>(key: K): T[K];
/**
* Abstract method to set the value of a key
* Must be implemented by subclasses
*/
protected abstract setKeyValue<K extends keyof T>(key: K, value: T[K]): void;
/**
* Abstract method to get all current values
* Must be implemented by subclasses
*/
protected abstract getAllCurrentValues(): T;
/**
* Subscribe to changes on a specific state key
* @param key The state key to listen for changes
* @param listener Callback function that receives the new and old values
* @returns Unsubscribe function to remove the listener
*/
subscribe<K extends keyof T>(key: K, listener: StateChangeListener<T, K>): () => void;
/**
* Clear all active listeners
*/
clearAllListeners(): void;
/**
* Internal method to notify listeners of state changes
*/
protected notifyListeners(newState: Partial<T>, prevState: T): void;
/**
* Internal method to notify listeners for a specific key
*/
protected notifyKeyListeners<K extends keyof T>(key: K, newValue: T[K], oldValue: T[K]): void;
/**
* Reset multiple states by calling resetState for each key
*/
resetStates(keys: (keyof T)[]): void;
/**
* Reset all states
*/
resetAll(): void;
/**
* Reset a single state key to its initial value
*/
resetState(key: keyof T): void;
/**
* Get a single value by key
*/
getValue<K extends keyof T>(key: K): T[K];
/**
* Get multiple values by keys
*/
getValues(keys: (keyof T)[]): Partial<T>;
/**
* Set state (replace with new values, merging with initial state)
*/
setState(newState: Partial<T>): void;
/**
* Update state (merge with current state)
*/
updateState(newState: Partial<T>): void;
}
//# sourceMappingURL=BaseController.d.ts.map