import type { Atom, WritableAtom } from './atom.js'; type AnyValue = unknown; type AnyError = unknown; type AnyAtom = Atom; type WithOnMount = { onMount: NonNullable['onMount']>; }; type WithOnInit = { INTERNAL_onInit: NonNullable['INTERNAL_onInit']>; }; 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; /** The store epoch number that last validated the atom. */ m?: 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 WeakMapLike = { get(key: K): V | undefined; set(key: K, value: V): void; has(key: K): boolean; delete(key: K): boolean; }; type SetLike = { readonly size: number; add(value: T): void; has(value: T): boolean; delete(value: T): boolean; clear(): void; forEach(callback: (value: T) => void): void; [Symbol.iterator](): IterableIterator; }; type AtomStateMap = WeakMapLike; type MountedMap = WeakMapLike; type InvalidatedAtoms = WeakMapLike; type ChangedAtoms = SetLike; type Callbacks = SetLike<() => void>; type AtomRead = (buildingBlocks: Readonly, store: Store, atom: Atom, ...params: Parameters['read']>) => Value; type AtomWrite = (buildingBlocks: Readonly, store: Store, atom: WritableAtom, ...params: Parameters['write']>) => Result; type AtomOnInit = (buildingBlocks: Readonly, store: Store, atom: Atom & WithOnInit) => void; type AtomOnMount = (buildingBlocks: Readonly, store: Store, atom: WritableAtom & WithOnMount, setAtom: (...args: Args) => Result) => OnUnmount | void; type EnsureAtomState = (buildingBlocks: Readonly, store: Store, atom: Atom) => AtomState; type FlushCallbacks = (buildingBlocks: Readonly, store: Store) => void; type RecomputeInvalidatedAtoms = (buildingBlocks: Readonly, store: Store) => void; type ReadAtomState = (buildingBlocks: Readonly, store: Store, atom: Atom) => AtomState; type InvalidateDependents = (buildingBlocks: Readonly, store: Store, atom: AnyAtom) => void; type WriteAtomState = (buildingBlocks: Readonly, store: Store, atom: WritableAtom, args: Args) => Result; type MountDependencies = (buildingBlocks: Readonly, store: Store, atom: AnyAtom) => void; type MountAtom = (buildingBlocks: Readonly, store: Store, atom: Atom) => Mounted; type UnmountAtom = (buildingBlocks: Readonly, store: Store, atom: Atom) => Mounted | undefined; type SetAtomStateValueOrPromise = (buildingBlocks: Readonly, store: Store, atom: Atom, valueOrPromise: Value) => void; type StoreGet = (buildingBlocks: Readonly, store: Store, atom: Atom) => Value; type StoreSet = (buildingBlocks: Readonly, store: Store, atom: WritableAtom, ...args: Args) => Result; type StoreSub = (buildingBlocks: Readonly, store: Store, atom: AnyAtom, listener: () => void) => () => void; type EnhanceBuildingBlocks = (buildingBlocks: Readonly, store: Store) => Readonly; type AbortHandlersMap = WeakMapLike, Set<() => void>>; type RegisterAbortHandler = (buildingBlocks: Readonly, store: Store, promise: PromiseLike, abortHandler: () => void) => void; type AbortPromise = (buildingBlocks: Readonly, store: Store, promise: PromiseLike) => void; type StoreEpochHolder = [n: EpochNumber]; type Store = { get: (atom: Atom) => Value; set: (atom: WritableAtom, ...args: Args) => Result; sub: (atom: AnyAtom, listener: () => void) => () => void; }; declare const KEY_atomStateMap = "a"; declare const KEY_mountedMap = "m"; declare const KEY_invalidatedAtoms = "i"; declare const KEY_changedAtoms = "c"; declare const KEY_mountCallbacks = "q"; declare const KEY_unmountCallbacks = "Q"; declare const KEY_storeHooks = "h"; declare const KEY_atomRead = "R"; declare const KEY_atomWrite = "W"; declare const KEY_atomOnInit = "I"; declare const KEY_atomOnMount = "M"; declare const KEY_ensureAtomState = "e"; declare const KEY_flushCallbacks = "f"; declare const KEY_recomputeInvalidatedAtoms = "C"; declare const KEY_readAtomState = "r"; declare const KEY_invalidateDependents = "d"; declare const KEY_writeAtomState = "w"; declare const KEY_mountDependencies = "D"; declare const KEY_mountAtom = "t"; declare const KEY_unmountAtom = "T"; declare const KEY_setAtomStateValueOrPromise = "v"; declare const KEY_storeGet = "g"; declare const KEY_storeSet = "s"; declare const KEY_storeSub = "b"; declare const KEY_enhanceBuildingBlocks = "B"; declare const KEY_abortHandlersMap = "p"; declare const KEY_registerAbortHandler = "H"; declare const KEY_abortPromise = "A"; declare const KEY_storeEpochHolder = "E"; type BuildingBlocks = { [KEY_atomStateMap]: AtomStateMap; [KEY_mountedMap]: MountedMap; [KEY_invalidatedAtoms]: InvalidatedAtoms; [KEY_changedAtoms]: ChangedAtoms; [KEY_mountCallbacks]: Callbacks; [KEY_unmountCallbacks]: Callbacks; [KEY_storeHooks]: StoreHooks; [KEY_atomRead]: AtomRead; [KEY_atomWrite]: AtomWrite; [KEY_atomOnInit]: AtomOnInit; [KEY_atomOnMount]: AtomOnMount; [KEY_ensureAtomState]: EnsureAtomState; [KEY_flushCallbacks]: FlushCallbacks; [KEY_recomputeInvalidatedAtoms]: RecomputeInvalidatedAtoms; [KEY_readAtomState]: ReadAtomState; [KEY_invalidateDependents]: InvalidateDependents; [KEY_writeAtomState]: WriteAtomState; [KEY_mountDependencies]: MountDependencies; [KEY_mountAtom]: MountAtom; [KEY_unmountAtom]: UnmountAtom; [KEY_setAtomStateValueOrPromise]: SetAtomStateValueOrPromise; [KEY_storeGet]: StoreGet; [KEY_storeSet]: StoreSet; [KEY_storeSub]: StoreSub; [KEY_enhanceBuildingBlocks]: EnhanceBuildingBlocks | undefined; [KEY_abortHandlersMap]: AbortHandlersMap; [KEY_registerAbortHandler]: RegisterAbortHandler; [KEY_abortPromise]: AbortPromise; [KEY_storeEpochHolder]: StoreEpochHolder; }; 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); type ActuallyWritableAtom = T extends WritableAtom ? T & WritableAtom : T extends Atom ? T & WritableAtom : never; declare function isActuallyWritableAtom(atom: T): atom is ActuallyWritableAtom; declare function isAtomStateInitialized(atomState: AtomState): boolean; declare function returnAtomValue(atomState: AtomState): Value; declare function isPromiseLike(p: unknown): p is PromiseLike; declare function shouldThrowSynchronously(error: unknown): boolean; declare function addPendingPromiseToDependency(atom: AnyAtom, promise: PromiseLike, dependencyAtomState: AtomState): void; declare function getMountedOrPendingDependents(atom: AnyAtom, atomState: AtomState, mountedMap: MountedMap): Iterable; 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 state is created. */ readonly i?: StoreHookForAtoms; /** 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(partialBuildingBlocks?: Partial): Store; export { buildStore as INTERNAL_buildStoreRev4, getBuildingBlocks as INTERNAL_getBuildingBlocksRev4, initializeStoreHooks as INTERNAL_initializeStoreHooksRev4, KEY_atomStateMap as INTERNAL_KEY_atomStateMap, KEY_mountedMap as INTERNAL_KEY_mountedMap, KEY_invalidatedAtoms as INTERNAL_KEY_invalidatedAtoms, KEY_changedAtoms as INTERNAL_KEY_changedAtoms, KEY_mountCallbacks as INTERNAL_KEY_mountCallbacks, KEY_unmountCallbacks as INTERNAL_KEY_unmountCallbacks, KEY_storeHooks as INTERNAL_KEY_storeHooks, KEY_atomRead as INTERNAL_KEY_atomRead, KEY_atomWrite as INTERNAL_KEY_atomWrite, KEY_atomOnInit as INTERNAL_KEY_atomOnInit, KEY_atomOnMount as INTERNAL_KEY_atomOnMount, KEY_ensureAtomState as INTERNAL_KEY_ensureAtomState, KEY_flushCallbacks as INTERNAL_KEY_flushCallbacks, KEY_recomputeInvalidatedAtoms as INTERNAL_KEY_recomputeInvalidatedAtoms, KEY_readAtomState as INTERNAL_KEY_readAtomState, KEY_invalidateDependents as INTERNAL_KEY_invalidateDependents, KEY_writeAtomState as INTERNAL_KEY_writeAtomState, KEY_mountDependencies as INTERNAL_KEY_mountDependencies, KEY_mountAtom as INTERNAL_KEY_mountAtom, KEY_unmountAtom as INTERNAL_KEY_unmountAtom, KEY_setAtomStateValueOrPromise as INTERNAL_KEY_setAtomStateValueOrPromise, KEY_storeGet as INTERNAL_KEY_storeGet, KEY_storeSet as INTERNAL_KEY_storeSet, KEY_storeSub as INTERNAL_KEY_storeSub, KEY_enhanceBuildingBlocks as INTERNAL_KEY_enhanceBuildingBlocks, KEY_abortHandlersMap as INTERNAL_KEY_abortHandlersMap, KEY_registerAbortHandler as INTERNAL_KEY_registerAbortHandler, KEY_abortPromise as INTERNAL_KEY_abortPromise, KEY_storeEpochHolder as INTERNAL_KEY_storeEpochHolder, hasInitialValue as INTERNAL_hasInitialValue, isActuallyWritableAtom as INTERNAL_isActuallyWritableAtom, isAtomStateInitialized as INTERNAL_isAtomStateInitialized, returnAtomValue as INTERNAL_returnAtomValue, isPromiseLike as INTERNAL_isPromiseLike, shouldThrowSynchronously as INTERNAL_shouldThrowSynchronously, addPendingPromiseToDependency as INTERNAL_addPendingPromiseToDependency, getMountedOrPendingDependents as INTERNAL_getMountedOrPendingDependents, };