/** @license Atomic State * Copyright (c) Dany Beltran * * This source code is licensed under the MIT license found in the * LICENSE file in the roo&t directory of this source tree. */ import { Dispatch, SetStateAction } from 'react'; type Observable = { addListener(event: string, listener?: any): void; removeListener(event: string, listener?: any): void; emit(event: string, payload?: any): void; }; export type ActionType = T extends undefined ? (args: { args?: Args; state: T; dispatch: Dispatch>; } & ActionGet) => Return : (args: { args: Args; state: T; dispatch: Dispatch>; } & ActionGet) => Return; /** * Atom type */ export type Atom = { /** * @deprecated Use `key` instead */ name?: string; key: string; default?: T | Promise | (() => Promise) | (() => T); /** * Short for `localStoragePersistence` */ persist?: boolean; /** * The persistence provider (optional). It should have the `getItem`, `setItem` and `removeItem` methods. * * @default localStorage */ persistenceProvider?: PersistenceStoreType; /** * If true, `persist` will keep the value in sync between tabs. * By default it's `true` */ sync?: boolean; /** * If `persist` is true, this will run whenever the state is updated from another tab. This will not run in the tab that updated the state. */ onSync?(message: T): void; actions?: { [E in keyof Partial]: ActionType; }; effects?: ((s: { previous: T; state: T; dispatch: Dispatch>; /** * Cancel the new state update */ cancel: () => void; }) => void)[]; }; export type ActionsObjectType = { [E in keyof ArgsTypes]: (args?: ArgsTypes[E]) => Returns; }; export type useAtomType = () => (R | Dispatch> | ActionsObjectType)[]; /** * Type for the `get` function of filters */ export type SelectorGet = { get(atom: useAtomType | Atom): R; }; /** * Type for the `get` function of filters */ export type ActionGet = { get(atom: useAtomType | Atom, storeName?: string | boolean): R; }; /** * Filter type */ export type Selector = { /** * @deprecated Use `key` instead */ name?: string; key: string; default?: T; get(c: SelectorGet): T | Promise; }; export declare function createObserver(): { observer: Observable; notify: (storeName: string, hookCall: string, payload: any) => void; }; type PersistenceGet = (key: string) => any; type PersistenceSet = (key: string, value: any) => void; type PersistenceRemove = (key: string) => void; export type PersistenceStoreType = { get?: PersistenceGet; getItem?: PersistenceGet; getItemAsync?: PersistenceGet; set?: PersistenceSet; setItem?: PersistenceSet; setItemAsync?: PersistenceSet; remove?: PersistenceRemove; removeItem?: PersistenceRemove; removeItemAsync?: PersistenceRemove; delete?: PersistenceRemove; deleteItem?: PersistenceRemove; deleteItemAsync?: PersistenceRemove; }; export declare function createPersistence(_persistenceProvider?: PersistenceStoreType): { setItem: any; getItem: any; removeItem: any; }; export declare const AtomicState: React.FC<{ clientOnly?: boolean; children: any; /** * Set default values using an atom's key */ default?: { [key: string]: any; }; value?: { [key: string]: any; }; /** * The store name where atoms under the tree will be saved */ storeName?: string | boolean; /** * The persistence provider (optional). It should have the `getItem`, `setItem` and `removeItem` methods. * * @default localStorage */ persistenceProvider?: PersistenceStoreType; }>; /** * Take a snapshot of all atoms' and filters' values. * You can pass a string with the `prefix` you used in the `AtomicState` root component * if you want only atoms and filters using that prefix. */ export declare function takeSnapshot(storeName?: string): any; export declare function setAtom($atom: Atom, v: SetStateAction, prefix?: string): void; export declare function getActions($atom: Atom): Required>; /** * Creates an atom containing state */ export declare function atom($init: Omit, 'name'> | Omit, 'name'>): Atom | Selector; export declare const createAtom: typeof atom; export declare function filter(init: Selector>): Selector; export declare const selector: typeof filter; export declare function useFilter(f: (() => T | Promise) | Selector>): T; /** * Get an atom's value and state setter */ export declare function useAtom(atom: Atom): [R, React.Dispatch>, ActionsObjectType]; /** * Creates a store with the `setPartialvalue`, `setValue` and `reset` methods. * It returns a hook that returns an array with the value and the actions */ export declare function createAtomicHook(config?: Partial>): { (): readonly [R, ActionsObjectType<{ setPartialvalue: Partial | ((v: Required) => Partial); setPartial: Partial | ((v: Required) => Partial); setValue: R | ((v: Required) => R); reset: any; } & Actions>]; atom: Atom | ((v: Required) => Partial); setPartial: Partial | ((v: Required) => Partial); setValue: R | ((v: Required) => R); reset: any; } & Actions> | Selector; }; /** * Creates a store with the `setPartialvalue`, `setValue` and `reset` methods. * It uses `createAtomicHook` under the hood but instead of returing an array, it returns an object with the store value and actions merged */ export declare function createStore(config?: Partial>): { (): R & ActionsObjectType<{ setPartialvalue: Partial | ((v: Required) => Partial); setPartial: Partial | ((v: Required) => Partial); setValue: R | ((v: Required) => R); reset: any; } & Actions>; atom: Atom | ((v: Required) => Partial); setPartial: Partial | ((v: Required) => Partial); setValue: R | ((v: Required) => R); reset: any; } & Actions> | Selector; }; export declare function create(config: Omit, 'name'> | Omit, 'name'>): { (): [R, Dispatch>, ActionsObjectType] & { value: R; setValue: Dispatch>; actions: ActionsObjectType; }; value(): R; set(value: R | ((v: R) => R), storeName?: string): void; actions: Required>; atom: Atom | (() => Promise) | (() => R), Actions> | Selector | (() => Promise) | (() => R)>; useValue(): R; setValue: (value: R | ((v: R) => R), storeName?: string) => void; useAtom(): [R, Dispatch>, ActionsObjectType]; }; export declare const store: typeof createStore; export declare const atomicHook: typeof createAtomicHook; /** * Get an atom's value */ export declare function useValue(atom: useAtomType | Atom | Selector): R; export declare const useAtomValue: typeof useValue; /** * Get the function that updates the atom's value */ export declare function useDispatch(atom: useAtomType | Atom): (cb: ((c: R) => R) | R) => void; export declare const useAtomDispatch: typeof useDispatch; /** * Get the actions of the atom as reducers */ export declare function useActions(atom: useAtomType | Atom): Required>; /** * Get an atom's value and actions */ export declare function useValueAndActions(atom: useAtomType | Atom): readonly [R, ActionsObjectType]; export declare const useAtomActions: typeof useActions; /** * Create a single provider hook with atoms */ export declare function atomProvider(states: { [e in keyof R]: Atom; }): { (name: E): [R[E], Dispatch>, ActionsObjectType<{ [x: string]: /*elided*/ any; }>] & { value: R[E]; dispatch: Dispatch>; actions: ActionsObjectType<{ [x: string]: /*elided*/ any; }>; }; stores: { [e in keyof R]: Atom; }; value(name: E): R[E] | Promise | (() => Promise) | (() => R[E]); dispatch(name: E): (cb: R[E] | ((c: R[E]) => R[E])) => void; actions(name: E): Required>; }; export declare const stateProvider: typeof atomProvider; /** * Get all localStorage items as an object (they will be JSON parsed). You can pass default values (which work with SSR) and a type argument */ export declare function useStorage(defaults?: K): K; /** * A sync wrapper around the `localStorage` API */ export declare const storage: { /** * Set an item in `localStorage`. Its value will be serialized as JSON */ set(k: string, v: T): void; /** * Remove a `localStorage` item */ remove(k: string): Promise; /** * Get an item in `localStorage`. Its value will be JSON parsed. If it does not exist or * is an invalid JSON format, the default value passed in the second argument will be returned */ get(k: string, def?: T): T; }; /** * A sync wrapper around the `sessionStorage` API */ export declare const session: { /** * Set an item in `sessionStorage`. Its value will be serialized as JSON */ set(k: string, v: T): void; /** * Remove a `sessionStorage` item */ remove(k: string): Promise; /** * Get an item in `sessionStorage`. Its value will be JSON parsed. If it does not exist or * is an invalid JSON format, the default value passed in the second argument will be returned */ get(k: string, def?: T): T; }; /** * Get a localStorage item. Whenever `storage.set` or `storage.remove` are called, * this hook will update its state */ export declare function useStorageItem(k: string, def?: T): T; export {};