import type { Observable, Subject } from "rxjs"; import type { Machine } from "../../query/core/machine/index.js"; import type { ReadonlySignal, TBeforeDevtoolsPushFn } from "../../signals/types/index.js"; import type { TMapError } from "./api.js"; import type { IPatchHandle, Keyed } from "./common.js"; export interface ICacheEntryOptions { retentionTime: number | false; devtoolsKey: string; beforeDevtoolsPush?: TBeforeDevtoolsPushFn; } export interface ICacheEntry { readonly completed$: Subject; readonly state$: ReadonlySignal; peek(): TState; set(state: TState, actionName?: string): void; complete(): void; } export interface IQueryCacheEntryOptions { queryFn: (keyedArgs: Keyed, signal: AbortSignal) => Promise | Observable; retentionTime: number | false; keyedArgs: Keyed; resourceKey?: string; /** * Normalizes a raw query rejection into the api's error type at the single * point it enters the machine (`machine.fail`). Defaults to identity. */ mapError?: TMapError; /** Provenance forwarded to {@link mapError}'s context. Defaults to `"query"`. */ errorSource?: "query" | "command"; initialMachine?: Machine; beforeDevtoolsPush?: TBeforeDevtoolsPushFn>; /** * Invoked on every `createPatch` made while a query stream is open. Lets * the owning resource surface the emissions-rebase-over-patches interplay * (a one-time warning unless `allowStreamPatches` is set). */ onStreamPatch?: () => void; } export interface IQueryCacheEntry extends ICacheEntry> { readonly keyedArgs: Keyed; readonly machine$: ReadonlySignal>; refresh(): void; retry(): void; createPatch(patchFn: (data: TData) => void): IPatchHandle | null; /** @experimental Low-level primitive backing the imperative fetch API; may change before stabilization. */ whenLoaded(signal?: AbortSignal): Promise; /** @experimental Low-level primitive backing the imperative fetch API; may change before stabilization. */ whenFetched(signal?: AbortSignal): Promise; } export interface TCacheEntryAddedContext { entry: IQueryCacheEntry; $cacheDataLoaded: Promise; $cacheEntryRemoved: Promise; } /** * Fine-grained stream milestones of a single query run, available alongside * `$queryFulfilled` in the `onQueryStarted` context. * * For a promise-returning queryFn both promises settle together with the run's * result. For a stream-returning queryFn, `firstReceived` settles with the * first emission (≙ `$queryFulfilled`) and `allReceived` with the last * emission once the stream completes; both reject with the raw producer error. * If the run is torn down before the milestone (refresh / retry / eviction), * the promise rejects with the teardown reason. */ export interface TQueryStreamContext { firstReceived: Promise; allReceived: Promise; } export interface TQueryStartedContext { entry: IQueryCacheEntry; $queryFulfilled: Promise<{ data: TData; }>; $queryStream: TQueryStreamContext; }