UNPKG

3.75 kBTypeScriptView Raw
1import type { Atom, WritableAtom } from './atom';
2type AnyValue = unknown;
3type AnyError = unknown;
4type AnyAtom = Atom<AnyValue>;
5type OnUnmount = () => void;
6/**
7 * Immutable map from a dependency to the dependency's atom state
8 * when it was last read.
9 * We can skip recomputation of an atom by comparing the atom state
10 * of each dependency to that dependencies's current revision.
11 */
12type Dependencies = Map<AnyAtom, AtomState>;
13/**
14 * Immutable atom state,
15 * tracked for both mounted and unmounted atoms in a store.
16 */
17type AtomState<Value = AnyValue> = {
18 d: Dependencies;
19} & ({
20 e: AnyError;
21} | {
22 v: Value;
23});
24type Listeners = Set<() => void>;
25type Dependents = Set<AnyAtom>;
26/**
27 * State tracked for mounted atoms. An atom is considered "mounted" if it has a
28 * subscriber, or is a transitive dependency of another atom that has a
29 * subscriber.
30 *
31 * The mounted state of an atom is freed once it is no longer mounted.
32 */
33type Mounted = {
34 /** The list of subscriber functions. */
35 l: Listeners;
36 /** Atoms that depend on *this* atom. Used to fan out invalidation. */
37 t: Dependents;
38 /** Function to run when the atom is unmounted. */
39 u?: OnUnmount;
40};
41type StateListener = () => void;
42/**
43 * Create a new store. Each store is an independent, isolated universe of atom
44 * states.
45 *
46 * Jotai atoms are not themselves state containers. When you read or write an
47 * atom, that state is stored in a store. You can think of a Store like a
48 * multi-layered map from atoms to states, like this:
49 *
50 * ```
51 * // Conceptually, a Store is a map from atoms to states.
52 * // The real type is a bit different.
53 * type Store = Map<VersionObject, Map<Atom, AtomState>>
54 * ```
55 *
56 * @returns A store.
57 */
58export declare const createStore: () => {
59 get: <Value>(atom: Atom<Value>) => Value;
60 set: <Value_1, Args extends unknown[], Result>(atom: WritableAtom<Value_1, Args, Result>, ...args: Args) => Result;
61 sub: (atom: AnyAtom, listener: () => void) => () => void;
62 dev_subscribe_state: (l: StateListener) => () => void;
63 dev_get_mounted_atoms: () => IterableIterator<AnyAtom>;
64 dev_get_atom_state: (a: AnyAtom) => AtomState<unknown> | undefined;
65 dev_get_mounted: (a: AnyAtom) => Mounted | undefined;
66 dev_restore_atoms: (values: Iterable<readonly [AnyAtom, AnyValue]>) => void;
67} | {
68 get: <Value>(atom: Atom<Value>) => Value;
69 set: <Value_1, Args extends unknown[], Result>(atom: WritableAtom<Value_1, Args, Result>, ...args: Args) => Result;
70 sub: (atom: AnyAtom, listener: () => void) => () => void;
71 dev_subscribe_state?: never;
72 dev_get_mounted_atoms?: never;
73 dev_get_atom_state?: never;
74 dev_get_mounted?: never;
75 dev_restore_atoms?: never;
76};
77export declare const getDefaultStore: () => {
78 get: <Value>(atom: Atom<Value>) => Value;
79 set: <Value_1, Args extends unknown[], Result>(atom: WritableAtom<Value_1, Args, Result>, ...args: Args) => Result;
80 sub: (atom: AnyAtom, listener: () => void) => () => void;
81 dev_subscribe_state: (l: StateListener) => () => void;
82 dev_get_mounted_atoms: () => IterableIterator<AnyAtom>;
83 dev_get_atom_state: (a: AnyAtom) => AtomState<unknown> | undefined;
84 dev_get_mounted: (a: AnyAtom) => Mounted | undefined;
85 dev_restore_atoms: (values: Iterable<readonly [AnyAtom, AnyValue]>) => void;
86} | {
87 get: <Value>(atom: Atom<Value>) => Value;
88 set: <Value_1, Args extends unknown[], Result>(atom: WritableAtom<Value_1, Args, Result>, ...args: Args) => Result;
89 sub: (atom: AnyAtom, listener: () => void) => () => void;
90 dev_subscribe_state?: never;
91 dev_get_mounted_atoms?: never;
92 dev_get_atom_state?: never;
93 dev_get_mounted?: never;
94 dev_restore_atoms?: never;
95};
96export {};