UNPKG

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