import type { IPatchHandle, IQueryCacheEntry, IQueryCacheEntryOptions, Keyed } from "../../../query/types/index.js"; import type { ReadonlySignal } from "../../../signals/types/index.js"; import { Machine } from "../machine/Machine.js"; import { CacheEntry } from "./CacheEntry.js"; export declare class QueryCacheEntry extends CacheEntry> implements IQueryCacheEntry { readonly keyedArgs: Keyed; readonly machine$: ReadonlySignal>; private _queryFn; private _abortController; /** * Controller of the run whose query stream is currently open (has an * active, non-terminated subscription); `null` when no stream is open. * Owner-tracked instead of a plain boolean so a superseded run's teardown * can tell whether the flag is still its own to reset (see * {@link _subscribeStream}). */ private _streamController; /** True while the current run's query stream has an active, non-terminated subscription. */ private get _isStreamOpen(); private readonly _mapError; private readonly _errorSource; private readonly _resourceKey; private readonly _onStreamPatch; /** First data ever seen (survives error+retry); rejected only if the entry is removed first. */ private readonly _firstLoaded; constructor(options: IQueryCacheEntryOptions); /** Transition to refreshing and re-fetch data. Valid from success or refresh-error. */ refresh(): void; /** * Re-execute the query after a failure. Valid from error and refresh-error. * Unlike {@link refresh}, the failed error stays visible and the in-flight * state is marked `isRetrying`. */ retry(): void; /** Create an optimistic patch. Returns null if state has no data. */ createPatch(patchFn: (data: TData) => void): IPatchHandle | null; /** * Resolve as soon as the entry holds data (whether freshly loaded or already * cached / being refreshed), and reject on a terminal `error`. Used by * {@link Resource.ensure} / {@link Resource.prefetch}. * * Stale data (refreshing / refresh-error) resolves immediately — the caller * gets whatever is available without waiting for a background refresh. * * @experimental Low-level primitive backing the imperative fetch API; may * change before stabilization. * @param signal - Detaches the caller when aborted: the promise rejects with * the signal's reason. The query itself is untouched and is only torn down * by retention GC once no consumer remains. */ whenLoaded(signal?: AbortSignal): Promise; /** * Resolve when the machine settles with fresh data (`success`), rejecting on * `error` / `refresh-error`. Unlike {@link whenLoaded}, transient stale data * (pending / refreshing) is awaited rather than resolved. Used by * {@link Resource.fetch} (which always (re)starts a run before awaiting). * * @experimental Low-level primitive backing the imperative fetch API; may * change before stabilization. * @param signal - See {@link whenLoaded}. */ whenFetched(signal?: AbortSignal): Promise; /** * Promise resolving on the first data the entry ever holds (surviving an * initial error + retry), rejecting only if the entry is removed beforehand. * Backs the `$cacheDataLoaded` lifecycle context. */ whenFirstLoaded(): Promise; /** * Resolve/reject with the outcome of the machine's next settled state — the * same transitions as {@link whenFetched}, but without a keepalive * subscription, so the caller owns the entry's lifecycle. Backs `Command.execute`. * * Entry-removal rejections (`CacheEntryRemovedError` from an eviction by a * newer execute or a `reset()`) pass through `mapError` here: this promise * feeds the typed `TTriggerResult` envelope, whose `error` is declared as * `TError`, so an unmapped escape would break that contract at runtime. */ currentResult(): Promise; /** Abort any in-flight request before completing the entry. */ complete(): void; /** * Universal state-driven waiter: observe machine transitions (starting from * the current state, which is replayed on subscribe) and settle on the first * state that `settle` maps to an outcome. * * Rejects with {@link CacheEntryRemovedError} if the entry completes before a * matching state, and with the signal's reason if `signal` aborts first. * * @param settle - Maps a machine state to a resolution/rejection outcome, or * `null` to keep waiting. * @param opts.keepalive - When `true`, observes the shared stream and thereby * holds the share's refcount, so retention GC only resumes once the waiter * settles or detaches. When `false`, observes the raw state stream without * affecting the entry's lifecycle. * @param opts.mapRemoval - When `true`, the removal rejection passes through * `mapError` — for waiters feeding a channel typed as `TError` (the * command result envelope). Waiters on untyped channels (`ensure`/`fetch` * rejections, `$cacheDataLoaded`) keep the raw `CacheEntryRemovedError`. */ private _awaitState; /** Provenance handed to `mapError` for any failure surfaced by this entry. */ private _errorContext; /** * The single normalization boundary shared by the promise and stream * failure paths: a raw rejection becomes the api's TError exactly here. An * error arriving in a {@link PreMappedError} envelope already passed * `mapError` at an upstream entry's boundary (a projection run re-surfacing its * wrapped resource's rejection) — it is unwrapped instead of being mapped * a second time. */ private _normalizeError; /** Settle matcher for a query run's outcome: fresh data or a failed run. */ private _settleQueryOutcome; /** @internal Called by Resource when beforeQuery intercept needs to trigger the query. */ _execute(): void; /** * Run a stream-returning queryFn: the first emission settles the run * (pending → success / refreshing → rebase), each subsequent emission * updates the data through the patch-rebase machinery (success → success), * a stream error after data lands in refresh-error (data kept), and a * completion without a single emission fails the run with * {@link EmptyStreamError}. Completion after data leaves the entry as-is. * * The subscription is tied to the run's abort controller: a newer * `_execute` (refresh / retry) or entry completion aborts it, which * unsubscribes and thereby triggers the producer's teardown. */ private _subscribeStream; /** Apply a stream emission to the machine (see {@link _subscribeStream}). */ private _applyStreamData; /** Fail the current stream run; unlike the promise path, `success` is a valid failure origin. */ private _failStreamRun; }