UNPKG

@sourcebug/amos

Version:

A decentralized state manager for react

140 lines (139 loc) 3.76 kB
import { Action } from './action'; import { Box, Mutation } from './box'; import { Selector } from './selector'; import { Signal } from './signal'; import { AmosObject } from './utils'; /** * the state snapshot in store * * @stable */ export declare type Snapshot = Record<string, unknown>; /** * dispatchable things * * @stable */ export declare type Dispatchable<R = any> = Mutation<R> | Action<R> | Signal<R>; /** * base amos signature, this is used for someone want to change the signature of useDispatch() * * @stable */ export interface AmosDispatch extends AmosObject<'store.dispatch'> { <R>(task: Dispatchable<R>): R; <R1>(tasks: readonly [Dispatchable<R1>]): [R1]; <R1, R2>(tasks: readonly [Dispatchable<R1>, Dispatchable<R2>]): [R1, R2]; <R1, R2, R3>(tasks: readonly [Dispatchable<R1>, Dispatchable<R2>, Dispatchable<R3>]): [ R1, R2, R3 ]; <R1, R2, R3, R4>(tasks: readonly [Dispatchable<R1>, Dispatchable<R2>, Dispatchable<R3>, Dispatchable<R4>]): [R1, R2, R3, R4]; <R1, R2, R3, R4, R5>(tasks: readonly [ Dispatchable<R1>, Dispatchable<R2>, Dispatchable<R3>, Dispatchable<R4>, Dispatchable<R5> ]): [R1, R2, R3, R4, R5]; <R1, R2, R3, R4, R5, R6>(tasks: readonly [ Dispatchable<R1>, Dispatchable<R2>, Dispatchable<R3>, Dispatchable<R4>, Dispatchable<R5>, Dispatchable<R6> ]): [R1, R2, R3, R4, R5, R6]; <R1, R2, R3, R4, R5, R6, R7>(tasks: readonly [ Dispatchable<R1>, Dispatchable<R2>, Dispatchable<R3>, Dispatchable<R4>, Dispatchable<R5>, Dispatchable<R6>, Dispatchable<R7> ]): [R1, R2, R3, R4, R5, R6, R7]; <R1, R2, R3, R4, R5, R6, R7, R8>(tasks: readonly [ Dispatchable<R1>, Dispatchable<R2>, Dispatchable<R3>, Dispatchable<R4>, Dispatchable<R5>, Dispatchable<R6>, Dispatchable<R7>, Dispatchable<R8> ]): [R1, R2, R3, R4, R5, R6, R7, R8]; <R1, R2, R3, R4, R5, R6, R7, R8, R9>(tasks: readonly [ Dispatchable<R1>, Dispatchable<R2>, Dispatchable<R3>, Dispatchable<R4>, Dispatchable<R5>, Dispatchable<R6>, Dispatchable<R7>, Dispatchable<R8>, Dispatchable<R9> ]): [R1, R2, R3, R4, R5, R6, R7, R8, R9]; <R>(tasks: readonly Dispatchable<R>[]): R[]; } export interface Dispatch extends AmosDispatch { } /** * selectable things * * @stable */ export declare type Selectable<R = any> = Box<R> | Selector<R>; /** * select * * @stable */ export interface Select extends AmosObject<'store.select'> { <R>(selectable: Selectable<R>, snapshot?: Snapshot): R; } /** * Store * * @stable */ export interface Store { /** * get the state snapshot of the store. * * Please note that any mutation of the snapshot is silent. */ snapshot: () => Snapshot; /** * dispatch one or more dispatchable things. */ dispatch: Dispatch; /** * subscribe the mutations * @param fn */ subscribe: (fn: (updatedState: Snapshot) => void) => () => void; /** * select a selectable thing */ select: Select; /** * whether to auto batch the updates * @default false */ isAutoBatch?: boolean; batchedUpdates: SchedulerFn; } declare type SchedulerFn = (cb: () => void) => void; export declare let batchedUpdates: SchedulerFn; export declare type StoreEnhancer = (store: Store) => Store; /** * create a store * @param preloadedState * @param enhancers * * @stable */ export declare function createStore(preloadedState?: Snapshot, ...enhancers: StoreEnhancer[]): Store; export {};