UNPKG

4.22 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 StoreListenerRev1 = (type: 'state' | 'sub' | 'unsub') => void;
42type StoreListenerRev2 = (action: {
43 type: 'write';
44 flushed: Set<AnyAtom>;
45} | {
46 type: 'async-write';
47 flushed: Set<AnyAtom>;
48} | {
49 type: 'sub';
50 flushed: Set<AnyAtom>;
51} | {
52 type: 'unsub';
53} | {
54 type: 'restore';
55 flushed: Set<AnyAtom>;
56}) => void;
57type DevSubscribeStore = {
58 /**
59 * @deprecated use StoreListenerRev2
60 */
61 (listener: StoreListenerRev1): () => void;
62 (listener: StoreListenerRev2, rev: 2): () => void;
63};
64/**
65 * Create a new store. Each store is an independent, isolated universe of atom
66 * states.
67 *
68 * Jotai atoms are not themselves state containers. When you read or write an
69 * atom, that state is stored in a store. You can think of a Store like a
70 * multi-layered map from atoms to states, like this:
71 *
72 * ```
73 * // Conceptually, a Store is a map from atoms to states.
74 * // The real type is a bit different.
75 * type Store = Map<VersionObject, Map<Atom, AtomState>>
76 * ```
77 *
78 * @returns A store.
79 */
80export declare const createStore: () => {
81 get: <Value>(atom: Atom<Value>) => Value;
82 set: <Value_1, Args extends unknown[], Result>(atom: WritableAtom<Value_1, Args, Result>, ...args: Args) => Result;
83 sub: (atom: AnyAtom, listener: () => void) => () => void;
84 dev_subscribe_store: DevSubscribeStore;
85 dev_get_mounted_atoms: () => IterableIterator<AnyAtom>;
86 dev_get_atom_state: (a: AnyAtom) => AtomState<unknown> | undefined;
87 dev_get_mounted: (a: AnyAtom) => Mounted | undefined;
88 dev_restore_atoms: (values: Iterable<readonly [AnyAtom, AnyValue]>) => void;
89} | {
90 get: <Value>(atom: Atom<Value>) => Value;
91 set: <Value_1, Args extends unknown[], Result>(atom: WritableAtom<Value_1, Args, Result>, ...args: Args) => Result;
92 sub: (atom: AnyAtom, listener: () => void) => () => void;
93 dev_subscribe_store?: never;
94 dev_get_mounted_atoms?: never;
95 dev_get_atom_state?: never;
96 dev_get_mounted?: never;
97 dev_restore_atoms?: never;
98};
99export declare const getDefaultStore: () => {
100 get: <Value>(atom: Atom<Value>) => Value;
101 set: <Value_1, Args extends unknown[], Result>(atom: WritableAtom<Value_1, Args, Result>, ...args: Args) => Result;
102 sub: (atom: AnyAtom, listener: () => void) => () => void;
103 dev_subscribe_store: DevSubscribeStore;
104 dev_get_mounted_atoms: () => IterableIterator<AnyAtom>;
105 dev_get_atom_state: (a: AnyAtom) => AtomState<unknown> | undefined;
106 dev_get_mounted: (a: AnyAtom) => Mounted | undefined;
107 dev_restore_atoms: (values: Iterable<readonly [AnyAtom, AnyValue]>) => void;
108} | {
109 get: <Value>(atom: Atom<Value>) => Value;
110 set: <Value_1, Args extends unknown[], Result>(atom: WritableAtom<Value_1, Args, Result>, ...args: Args) => Result;
111 sub: (atom: AnyAtom, listener: () => void) => () => void;
112 dev_subscribe_store?: never;
113 dev_get_mounted_atoms?: never;
114 dev_get_atom_state?: never;
115 dev_get_mounted?: never;
116 dev_restore_atoms?: never;
117};
118export {};