/** * Copyright (c) 2025 Elara AI Pty Ltd * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details. */ /** * The paging runtime's first layer: the channels of a dataset's snapshot. It * follows each dataset's content hash — every bind of one shares a snapshot, * and a move drops what the old one delivered — and runs every channel's fetch: * launched, aborted once no read waits on it (#1398, CL6), applied only while * the channel still waits on it, failed with a reason a read throws and a retry * gap, and landed once a frame (CL9). Landed windows are kept least recently * read first, at most {@link MAX_RETAINED_WINDOWS}, never one a reader holds * (#1409, R24). The layers after it read through these channels: single windows * and searches (`reads.ts`), batched reads (`batches.ts`), and the handles and * platform functions (`../paged-runtime.ts`). * * @packageDocumentation */ import { type option } from "@elaraai/east"; import { RunsHashMismatchError } from "@elaraai/e3-api-client"; import type { TreePath } from "@elaraai/e3-types"; import { TrackedChannelStore } from "../tracked-channel.js"; import type { PagedApi } from "./api.js"; import { type PageEntry, type SearchBurst, type Snapshot } from "./state.js"; /** * What asks for the frame that applies the windows a runtime landed, in place * of the page's next frame (#1398, CL9). * * @param apply - Applies the windows landed, as the frame comes * @returns What cancels the frame */ export type PagedLandingFrame = (apply: () => void) => () => void; /** * Decoded windows retained across ALL paged sources (#567 D6). * * A decoded window is the heavy object here — a window of wide rows runs to * megabytes — and nothing else evicts one while its revision stands: scrolling * a GB-scale dataset end to end would pin every window it passed. Retention is * bounded least-recently-READ: past the cap the least recently read window no * reader holds goes whole, and a read of it fetches it again (the raw bytes * may still be in an HTTP cache, so a return is usually cheap). * * The windows its readers hold count toward the cap, but none is evicted * under its reader: past the cap the runtime keeps what its readers hold * alone, each reader bounding what it holds by its own demand (#1409, R24) — * a reader of more windows than the cap, rows read a key at a time among * them, each a one-row window, settles, each read once. * * `` caps its own materialized rows the same way * (`MAX_RETAINED_PAGES`); this is the cap for the *bound* path, where the * renderer holds no cache of its own and the runtime is the only holder. */ export declare const MAX_RETAINED_WINDOWS = 24; /** * One-row windows cut from a landed one, or from the rows a read by key holds, * kept across all paged sources, least recently read first (#1596). A one-row * cut holds a row and asked nothing of e3, so it counts toward none of * {@link MAX_RETAINED_WINDOWS}, and a reader that reads every row it draws a * row at a time — a sheet's row checks, its cells from data — leaves the * windows those rows came from where they are. A cut of more rows is a window * of the cap's, as one fetched is: at most this many rows are held by cuts. * Past it the runtime keeps the cuts its readers hold, as it keeps their * windows, and at most {@link CUTS_BETWEEN_LOOKS} that none does. */ export declare const MAX_RETAINED_CUTS = 4096; /** * How many one-row cuts are kept, past {@link MAX_RETAINED_CUTS}, between two * looks at them all (#1596). A look that leaves more than the cap leaves only * cuts a reader holds, each held until its reader lets it go: another look * would ask of every one of them whether a reader holds it, to let none go — * and a read past the cap is what looks, so a reader of 5,000 rows a row at a * time asked 25 million times a run. The cuts are looked at again once a * reader lets one go, or once this many more have been kept, which are the * only ones that can go. */ export declare const CUTS_BETWEEN_LOOKS: number; /** * The channels of every dataset's snapshot, and their fetches: the paging runtime's first layer. */ export declare class PagedChannels extends TrackedChannelStore { protected api: PagedApi | null; protected workspace: string | null; /** Loaded window keys in least-recently-READ order (a Map preserves * insertion order; a read re-inserts). Bounds the decoded-window cache. */ protected readonly loadedWindows: Map; /** The one-row windows cut from a landed one, least-recently-READ order (#1596): apart from {@link loadedWindows}, whose cap they never press on. */ protected readonly cutWindows: Map; /** * How many cuts have been kept since {@link trimCuts} last looked at them all and left more than the cap, every * one left a reader's; `undefined` while the next read past the cap is to look ({@link CUTS_BETWEEN_LOOKS}). */ private cutsSinceLook; /** Each dataset's snapshot, by workspace and path. */ protected readonly snapshots: Map; /** This turn's open search bursts, by the family of the rows they search. */ protected readonly bursts: Map; /** Fetches launched so far — the source of every channel's `launchSeq`. */ protected launches: number; /** Each fetch in flight, by its channel: its launch, what aborts it, and what an abort lets go besides (#1398, CL6). */ protected readonly fetching: Map void) | undefined; }>; /** The channels of the windows landed since the frame began, and their totals', whose readers are still to hear (#1398, CL9). */ protected readonly landed: Set; /** What cancels the frame — or the timer standing in for one — that applies them, while one waits ({@link scheduleLanding}). */ protected landing: (() => void) | undefined; /** What asks for the frame that applies them in place of the page's, once one is set ({@link setLandingFrame}). */ protected landingFrame: PagedLandingFrame | undefined; /** Monotonic clock seam so tests can drive the retry gate. */ protected now(): number; protected createEntry(): PageEntry; /** Stop following every dataset, and drop what every snapshot delivered — aborting what is still in flight. */ protected reset(): void; /** * A window has landed: its readers hear with every other window landing before the next frame (#1398, CL9), so a * burst of windows — a fling coming to rest, the windows either side read ahead — is applied to each reader once a * frame, never once a window. * * @param keys - The window's channel, and its total's when it moved */ protected notifyLanded(keys: readonly string[]): void; /** * Apply the windows landed in frames of this, in place of the page's next frame (#1398, CL9): a DOM test's, whose * page draws no frame its test waits on. `undefined` gives the page's frames back. * * @param frame - What asks for the frame, or `undefined` */ setLandingFrame(frame: PagedLandingFrame | undefined): void; /** * What applies the windows landed (#1398, CL9): the next animation frame — or, where the page draws none, hidden * say, a timer standing in for it, a few frames late — or the frame set in its place ({@link setLandingFrame}). A * runtime with no frames to draw at all applies them at once. A spec drives frames of its own. * * @param apply - Applies the windows landed * @returns What cancels it; `undefined` to apply them at once */ protected scheduleLanding(apply: () => void): (() => void) | undefined; /** No frame or timer waits to apply the windows landed. */ protected cancelLanding(): void; /** * Apply the windows landed since the frame began to their readers now (#1398, CL9): every channel's version moves, * then each reader hears once, however many of the windows it reads — what the frame does as it comes. */ protected flushLanded(): void; protected resolveWorkspace(): string; /** A dataset's snapshot, followed from a read while something reads the * dataset ({@link unsubscribed}) — the read that makes it, or the first * after its readers all let it go. */ protected snapshotOf(workspace: string, path: TreePath): Snapshot; /** * A dataset its readers have all let go stops being followed, so nothing * polls for it: every read tracks its revision channel, and a reader lets * the channel go as it unmounts. What its snapshot delivered stays, and a * read follows it again — moved at the first report if the dataset moved * meanwhile. */ protected unsubscribed(key: string): void; /** Whether a snapshot is still the runtime's, rather than one a clear or a * re-initialization dropped. */ protected isLive(snapshot: Snapshot): boolean; /** Whether a live snapshot is still pinned to `revision`. */ protected pinnedTo(snapshot: Snapshot, revision: string): boolean; /** * The revision a read of `path` is pinned to — a hash, `null` while the * dataset has no value, or `undefined` while it is being found — tracked so * the read re-fires when the source moves. * * @throws {PagedReadError} The failed lookup's reason, until a read after the retry gap * looks again */ protected revisionFor(workspace: string, path: TreePath): string | null | undefined; /** * Ask the server which content the dataset holds, and move there — unless * a move lands first, which came from something no older than the answer. */ protected find(workspace: string, path: TreePath, snapshot: Snapshot): void; /** * Move a dataset's source to `revision`: drop what the old snapshot * delivered and re-fire every read of the dataset, so the windows and * searches still in use are fetched again, pinned to the new one. A move to * the revision the source already has changes nothing. * * A move made by evidence of the source's own — an answer's revision * installed, a refusal naming the dataset's state, a lookup — is said to * whoever follows the dataset for it ({@link PagedApi.revisionHeld}), so * the next poll compares the dataset's hash with where the source reads. * A source put at a state the dataset has since left would otherwise * stay there wherever the dataset's hash is the one it was last told: * its pinned reads answered by a browser that kept them, e3 never asked * and so refusing none. * * @param told - Whether the follower itself told the source of `revision` */ protected moveTo(workspace: string, path: TreePath, snapshot: Snapshot, revision: string | null, told?: boolean): void; /** * Handle what a pinned fetch threw when it is not the read's failure: a * refusal naming other content moves the source there, and "no value" * moves it to no snapshot. Returns whether the error was one of those, or * arrived after the source had already moved on. */ protected followed(workspace: string, path: TreePath, snapshot: Snapshot, revision: string, err: unknown): boolean; /** * Handle a read by a record's runs refused because the record moved * (#1393), as {@link followed} handles a moved dataset: a refusal naming * other content moves the record there; one naming none looks it up, and * the read fails until the lookup moves it. Returns whether the refusal * moved it, or arrived after it had already moved on. */ protected followedRuns(workspace: string, path: TreePath, snapshot: Snapshot, revision: string, err: RunsHashMismatchError): boolean; /** A channel of the snapshot's current revision, dropped when it moves. */ protected channel(snapshot: Snapshot, key: string): PageEntry; /** * Let a channel go (#1351): its entry, its place among its snapshots' * channels and the window cache, and — once nothing subscribes to it — its * version. A read of it makes it again, idle, and asks for it afresh. * Never a channel in flight: its callers let go only what no fetch waits on. * * @param key - The channel */ protected dropChannel(key: string): void; /** * What a channel no reader holds any longer leaves (#1351), once the * reader re-subscribing has done so: nothing of a channel let go but its * version, which goes; a search's answer, which the revision holds apart * from its channel ({@link Snapshot.searches}), as it holds what batches * answered; and a channel holding nothing — aborted, evicted, or a batch * whose answer is held elsewhere — which goes. A landed window stays, * bounded by the windows retained: held by no reader any longer, it is the * cap's to let go again, and past the cap the least recently read windows * no reader holds go ({@link trimWindows}), so readers that held more than * the cap leave no more than it behind (#1409, R24); a one-row cut stays * bounded by the cuts retained ({@link trimCuts}) as a window is by the * windows. A placement stays, bounded by the placements a revision holds * ({@link MAX_HELD_PLACEMENTS}); and so do a failure and a total. * * @param key - The channel */ protected letGo(key: string): void; /** Whether a channel is due a fetch: never fetched, evicted, or failed * for a reason a retry can fix, longer ago than the retry gap. */ protected due(entry: PageEntry): boolean; /** * Mark a channel's fetch as started, returning the fetch's sequence number. * * Deliberately does NOT notify — the read that triggers it runs inside a * render pass, and notifying there would re-enter the renderer. Only the * settle notifies. */ protected launch(entry: PageEntry): number; /** * Mark a channel's fetch as started, as {@link launch} does, and hand it * the signal that aborts its requests (#1398, CL6): aborted once no read * waits on the channel any longer ({@link unsubscribed}). A fetch it * replaces is aborted. A fetch of a revision the source moves past is * not: the move lets go of it and of its channel, and its answer lands * nowhere, its pin no longer holding ({@link moveTo}). * * @param key - The channel * @param entry - Its entry * @param onAbort - What an abort lets go besides the request: what the fetch kept for its reader as it launched * @returns The fetch's sequence number, and its signal */ protected launchFetch(key: string, entry: PageEntry, onAbort?: () => void): { seq: number; signal: AbortSignal; }; /** A channel's fetch is over: what aborts it is let go. */ protected forgetFetch(key: string, seq: number): void; /** * Abort a channel's fetch still in flight, which no read waits on any * longer (#1398, CL6): its requests are cancelled — a request still to be * sent, a retry among them, never is — and the channel goes back to * `idle`, so a read that asks for it again fetches it afresh. Nothing is * notified: no reader is waiting on it. * * @param key - The channel */ protected abortFetch(key: string): void; /** Apply a fetch's outcome if the channel still waits on it, letting * the fetch go once the channel has settled. Returns whether it applied. */ protected apply(key: string, seq: number, mutate: (entry: PageEntry) => void): boolean; /** Apply a fetch's outcome if the channel still waits on it, re-firing the * channel's reads. Returns whether it applied. */ protected settle(key: string, seq: number, mutate: (entry: PageEntry) => void): boolean; /** A failed fetch's outcome: the reason a read will throw. */ protected failure(reason: string, permanent: boolean): (entry: PageEntry) => void; /** Settle a fetch as failed, with the reason a read will throw. */ protected fail(key: string, seq: number, reason: string, permanent: boolean): void; /** * Join this turn's burst of searches of one source's rows (#1360), opening * it with the turn's first. It closes as the turn ends. * * @param family - The rows searched — {@link pagedWindowFamily} * @returns The burst */ protected joinBurst(family: string): SearchBurst; /** * A member of a burst is done: answered, or dropped — by a move, or by a * newer launch on its channel. * * @param burst - Its burst * @param answered - Its channel, when its answer was applied */ protected leaveBurst(burst: SearchBurst, answered: string | undefined): void; /** Re-fire every channel a closed burst answered, once its last member is done. */ protected flushBurst(burst: SearchBurst): void; /** * Ask again what failed: an explicit refresh — a Retry — puts every channel * of the snapshot that failed for a reason a retry can fix back to `idle` * and re-fires its readers, so their next read fetches it at once, not * after the retry gap a read alone waits. A permanent failure stays until * the source moves; nothing else is dropped. * * @param snapshot - The source's snapshot */ protected rearmFailed(snapshot: Snapshot): void; /** * A Retry of a dataset, following its current state: what failed asked * again now ({@link rearmFailed}), and the content it holds looked up * again — what `refresh` with no target and {@link askAgain} both ask. * * @param workspace - The workspace the Retry was asked in: another one since installed asks nothing * @param path - The dataset's path */ protected retryNow(workspace: string, path: TreePath): void; /** What a read of a channel answers: its value once loaded, `none` while * in flight, or its failure, thrown as a {@link PagedReadError}. */ protected answer(entry: PageEntry, value: T | undefined): option; /** Note a window as most-recently-read, and let the coldest windows no * reader holds go once more have landed than the cap ({@link trimWindows}). */ protected touchWindow(key: string): void; /** * Keep a one-row window cut from a landed one, or from the row a read by * key holds, apart from the windows (#1596): the most recently read of the * cuts, and one more kept since they were last looked at ({@link trimCuts}). * * @param key - The cut's channel */ protected keepCut(key: string): void; /** * Note a one-row cut as most-recently-read (#1596), and let the coldest no * reader holds go past {@link MAX_RETAINED_CUTS} ({@link trimCuts}). Read * again, one is cut again from what it came from, for nothing. * * @param key - The cut's channel */ protected touchCut(key: string): void; /** * Past {@link MAX_RETAINED_CUTS} one-row cuts, let the least recently read * that no reader holds go, coldest first, as {@link trimWindows} lets * windows go (#1596). * * A look that leaves more than the cap leaves only cuts a reader holds — * one it subscribes to, or one the read in hand has read, which it * subscribes to as it ends — and each stays held until its reader lets it * go. Looking again meanwhile asks of every one whether a reader holds it, * to let none go, and every read of a cut past the cap is what looks: a * reader of more rows a row at a time than the cap would ask as many times * as its rows, squared, a run. So the cuts are not looked at again until * one may go: a reader lets a cut go ({@link letGo}), or * {@link CUTS_BETWEEN_LOOKS} more have been kept ({@link keepCut}), which * are the only cuts no reader may hold. Until then at most that many cuts * no reader holds stand past the cap. */ protected trimCuts(): void; /** * Past {@link MAX_RETAINED_WINDOWS} landed windows, let the least recently * read that no reader holds go, whole (#1351), coldest first, until the * cap holds or none is left to go. * * A window a reader holds is never evicted under it: one a reader * subscribes to — what its last run read — and one the evaluation in hand * has read, which it subscribes to as it ends. A read pass longer than the * cap would otherwise evict its own head, find it gone on its next pass and * blank the surface (#581). And evicted while a reader held it, a window * went `idle` without its reader being told, so the reader's next run, set * off by any other landing, read it again: a reader of more windows than * the cap — rows read a key at a time, each a key search and a one-row * window, or windows several readers hold between them — never settled, * each landing evicting one it held (#1409, R24). Past the cap the runtime * keeps what its readers hold alone, each reader bounding that by its own * demand: exceeding a memory target beats a surface that never settles. * * A window still in flight is never evicted either: its settle would land * on an entry the next read has already relaunched. */ protected trimWindows(): void; /** * Note a placement as its revision's most recently read, and let the least * recently read go past {@link MAX_HELD_PLACEMENTS} (#1349): a viewer trying * narrowing after narrowing leaves each one's placement behind, which * nothing else lets go while the revision stands. One a reader holds, one * the read in hand has read, and one still in flight stay — the revision * over its bound, rather than a reader's placement asked again. * * @param snapshot - The placement's revision * @param key - The placement's channel */ protected touchPlacement(snapshot: Snapshot, key: string): void; } //# sourceMappingURL=channels.d.ts.map