UNPKG

jotai

Version:

👻 Next gen state management that will spook you

46 lines (45 loc) • 2.6 kB
import type { WritableAtom } from 'jotai'; import { RESET } from './constants'; export declare const NO_STORAGE_VALUE: unique symbol; declare type Unsubscribe = () => void; declare type SetStateActionWithReset<Value> = Value | typeof RESET | ((prev: Value) => Value | typeof RESET); export interface AsyncStorage<Value> { getItem: (key: string) => Promise<Value | typeof NO_STORAGE_VALUE>; setItem: (key: string, newValue: Value) => Promise<void>; removeItem: (key: string) => Promise<void>; delayInit?: boolean; subscribe?: (key: string, callback: (value: Value) => void) => Unsubscribe; } export interface SyncStorage<Value> { getItem: (key: string) => Value | typeof NO_STORAGE_VALUE; setItem: (key: string, newValue: Value) => void; removeItem: (key: string) => void; delayInit?: boolean; subscribe?: (key: string, callback: (value: Value) => void) => Unsubscribe; } export interface AsyncStringStorage { getItem: (key: string) => Promise<string | null>; setItem: (key: string, newValue: string) => Promise<void>; removeItem: (key: string) => Promise<void>; } export interface SyncStringStorage { getItem: (key: string) => string | null; setItem: (key: string, newValue: string) => void; removeItem: (key: string) => void; } export declare function createJSONStorage<Value>(getStringStorage: () => AsyncStringStorage): AsyncStorage<Value>; export declare function createJSONStorage<Value>(getStringStorage: () => SyncStringStorage): SyncStorage<Value>; export declare function atomWithStorage<Value>(key: string, initialValue: Value, storage: AsyncStorage<Value> & { delayInit: true; }): WritableAtom<Value, SetStateActionWithReset<Value>, Promise<void>>; export declare function atomWithStorage<Value>(key: string, initialValue: Value, storage: AsyncStorage<Value>): WritableAtom<Promise<Value>, SetStateActionWithReset<Value>, Promise<void>>; export declare function atomWithStorage<Value>(key: string, initialValue: Value, storage: SyncStorage<Value>): WritableAtom<Value, SetStateActionWithReset<Value>>; export declare function atomWithStorage<Value>(key: string, initialValue: Value): WritableAtom<Value, SetStateActionWithReset<Value>>; export declare function atomWithHash<Value>(key: string, initialValue: Value, options?: { serialize?: (val: Value) => string; deserialize?: (str: string | null) => Value | typeof NO_STORAGE_VALUE; delayInit?: boolean; replaceState?: boolean; subscribe?: (callback: () => void) => () => void; }): WritableAtom<Value, SetStateActionWithReset<Value>>; export {};