UNPKG

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