UNPKG

8.39 kBTypeScriptView Raw
1import type { Atom, WritableAtom } from './atom';
2import type { SuspensePromise } from './suspensePromise';
3declare type Awaited<T> = T extends Promise<infer V> ? Awaited<V> : T;
4declare type AnyAtomValue = unknown;
5declare type AnyAtom = Atom<AnyAtomValue>;
6declare type OnUnmount = () => void;
7declare type ReadError = unknown;
8declare type Revision = number;
9declare type ReadDependencies = Map<AnyAtom, Revision>;
10/**
11 * Immutable atom state, tracked for both mounted and unmounted atoms in a store.
12 * @private This is for internal use and not considered part of the public API.
13 */
14export declare type AtomState<Value = AnyAtomValue> = {
15 /**
16 * Counts number of times atom has actually changed or recomputed.
17 */
18 r: Revision;
19 /**
20 * Validit(y) of the atom state.
21 * Mounted atoms are considered invalidated when `y === false`.
22 */
23 y: boolean;
24 /**
25 * Maps from a dependency to the dependency's revision when it was last read.
26 * We can skip recomputation of an atom by comparing the ReadDependencies revision
27 * of each dependency to that dependencies's current revision.
28 */
29 d: ReadDependencies;
30} & ({
31 e: ReadError;
32} | {
33 p: SuspensePromise;
34} | {
35 v: Awaited<Value>;
36});
37/**
38 * Represents a version of a store. A version contains a state for every atom
39 * read during the version's lifetime.
40 *
41 * In concurrent React rendering, state can "branch" during transitions: the
42 * current state may continue to be rendered while a new state is being built
43 * concurrently. We key our atom state containers by this global version to
44 * represent the state for each diverging branch.
45 *
46 * While a new version is being built, we read atom previous state from the
47 * previous version.
48 *
49 * This is an INTERNAL type alias.
50 */
51export declare type VersionObject = {
52 /**
53 * "p"arent version.
54 *
55 * Once a version is committed completely, the `p` property is deleted so the
56 * child version is independent, and the parent version can be garbage
57 * collected.
58 *
59 * See [Provider] for more details on version data flow.
60 */
61 p?: VersionObject;
62};
63declare type Listeners = Set<(version?: VersionObject) => void>;
64declare type Dependents = Set<AnyAtom>;
65/**
66 * State tracked for mounted atoms. An atom is considered "mounted" if it has a
67 * subscriber, or is a transitive dependency of another atom that has a
68 * subscriber.
69 *
70 * The mounted state of an atom is freed once it is no longer mounted.
71 */
72declare type Mounted = {
73 /** The list of subscriber functions. */
74 l: Listeners;
75 /** Atoms that depend on *this* atom. Used to fan out invalidation. */
76 t: Dependents;
77 /** Function to run when the atom is unmounted. */
78 u?: OnUnmount;
79};
80declare type StateListener = () => void;
81/**
82 * Read an atom's [AtomState], an internal data structure that is not considered
83 * part of the public API. See [useAtom] for more details.
84 *
85 * Derived atom states may be recomputed if they are invalidated and any of
86 * their transitive dependencies have changed.
87 */
88export declare const READ_ATOM = "r";
89/**
90 * Invoke an atom's [WritableAtom.write] method with an update value.
91 * That `write` method may set one or more atoms.
92 * The default `write` method of primitive atoms just sets the atom itself to
93 * the update value.
94 */
95export declare const WRITE_ATOM = "w";
96/**
97 * Commit pending writes to an atom.
98 * (The current implementation commits pending writes to all atoms; this is subject to change.)
99 */
100export declare const COMMIT_ATOM = "c";
101/**
102 * Add a subscriber function to an atom. Returns a function that removes the
103 * subscriber.
104 *
105 * The subscriber is called in two cases:
106 *
107 * - For writable atoms, the subscriber is called whenever the atom is directly
108 * changed by `atom.write`.
109 * - For derived atoms, the subscriber is called whenever the atom is
110 * *invalidated* (i.e. when it's possibly transitive dependencies change or
111 * become invalidated), **not** when the actual Value of the atom changes.
112 * Derived atoms are only recomputed on read.
113 */
114export declare const SUBSCRIBE_ATOM = "s";
115/**
116 * Bulk-apply new values to atoms.
117 */
118export declare const RESTORE_ATOMS = "h";
119export declare const DEV_SUBSCRIBE_STATE = "n";
120export declare const DEV_GET_MOUNTED_ATOMS = "l";
121export declare const DEV_GET_ATOM_STATE = "a";
122export declare const DEV_GET_MOUNTED = "m";
123/**
124 * Create a new store. Each store is an independent, isolated universe of atom
125 * states.
126 *
127 * Jotai atoms are not themselves state containers. When you read or write an
128 * atom, that state is stored in a store. You can think of a Store like a
129 * multi-layered map from atoms to states, like this:
130 *
131 * ```
132 * // Conceptually, a Store is a map from atoms to states.
133 * // The real type is a bit different.
134 * type Store = Map<VersionObject, Map<Atom, AtomState>>
135 * ```
136 *
137 * @param initialValues An iterable where item is a pair of [an atom, its
138 * initial value]. Use to set initial state of writable atoms; useful for
139 * testing.
140 *
141 * @returns A store.
142 */
143export declare const createStore: (initialValues?: Iterable<readonly [AnyAtom, AnyAtomValue]>) => {
144 r: <Value>(readingAtom: Atom<Value>, version?: VersionObject) => AtomState<Value>;
145 w: <Value_1, Update, Result extends void | Promise<void>>(writingAtom: WritableAtom<Value_1, Update, Result>, update: Update, version?: VersionObject) => Result;
146 c: (_atom: AnyAtom | null, version?: VersionObject) => void;
147 s: (atom: AnyAtom, callback: (version?: VersionObject) => void, version?: VersionObject) => () => void;
148 h: (values: Iterable<readonly [AnyAtom, AnyAtomValue]>, version?: VersionObject) => void;
149 n: (l: StateListener) => () => void;
150 l: () => IterableIterator<AnyAtom>;
151 a: (a: AnyAtom) => AtomState<unknown> | undefined;
152 m: (a: AnyAtom) => Mounted | undefined;
153} | {
154 r: <Value>(readingAtom: Atom<Value>, version?: VersionObject) => AtomState<Value>;
155 w: <Value_1, Update, Result extends void | Promise<void>>(writingAtom: WritableAtom<Value_1, Update, Result>, update: Update, version?: VersionObject) => Result;
156 c: (_atom: AnyAtom | null, version?: VersionObject) => void;
157 s: (atom: AnyAtom, callback: (version?: VersionObject) => void, version?: VersionObject) => () => void;
158 h: (values: Iterable<readonly [AnyAtom, AnyAtomValue]>, version?: VersionObject) => void;
159 n?: never;
160 l?: never;
161 a?: never;
162 m?: never;
163};
164export declare type Store = ReturnType<typeof createStore>;
165export declare const createStoreForExport: (initialValues?: Iterable<readonly [AnyAtom, AnyAtomValue]>) => {
166 get: <Value>(atom: Atom<Value>) => Awaited<Value> | undefined;
167 asyncGet: <Value_1>(atom: Atom<Value_1>) => Promise<Awaited<Value_1>>;
168 set: <Value_2, Update, Result extends void | Promise<void>>(atom: WritableAtom<Value_2, Update, Result>, update: Update) => Result;
169 sub: (atom: AnyAtom, callback: () => void) => () => void;
170 SECRET_INTERNAL_store: {
171 r: <Value>(readingAtom: Atom<Value>, version?: VersionObject) => AtomState<Value>;
172 w: <Value_1, Update, Result extends void | Promise<void>>(writingAtom: WritableAtom<Value_1, Update, Result>, update: Update, version?: VersionObject) => Result;
173 c: (_atom: AnyAtom | null, version?: VersionObject) => void;
174 s: (atom: AnyAtom, callback: (version?: VersionObject) => void, version?: VersionObject) => () => void;
175 h: (values: Iterable<readonly [AnyAtom, AnyAtomValue]>, version?: VersionObject) => void;
176 n: (l: StateListener) => () => void;
177 l: () => IterableIterator<AnyAtom>;
178 a: (a: AnyAtom) => AtomState<unknown> | undefined;
179 m: (a: AnyAtom) => Mounted | undefined;
180 } | {
181 r: <Value>(readingAtom: Atom<Value>, version?: VersionObject) => AtomState<Value>;
182 w: <Value_1, Update, Result extends void | Promise<void>>(writingAtom: WritableAtom<Value_1, Update, Result>, update: Update, version?: VersionObject) => Result;
183 c: (_atom: AnyAtom | null, version?: VersionObject) => void;
184 s: (atom: AnyAtom, callback: (version?: VersionObject) => void, version?: VersionObject) => () => void;
185 h: (values: Iterable<readonly [AnyAtom, AnyAtomValue]>, version?: VersionObject) => void;
186 n?: never;
187 l?: never;
188 a?: never;
189 m?: never;
190 };
191};
192export {};