UNPKG

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