UNPKG

2.76 kBTypeScriptView Raw
1import type { WritableAtom } from 'jotai';
2import { unstable_NO_STORAGE_VALUE as NO_STORAGE_VALUE } from 'jotai/vanilla/utils';
3import { RESET } from './constants';
4export { unstable_NO_STORAGE_VALUE as NO_STORAGE_VALUE } from 'jotai/vanilla/utils';
5type Unsubscribe = () => void;
6type SetStateActionWithReset<Value> = Value | typeof RESET | ((prev: Value) => Value | typeof RESET);
7export interface AsyncStorage<Value> {
8 getItem: (key: string) => Promise<Value | typeof NO_STORAGE_VALUE>;
9 setItem: (key: string, newValue: Value) => Promise<void>;
10 removeItem: (key: string) => Promise<void>;
11 delayInit?: boolean;
12 subscribe?: (key: string, callback: (value: Value) => void) => Unsubscribe;
13}
14export interface SyncStorage<Value> {
15 getItem: (key: string) => Value | typeof NO_STORAGE_VALUE;
16 setItem: (key: string, newValue: Value) => void;
17 removeItem: (key: string) => void;
18 delayInit?: boolean;
19 subscribe?: (key: string, callback: (value: Value) => void) => Unsubscribe;
20}
21export interface AsyncStringStorage {
22 getItem: (key: string) => Promise<string | null>;
23 setItem: (key: string, newValue: string) => Promise<void>;
24 removeItem: (key: string) => Promise<void>;
25}
26export interface SyncStringStorage {
27 getItem: (key: string) => string | null;
28 setItem: (key: string, newValue: string) => void;
29 removeItem: (key: string) => void;
30}
31export declare function createJSONStorage<Value>(getStringStorage: () => AsyncStringStorage): AsyncStorage<Value>;
32export declare function createJSONStorage<Value>(getStringStorage: () => SyncStringStorage): SyncStorage<Value>;
33export declare function atomWithStorage<Value>(key: string, initialValue: Value, storage: AsyncStorage<Value> & {
34 delayInit: true;
35}): WritableAtom<Value, SetStateActionWithReset<Value>, Promise<void>>;
36export declare function atomWithStorage<Value>(key: string, initialValue: Value, storage: AsyncStorage<Value>): WritableAtom<Promise<Value>, SetStateActionWithReset<Value>, Promise<void>>;
37export declare function atomWithStorage<Value>(key: string, initialValue: Value, storage: SyncStorage<Value>): WritableAtom<Value, SetStateActionWithReset<Value>>;
38export declare function atomWithStorage<Value>(key: string, initialValue: Value): WritableAtom<Value, SetStateActionWithReset<Value>>;
39/**
40 * @deprecated Please `import { atomWithHash } from 'jotai-location'`
41 */
42export declare function atomWithHash<Value>(key: string, initialValue: Value, options?: {
43 serialize?: (val: Value) => string;
44 deserialize?: (str: string | null) => Value | typeof NO_STORAGE_VALUE;
45 delayInit?: boolean;
46 replaceState?: boolean;
47 subscribe?: (callback: () => void) => () => void;
48}): WritableAtom<Value, SetStateActionWithReset<Value>>;