UNPKG

1.67 kBTypeScriptView Raw
1import type { WritableAtom } from 'jotai/vanilla';
2import { RESET } from './constants';
3export declare const NO_STORAGE_VALUE: unique symbol;
4type Unsubscribe = () => void;
5type 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 subscribe?: (key: string, callback: (value: Value) => void) => Unsubscribe;
11}
12export interface SyncStorage<Value> {
13 getItem: (key: string) => Value | typeof NO_STORAGE_VALUE;
14 setItem: (key: string, newValue: Value) => void;
15 removeItem: (key: string) => void;
16 subscribe?: (key: string, callback: (value: Value) => void) => Unsubscribe;
17}
18export interface AsyncStringStorage {
19 getItem: (key: string) => Promise<string | null>;
20 setItem: (key: string, newValue: string) => Promise<void>;
21 removeItem: (key: string) => Promise<void>;
22}
23export interface SyncStringStorage {
24 getItem: (key: string) => string | null;
25 setItem: (key: string, newValue: string) => void;
26 removeItem: (key: string) => void;
27}
28export declare function createJSONStorage<Value>(getStringStorage: () => AsyncStringStorage): AsyncStorage<Value>;
29export declare function createJSONStorage<Value>(getStringStorage: () => SyncStringStorage): SyncStorage<Value>;
30export declare function atomWithStorage<Value>(key: string, initialValue: Value, storage?: SyncStorage<Value> | AsyncStorage<Value>): WritableAtom<Value, [SetStateActionWithReset<Value>], void>;
31export {};