UNPKG

react-liberate

Version:

simple and elegant React Global State Manager.

70 lines (66 loc) 3.35 kB
type StateTree = Record<string | number | symbol, unknown>; type _StoreWithGetters<G> = { readonly [k in keyof G]: G[k] extends (...args: any[]) => infer R ? R : G[k]; }; type _ActionsTree = Record<string | number | symbol, (...args: any[]) => any>; interface LiberateCustomStateProperties<S extends StateTree = StateTree> { } type _GettersTree<S extends StateTree> = Record<string, (state: S & LiberateCustomStateProperties<S>) => any>; /** * Interface to be extended by the user when they add properties through plugins. */ interface LiberateCustomProperties<Id extends string = string, S extends StateTree = StateTree, G = _GettersTree<S>, A = _ActionsTree> { } type _DeepPartial<T> = { [K in keyof T]?: _DeepPartial<T[K]>; }; interface _StoreWithState<Id extends string, S extends StateTree, G, A> { $id: Id; $state: S & LiberateCustomStateProperties<S>; $patch(partialState: _DeepPartial<S>): void; $patch<F extends (state: S) => any>(stateMutator: ReturnType<F> extends Promise<any> ? never : F): void; $reset(): void; $subscribe(callback: (newValue: S) => any, options?: { detached: boolean; }): any; } type Store<Id extends string, S extends StateTree, G, A> = _StoreWithState<Id, S, G, A> & S & _StoreWithGetters<G> & (_ActionsTree extends A ? {} : A) & LiberateCustomProperties<Id, S, G, A> & LiberateCustomStateProperties<S>; type StoreGeneric = Store<string, StateTree, _GettersTree<StateTree>, _ActionsTree>; interface DefineStoreOptionsBase<S extends StateTree, Store> { } interface DefineStoreOptions<Id extends string, S extends StateTree, G, A> extends DefineStoreOptionsBase<S, Store<Id, S, G, A>> { state?: () => S; getters?: G & ThisType<S & _StoreWithGetters<G> & LiberateCustomProperties>; actions?: A & ThisType<A & S & _StoreWithState<Id, S, G, A> & _StoreWithGetters<G> & LiberateCustomProperties>; } /** * Return type of `defineStore()`. Function that allows instantiating a store. */ interface StoreDefinition<Id extends string = string, S extends StateTree = StateTree, G = _GettersTree<S>, A = _ActionsTree> { /** * Returns a store, creates it if necessary. */ (): Store<Id, S, G, A>; /** * Id of the store. Used by map helpers. */ $id: Id; } type LiberatePluginContext<Id extends string = string, S extends StateTree = StateTree, G = _GettersTree<S>, A = _ActionsTree> = { options: DefineStoreOptions<Id, S, G, A>; store: Store<Id, S, G, A>; }; interface LiberatePlugin { (context: LiberatePluginContext): Partial<LiberateCustomProperties & LiberateCustomStateProperties> | void; } declare function defineStore<Id extends string, S extends StateTree, G extends _GettersTree<S> = {}, A extends _ActionsTree = {}>(id: Id, options: DefineStoreOptions<Id, S, G, A>): StoreDefinition<Id, S, G, A>; interface Liberate { _store: Map<string, StoreGeneric>; _state: Map<string, StateTree>; _plugins: Set<LiberatePlugin>; use(plugin: LiberatePlugin): this; } declare let liberate: Liberate; declare function createLiberate(): Liberate; declare function setActiveLiberate(_liberate: Liberate): void; export { DefineStoreOptionsBase, LiberateCustomProperties, LiberateCustomStateProperties, LiberatePlugin, LiberatePluginContext, StateTree, Store, createLiberate, defineStore, liberate, setActiveLiberate };