UNPKG

1.99 kBTypeScriptView Raw
1import type { WritableAtom } from 'jotai/vanilla';
2import { RESET } from './constants';
3type Unsubscribe = () => void;
4type SetStateActionWithReset<Value> = Value | typeof RESET | ((prev: Value) => Value | typeof RESET);
5export interface AsyncStorage<Value> {
6 getItem: (key: string, initialValue: Value) => PromiseLike<Value>;
7 setItem: (key: string, newValue: Value) => PromiseLike<void>;
8 removeItem: (key: string) => PromiseLike<void>;
9 subscribe?: (key: string, callback: (value: Value) => void, initialValue: Value) => Unsubscribe;
10}
11export interface SyncStorage<Value> {
12 getItem: (key: string, initialValue: Value) => Value;
13 setItem: (key: string, newValue: Value) => void;
14 removeItem: (key: string) => void;
15 subscribe?: (key: string, callback: (value: Value) => void, initialValue: Value) => Unsubscribe;
16}
17export interface AsyncStringStorage {
18 getItem: (key: string) => PromiseLike<string | null>;
19 setItem: (key: string, newValue: string) => PromiseLike<void>;
20 removeItem: (key: string) => PromiseLike<void>;
21}
22export interface SyncStringStorage {
23 getItem: (key: string) => string | null;
24 setItem: (key: string, newValue: string) => void;
25 removeItem: (key: string) => void;
26}
27export declare function createJSONStorage<Value>(getStringStorage: () => AsyncStringStorage): AsyncStorage<Value>;
28export declare function createJSONStorage<Value>(getStringStorage: () => SyncStringStorage): SyncStorage<Value>;
29export declare function atomWithStorage<Value>(key: string, initialValue: Value, storage: AsyncStorage<Value>, unstable_options?: {
30 unstable_getOnInit?: boolean;
31}): WritableAtom<Value | Promise<Value>, [
32 SetStateActionWithReset<Value | Promise<Value>>
33], Promise<void>>;
34export declare function atomWithStorage<Value>(key: string, initialValue: Value, storage?: SyncStorage<Value>, unstable_options?: {
35 unstable_getOnInit?: boolean;
36}): WritableAtom<Value, [SetStateActionWithReset<Value>], void>;
37export {};