@jupri-lab/store-core
Version:
A lightweight, easy-to-integrate, and type-safe state management library
37 lines (33 loc) • 1.6 kB
TypeScript
interface IStoreConfigs<TInitialState extends object, TActions extends Record<string, TAction<TInitialState, any>>> {
actions: TActions;
initialState: TInitialState;
middlewares?: TMiddleware[];
name: string;
}
type TAction<TInitialState, TPayload = void> = (state: TInitialState, payload: TPayload) => Promise<TInitialState> | TInitialState;
type TSubscriber<TInitialState> = (state: TInitialState) => void;
type TNextFunction = () => boolean;
type TMiddleware = (params: {
action: TAction<any>;
actionName: string;
payload: any;
state: any;
}, next: TNextFunction) => ReturnType<TNextFunction> | undefined;
declare class Store<TInitialState extends object, TActions extends Record<string, TAction<TInitialState, any>>> {
private subscribers;
private states;
private actions;
private middleware;
constructor(configs: IStoreConfigs<TInitialState, TActions>);
get(): TInitialState;
dispatch<T extends keyof TActions>(actionName: T, ...payload: Parameters<TActions[T]>[1] extends void ? [] : [Parameters<TActions[T]>[1]]): void;
subscribe(callback: TSubscriber<TInitialState>): () => void;
unsubscribe(callback: TSubscriber<TInitialState>): void;
private notify;
}
declare class CombineStores<TStores extends Record<string, Store<any, any>>> {
private stores;
constructor(stores: TStores);
getStore<TSelector extends (stores: TStores) => any>(selector: TSelector): ReturnType<TSelector>;
}
export { CombineStores, type IStoreConfigs, Store, type TAction, type TMiddleware, type TNextFunction, type TSubscriber };