UNPKG

state-jet

Version:

Ultra-lightweight global state management for React

46 lines (38 loc) 2.07 kB
type Middleware<T> = (_key: string, _prev: T | undefined, _next: T | Action<T>, _set?: (_value: T) => void) => T | void | Promise<T | void>; type Options<T> = { middleware?: Middleware<T | undefined>[]; persist?: boolean; encrypt?: boolean; frameSync?: boolean; }; type Action<T> = { type: string; payload?: T; }; interface StoreHook<T> { useState: () => T; set: (val: T | ((prev: T) => T) | Action<T>, immediate?: boolean) => Promise<void>; get: () => T; undo: () => void; redo: () => void; clear: () => void; } declare function useSlice(sliceName: string): <T>(key: string, initialValue: T, options?: Options<T | undefined>) => StoreHook<T>; declare function useStateGlobal<T>(key: string, initialValue: T, options?: Options<T>): StoreHook<T>; declare function useStateGlobal<T>(key: string, initialValue?: T, options?: Options<T>): StoreHook<T | undefined>; declare function useStore<T extends Record<string, unknown>>(initializer: () => T): T; declare const saveState: <T>(key: string, value: T) => void; declare const restoreState: <T>(key: string, defaultValue?: T) => any; declare const optimisticUpdate: <T>(setState: { set: (value: T) => void; useState: () => T; }, updateFn: (prevState: T) => T, apiCall: () => Promise<T>, rollbackFn?: (prevState: T) => T) => Promise<void>; declare const saveEncryptedState: <T>(key: string, value: T) => void; declare const restoreEncryptedState: <T>(key: string, defaultValue?: T) => unknown; declare const mergeCRDT: <T>(localState: T, remoteState: T) => T; declare const syncCRDT: <T>(remoteState: T, getStateValue: () => T, set: (state: T) => void) => void; declare const derivedState: <T, D extends { useState: () => unknown; }[]>(dependencies: D, computeFn: (...values: { [K in keyof D]: ReturnType<D[K]["useState"]>; }) => T) => () => T; declare const VERSION = "2.4.0"; export { VERSION, derivedState, mergeCRDT, optimisticUpdate, restoreEncryptedState, restoreState, saveEncryptedState, saveState, syncCRDT, useSlice, useStateGlobal, useStore };