UNPKG

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