UNPKG

2 kBTypeScriptView Raw
1import type { Atom, WritableAtom } from './atom';
2declare type AnyAtom = Atom<unknown>;
3declare type OnUnmount = () => void;
4declare type NonPromise<T> = T extends Promise<infer V> ? V : T;
5declare const IS_EQUAL_PROMISE: unique symbol;
6declare const INTERRUPT_PROMISE: unique symbol;
7declare type InterruptablePromise = Promise<void> & {
8 [IS_EQUAL_PROMISE]: (p: Promise<void>) => boolean;
9 [INTERRUPT_PROMISE]: () => void;
10};
11declare type Revision = number;
12declare type InvalidatedRevision = number;
13declare type ReadDependencies = Map<AnyAtom, Revision>;
14export declare type AtomState<Value = unknown> = {
15 e?: Error;
16 p?: InterruptablePromise;
17 c?: () => void;
18 w?: Promise<void>;
19 v?: NonPromise<Value>;
20 r: Revision;
21 i?: InvalidatedRevision;
22 d: ReadDependencies;
23};
24declare type AtomStateMap = WeakMap<AnyAtom, AtomState>;
25declare type Listeners = Set<() => void>;
26declare type Dependents = Set<AnyAtom>;
27declare type Mounted = {
28 l: Listeners;
29 d: Dependents;
30 u: OnUnmount | void;
31};
32declare type MountedMap = WeakMap<AnyAtom, Mounted>;
33declare type StateListener = (updatedAtom: AnyAtom, isNewAtom: boolean) => void;
34declare type StateVersion = number;
35declare type PendingAtoms = Set<AnyAtom>;
36export declare type State = {
37 l?: StateListener;
38 v: StateVersion;
39 a: AtomStateMap;
40 m: MountedMap;
41 p: PendingAtoms;
42};
43export declare const createState: (initialValues?: Iterable<readonly [AnyAtom, unknown]> | undefined, stateListener?: StateListener | undefined) => State;
44export declare const readAtom: <Value>(state: State, readingAtom: Atom<Value>) => AtomState<Value>;
45export declare const writeAtom: <Value, Update>(state: State, writingAtom: WritableAtom<Value, Update>, update: Update) => void | Promise<void>;
46export declare const subscribeAtom: (state: State, atom: AnyAtom, callback: () => void) => () => void;
47export declare const restoreAtoms: (state: State, values: Iterable<readonly [AnyAtom, unknown]>) => void;
48export {};