@mcastiello/simple-store
Version:
A simple state manager that can store data via a reducer function.
17 lines (16 loc) • 1.76 kB
TypeScript
import { ActionOf, ActionParameters, ActionsDefinitions } from "./actions";
export type GenericState = {
[Prop in string]: unknown;
};
export type Reducer<State extends GenericState, Definitions extends ActionsDefinitions<keyof Definitions>> = (state: State | undefined, action: ActionOf<Definitions, keyof Definitions>) => State;
export type StoreSubscription<State extends GenericState, Definitions extends ActionsDefinitions<keyof Definitions>> = <ActionType extends keyof Definitions>(state: State, action?: ActionOf<Definitions, ActionType>) => void | PromiseLike<void>;
export type StoreInterceptor<State extends GenericState, Definitions extends ActionsDefinitions<keyof Definitions>> = <ActionType extends keyof Definitions>(oldState: State | undefined, newState: State, action: ActionOf<Definitions, ActionType>) => State;
export type StoreSubscribe<State extends GenericState, Definitions extends ActionsDefinitions<keyof Definitions>> = (subscription: StoreSubscription<State, Definitions>, actions?: (keyof Definitions)[]) => () => void;
export type StoreIntercept<State extends GenericState, Definitions extends ActionsDefinitions<keyof Definitions>> = (interceptor: StoreInterceptor<State, Definitions>, actions: (keyof Definitions)[]) => () => void;
export type StoreDispatcher<Definitions extends ActionsDefinitions<keyof Definitions>> = <ActionType extends keyof Definitions>(...args: ActionParameters<Definitions, ActionType>) => void;
export type Store<State extends GenericState, Definitions extends ActionsDefinitions<keyof Definitions>> = Readonly<{
subscribe: StoreSubscribe<State, Definitions>;
intercept: StoreIntercept<State, Definitions>;
dispatch: StoreDispatcher<Definitions>;
state: Partial<Readonly<State>>;
}>;