import type { Atom, WritableAtom } from './atom'; type AnyValue = unknown; type AnyError = unknown; type AnyAtom = Atom; type AnyWritableAtom = WritableAtom; type OnUnmount = () => void; type EpochNumber = number; /** * Mutable atom state, * tracked for both mounted and unmounted atoms in a store. * * This should be garbage collectable. * We can mutate it during atom read. (except for fields with TODO) */ type AtomState = { /** * Map of atoms that the atom depends on. * The map value is the epoch number of the dependency. */ readonly d: Map; /** * Set of atoms with pending promise that depend on the atom. * * This may cause memory leaks, but it's for the capability to continue promises * TODO(daishi): revisit how to handle this */ readonly p: Set; /** The epoch number of the atom. */ n: EpochNumber; /** Atom value */ v?: Value; /** Atom error */ e?: AnyError; }; /** * State tracked for mounted atoms. An atom is considered "mounted" if it has a * subscriber, or is a transitive dependency of another atom that has a * subscriber. * The mounted state of an atom is freed once it is no longer mounted. */ type Mounted = { /** Set of listeners to notify when the atom value changes. */ readonly l: Set<() => void>; /** Set of mounted atoms that the atom depends on. */ readonly d: Set; /** Set of mounted atoms that depends on the atom. */ readonly t: Set; /** Function to run when the atom is unmounted. */ u?: () => void; }; type AtomStateMap = { get(atom: AnyAtom): AtomState | undefined; set(atom: AnyAtom, atomState: AtomState): void; }; type MountedMap = { get(atom: AnyAtom): Mounted | undefined; has(atom: AnyAtom): boolean; set(atom: AnyAtom, mounted: Mounted): void; delete(atom: AnyAtom): void; }; type InvalidatedAtoms = { get(atom: AnyAtom): EpochNumber | undefined; has(atom: AnyAtom): boolean; set(atom: AnyAtom, n: EpochNumber): void; delete(atom: AnyAtom): void; }; type ChangedAtoms = { readonly size: number; add(atom: AnyAtom): void; has(atom: AnyAtom): boolean; clear(): void; forEach(callback: (atom: AnyAtom) => void): void; [Symbol.iterator](): IterableIterator; }; type Callbacks = { readonly size: number; add(fn: () => void): void; clear(): void; forEach(callback: (fn: () => void) => void): void; }; type AtomRead = (store: Store, atom: Atom, ...params: Parameters['read']>) => Value; type AtomWrite = (store: Store, atom: WritableAtom, ...params: Parameters['write']>) => Result; type AtomOnInit = (store: Store, atom: Atom) => void; type AtomOnMount = (store: Store, atom: WritableAtom, setAtom: (...args: Args) => Result) => OnUnmount | void; type EnsureAtomState = (store: Store, atom: Atom) => AtomState; type FlushCallbacks = (store: Store) => void; type RecomputeInvalidatedAtoms = (store: Store) => void; type ReadAtomState = (store: Store, atom: Atom) => AtomState; type InvalidateDependents = (store: Store, atom: AnyAtom) => void; type WriteAtomState = (store: Store, atom: WritableAtom, ...args: Args) => Result; type MountDependencies = (store: Store, atom: AnyAtom) => void; type MountAtom = (store: Store, atom: Atom) => Mounted; type UnmountAtom = (store: Store, atom: Atom) => Mounted | undefined; type SetAtomStateValueOrPromise = (store: Store, atom: Atom, valueOrPromise: Value) => void; type StoreGet = (store: Store, atom: Atom) => Value; type StoreSet = (store: Store, atom: WritableAtom, ...args: Args) => Result; type StoreSub = (store: Store, atom: AnyAtom, listener: () => void) => () => void; type EnhanceBuildingBlocks = (buildingBlocks: Readonly) => Readonly; type Store = { get: (atom: Atom) => Value; set: (atom: WritableAtom, ...args: Args) => Result; sub: (atom: AnyAtom, listener: () => void) => () => void; }; type BuildingBlocks = [ /*atomStateMap*/ AtomStateMap, /*mountedMap*/ MountedMap, /*invalidatedAtoms*/ InvalidatedAtoms, /*changedAtoms*/ ChangedAtoms, /*mountCallbacks*/ Callbacks, /*unmountCallbacks*/ Callbacks, /*storeHooks*/ StoreHooks, /*atomRead*/ AtomRead, /*atomWrite*/ AtomWrite, /*atomOnInit*/ AtomOnInit, /*atomOnMount*/ AtomOnMount, /*ensureAtomState*/ EnsureAtomState, /*flushCallbacks*/ FlushCallbacks, /*recomputeInvalidatedAtoms*/ RecomputeInvalidatedAtoms, /*readAtomState*/ ReadAtomState, /*invalidateDependents*/ InvalidateDependents, /*writeAtomState*/ WriteAtomState, /*mountDependencies*/ MountDependencies, /*mountAtom*/ MountAtom, /*unmountAtom*/ UnmountAtom, /*setAtomStateValueOrPromise*/ SetAtomStateValueOrPromise, /*storeGet*/ StoreGet, /*storeSet*/ StoreSet, /*storeSub*/ StoreSub, /*enhanceBuildingBlocks*/ EnhanceBuildingBlocks | undefined ]; export type { AtomState as INTERNAL_AtomState, Mounted as INTERNAL_Mounted, AtomStateMap as INTERNAL_AtomStateMap, MountedMap as INTERNAL_MountedMap, InvalidatedAtoms as INTERNAL_InvalidatedAtoms, ChangedAtoms as INTERNAL_ChangedAtoms, Callbacks as INTERNAL_Callbacks, AtomRead as INTERNAL_AtomRead, AtomWrite as INTERNAL_AtomWrite, AtomOnInit as INTERNAL_AtomOnInit, AtomOnMount as INTERNAL_AtomOnMount, EnsureAtomState as INTERNAL_EnsureAtomState, FlushCallbacks as INTERNAL_FlushCallbacks, RecomputeInvalidatedAtoms as INTERNAL_RecomputeInvalidatedAtoms, ReadAtomState as INTERNAL_ReadAtomState, InvalidateDependents as INTERNAL_InvalidateDependents, WriteAtomState as INTERNAL_WriteAtomState, MountDependencies as INTERNAL_MountDependencies, MountAtom as INTERNAL_MountAtom, UnmountAtom as INTERNAL_UnmountAtom, Store as INTERNAL_Store, BuildingBlocks as INTERNAL_BuildingBlocks, StoreHooks as INTERNAL_StoreHooks, }; declare function hasInitialValue>(atom: T): atom is T & (T extends Atom ? { init: Value; } : never); declare function isActuallyWritableAtom(atom: AnyAtom): atom is AnyWritableAtom; declare function isAtomStateInitialized(atomState: AtomState): boolean; declare function returnAtomValue(atomState: AtomState): Value; declare const promiseStateMap: WeakMap, [ /*pending*/ boolean, /*abortHandlers*/ Set<() => void> ]>; declare function isPendingPromise(value: unknown): value is PromiseLike; declare function abortPromise(promise: PromiseLike): void; declare function registerAbortHandler(promise: PromiseLike, abortHandler: () => void): void; declare function isPromiseLike(p: unknown): p is PromiseLike; declare function addPendingPromiseToDependency(atom: AnyAtom, promise: PromiseLike, dependencyAtomState: AtomState): void; declare function getMountedOrPendingDependents(atom: AnyAtom, atomState: AtomState, mountedMap: MountedMap): Set; type StoreHook = { (): void; add(callback: () => void): () => void; }; type StoreHookForAtoms = { (atom: AnyAtom): void; add(atom: AnyAtom, callback: () => void): () => void; add(atom: undefined, callback: (atom: AnyAtom) => void): () => void; }; /** StoreHooks are an experimental API. */ type StoreHooks = { /** Listener to notify when the atom is read. */ readonly r?: StoreHookForAtoms; /** Listener to notify when the atom value is changed. */ readonly c?: StoreHookForAtoms; /** Listener to notify when the atom is mounted. */ readonly m?: StoreHookForAtoms; /** Listener to notify when the atom is unmounted. */ readonly u?: StoreHookForAtoms; /** Listener to notify when callbacks are being flushed. */ readonly f?: StoreHook; }; declare function initializeStoreHooks(storeHooks: StoreHooks): Required; declare function getBuildingBlocks(store: Store): Readonly; declare function buildStore(...buildArgs: Partial): Store; export { buildStore as INTERNAL_buildStoreRev2, getBuildingBlocks as INTERNAL_getBuildingBlocksRev2, initializeStoreHooks as INTERNAL_initializeStoreHooksRev2, hasInitialValue as INTERNAL_hasInitialValue, isActuallyWritableAtom as INTERNAL_isActuallyWritableAtom, isAtomStateInitialized as INTERNAL_isAtomStateInitialized, returnAtomValue as INTERNAL_returnAtomValue, promiseStateMap as INTERNAL_promiseStateMap, isPendingPromise as INTERNAL_isPendingPromise, abortPromise as INTERNAL_abortPromise, registerAbortHandler as INTERNAL_registerAbortHandler, isPromiseLike as INTERNAL_isPromiseLike, addPendingPromiseToDependency as INTERNAL_addPendingPromiseToDependency, getMountedOrPendingDependents as INTERNAL_getMountedOrPendingDependents, }; declare type Awaited = T extends Promise ? V : T;