/** * Copyright (c) 2025 Elara AI Pty Ltd * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details. */ /** * Copyright (c) 2025 Elara AI Pty Ltd * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details. */ /** * Runtime implementation for the `Data.bindPaged` platform functions — reading * a collection dataset one WINDOW at a time, for sources too large to hold * whole. * * The read itself is the trigger: `page(offset, limit)` returns the window if * it has landed, and otherwise starts the fetch and returns `none`. The caller * re-reads on the next reactive frame (the window's channel notifies when it * settles), which is why the East-side contract is `Option` rather than a * promise — the same shape `Func.bind`'s `read()` has. A fetch that failed * throws its reason when read — a {@link PagedReadError}, so a reader tells a * read that failed from any other error — so `none` means only "in flight". * * Every bind of one dataset shares a SNAPSHOT: the content hash its reads are * pinned to, its revision. Windows, totals and searches are fetched pinned, so * the server answers from that content or refuses; what one snapshot delivered * is never served beside another's. The snapshot follows its dataset — the * dataset store's status poll reporting a new hash, a refused pin, or * `refresh` moves it — and a move drops what the old snapshot delivered, so the * windows and searches still being read are fetched again, pinned to the new * one. It follows while something reads the dataset: once its readers have all * let it go, nothing polls for it, and the next read follows it again — moved * at the first report if the dataset moved meanwhile. * * One tracked channel per window, per total and per search, each keyed by the * revision it belongs to, plus one per dataset for its revision, which every * read tracks. * * Searches of one source's rows launched in one synchronous turn are a burst * (#1360): each answer is applied to its channel as it lands, and the last * re-fires every channel the burst answered at once, so a reader of a window * over an interval index — some fifty-five searches — runs again once, not * once a search. Windows are applied to their readers once a frame (#1398, * CL9) — those by offset and those across a narrowing's runs alike: each lands * in its channel at once, and the windows that land before the next frame — a * fling coming to rest, the windows read ahead either side — re-fire together, * so a reader draws once for them, never once a window. * * A window a landed one covers is never fetched (#1217): its rows are that * window's, at the same revision, so the read is answered from it and keeps * the answer in its own channel. A sheet reads an entry it is about to draft * as a one-element window, beside the windows it draws. A window one in flight * covers waits on that one, asking nothing of its own (#1596): its rows are * cut from it once it lands — a sheet's row checks over the rows a Save's new * revision reads again, a request a row otherwise. * * Landed windows are kept least recently read first, at most * {@link MAX_RETAINED_WINDOWS} of them: past the cap a window no reader holds * goes whole, and none a reader holds is evicted under it (#1409, R24), so a * reader of more windows than the cap — rows read a key at a time among * them, each a key search and a one-row window — settles, each read once. A * one-row window cut from a landed one cost no request, and is kept apart from * them (at most {@link MAX_RETAINED_CUTS}): a reader of many rows a row at a * time, each answered from a landed window, never pushes that window out * (#1596). A cut of more rows is kept as a window is. * * Several reads of one source are asked as one request when a reader asks * them together (#1351): `seekAll` a batch of key searches (`findKeys`), * `rowsAll` the rows of several keys of a Dict, `runsAll` the entries of * several runs — and `joinAll` a Dict's rows under the keys some runs of one * of a record's indexes name, the keys read by the server, none sent (#1393). * A batch is one request and one channel, named by a digest of what it asks, * which re-fires its readers once and holds its answer as a landed window * does, bounded with them. What it answers serves the single reads too: each * search's bounds are held at the revision — the last * {@link MAX_LANDED_SEARCHES} of them, a search's own answer among them once * no reader holds its channel — each row is held at its position — a search * by its key, or a one-row window at its row, is answered from it — and a * window inside one of a runs read's runs is cut from it. A channel no reader * holds goes once it holds nothing — aborted, evicted, or a batch whose * answer is held elsewhere — and its version with it. A record Slice over a * binding the indexed runtime built reads a plan's searches, and a window * across several runs, through them. A read by another record's runs is * pinned to both datasets' revisions, its channel both snapshots': a move of * either drops it. * * A read no reader waits on any longer is aborted while still in flight * (#1398, CL6, CL7): a window that left the demand ring, a period read a newer * navigation superseded. Each fetch carries its channel's signal through the * API seam; once the channel's last reader lets it go — and has not * subscribed again by the next microtask — its requests are cancelled and the * channel goes back to `idle`, so a read that asks for it again fetches it * afresh. (A read of a revision the source moved past is not aborted: its * answer lands nowhere, its pin no longer holding.) * * The dataset store's CONTENT is deliberately not used: it holds whole values, * and a paged source is precisely the thing you cannot hold whole. A source * rides the store's status poll for hashes alone, through * {@link PagedApi.watchRevision}, and reads windows through its own API seam — * the same shape `FuncRuntime` / `RecordRuntime` use. * * The runtime is a layer a file, each reading through the one before it, in * `paged/`: its API seam (`api.ts`), the names of its channels (`keys.ts`), * what it holds and its bounds (`state.ts`), window arithmetic (`windows.ts`) * and how it reads a failure (`errors.ts`); then the channels of a snapshot * and their fetches (`channels.ts`), single windows and searches (`reads.ts`) * and batched reads (`batches.ts`). This module is the last layer — the * handles, the platform functions and the default runtime — and the * runtime's entry: the rest of the package imports the runtime from here. * * @packageDocumentation */ import { type EastType, type EastTypeValue, type option } from "@elaraai/east"; import { type PlatformFunction } from "@elaraai/east/internal"; import { type TreePath } from "@elaraai/e3-types"; import { IndexedPagedRuntime } from "./indexed-paged.js"; import type { PagedSelector, PagedSource, PagedApi } from "./paged/api.js"; import type { PagedLandingFrame } from "./paged/channels.js"; import { PagedBatches } from "./paged/batches.js"; export { type PagedWindow, type PagedSelector, type PagedSource, type PagedRow, type PagedRun, type PagedPlacedSegment, type PagedPlacement, type PagedApi, createDefaultPagedApi, } from "./paged/api.js"; export { pagedRevisionKey, pagedWindowKey, pagedTotalKey, pagedSeekKey, pagedBatchKey } from "./paged/keys.js"; export { MAX_WITHIN_KEYS, MAX_HELD_PLACEMENTS } from "./paged/state.js"; export { type PagedLandingFrame, MAX_RETAINED_WINDOWS, MAX_RETAINED_CUTS, CUTS_BETWEEN_LOOKS } from "./paged/channels.js"; export { type PagedRuntimeProbe, setPagedRuntimeProbe } from "./paged/reads.js"; /** The Dict key / Set element type of a keyed collection, else null — the * `seek` capability is decided from the dataset's own type at bind time. */ export declare function keyTypeOf(sourceType: EastTypeValue): EastTypeValue | null; export { toFindQuery } from "./find-query.js"; /** * Encapsulates all `Data.bindPaged` runtime state. The module-level * {@link defaultPagedRuntime} instance backs the registered platform; tests * construct their own for isolation. */ export declare class PagedRuntime extends PagedBatches { /** Field-index bindings share this runtime's existing reads. */ readonly indexed: IndexedPagedRuntime; private readonly handleCache; /** The source each handle serves, by the handle's `id` — what the id says, * kept as it was built rather than read back from its text. */ private readonly sources; /** Install the API adapter + workspace — called by the React provider * (or a test) before any handle is used. Another adapter or workspace * starts from nothing: no snapshot of the old one's carries over. */ initialize(api: PagedApi, workspace: string): void; /** * Tear down the adapter and all window state. * * @param api - Clear only while this is the installed adapter, so a * provider tearing down after another installed its own leaves that one * in place */ clear(api?: PagedApi): void; /** * The low-level primitives backing handle methods, bound to THIS runtime. * Registered globally (extension registry) and included by the scoped * platform (e3 `ui()` tasks) so a decoded handle re-binds to whatever * runtime resolves the primitives on the decode side. */ buildPrimitives(): PlatformFunction[]; /** * Build the handle value for one paged bind. Every method is a thin * IR-bearing `East.function` over {@link buildPrimitives}, capturing only * the plain-data source path and selector (the value type rides as a * type-arg) — so the handle is ordinary serializable East data (issue * #106). * * @param sourceType - The dataset's type, or an index's window type * @param path - The dataset's path * @param selector - The rows the bind serves * @param shape - `released`: the handle `data_bind_paged` returns, without * `revision` or `refresh`; `pinned`: the handle `data_bind_paged_pinned` * returns. Both read through the same snapshot. * @returns The handle */ buildHandle(sourceType: EastTypeValue, path: TreePath, selector: PagedSelector, shape: "released" | "pinned"): Record; /** * The source a handle this runtime built serves, by the handle's `id`: its * dataset's path and the rows it reads — what a component holding only the * handle needs to name the dataset, such as a query's root. * * @param id - A paged handle's `id` * @returns Its source, or `undefined` when this runtime built no handle with that id */ sourceOf(id: string): PagedSource | undefined; /** Build the PlatformFunction behind `data_bind_paged` — the bind a UI * exported before pinned reads calls — bound to this runtime. Its handle * has no `revision` or `refresh`, but its reads are pinned, follow the * dataset and fail visibly all the same. Pass `allowed=null` for an * unscoped impl, or a Set of path strings for manifest scoping. */ buildPlatform(allowed: ReadonlySet | null): PlatformFunction; /** Build the PlatformFunction behind `data_bind_paged_pinned` — what * `Data.bindPaged` emits, for a dataset's own rows or a record's index — * bound to this runtime and scoped by `allowed` exactly as * {@link buildPlatform} is: an index read is a read of the record's path. */ buildPinnedPlatform(allowed: ReadonlySet | null): PlatformFunction; } /** Process-global runtime backing the `PagedPlatform` export. */ export declare const defaultPagedRuntime: PagedRuntime; /** * The source a paged handle serves, by the handle's `id`, from the runtime * behind the registered and the scoped paged platforms. * * @param id - A paged handle's `id` * @returns Its dataset's path and the rows it serves, or `undefined` when no * handle with that id has been bound */ export declare function pagedSourceOf(id: string): PagedSource | undefined; /** * Reads a keyed source's entries by their ids. * * @param ids - The entries' ids * @returns Each entry's row, `none` for one the source does not hold, by id; none while the read is in flight; * `undefined` for a source that cannot be read by key */ export type EntriesByKey = (ids: readonly string[]) => option>> | undefined; /** * Reads a keyed source's entries by their ids, every one in one request * (#1596): what a Save reads the entries it wrote back by, however many, and * wherever they sit. A source's entries are its dataset's own rows, whichever * of them it pages — all of them in key order, one of a record's indexes, a * binding's unfiltered view, or a record Slice's narrowing — so each is read * from the record itself, an id naming no key of the source's type being an * entry it does not hold. The source is found as each read runs: its handle is * bound as the page's program runs. * * @param id - The source's id, as its handle carries it, no narrowing printed into it * @param keyType - The source's key type * @param rowType - The type of its entries * @returns The reader */ export declare function entriesByKeyOf(id: string, keyType: EastType, rowType: EastType): EntriesByKey; /** * Whether the workspace has read the record a paged source reads since the * record's last write from here was answered (the record runtime's * `followedOf`): from then the revision the source is pinned to is the * record's as of a read made after that write. What a Save over the source * that did not install the revision it committed — the source having moved * since the Save was drafted — is read back by, wherever the source then * reads. The source is found as each read runs: its handle is bound as the * page's program runs. * * @param id - The source's id, as its handle carries it * @returns The reader, tracked: `true` once it has, `false` until then, and * `undefined` where nothing says — a source of no record, a record no write * from here has been answered for, or one whose hash is not followed */ export declare function readSinceAnswerOf(id: string): () => boolean | undefined; /** * Apply the windows the default runtime lands 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` */ export declare function setPagedLandingFrame(frame: PagedLandingFrame | undefined): void; /** Install the paging API adapter + workspace — called by the React provider * on mount (or by a test). */ export declare function initializePagedApi(api: PagedApi, workspace: string): void; /** * Tear down the paging API adapter and all window state. * * @param api - Clear only while this is the installed adapter */ export declare function clearPagedApi(api?: PagedApi): void; /** Global, manifest-unscoped paged binds + their backing primitives. * Registered on module load (powers the extension registry decode path). */ export declare const PagedPlatform: PlatformFunction[]; /** Build manifest-scoped paged binds + their backing primitives, from the * manifest's `pages` list. * * The `data_page*` primitives MUST ship with the scoped platform: e3 `ui()` * tasks render through `createScoped*()` arrays (UITaskPreview), NOT the * global registry, so a serialized handle's methods would otherwise decode to * "Platform function 'data_page' is not available". */ export declare function createScopedPagedPlatform(pages: readonly TreePath[]): PlatformFunction[]; //# sourceMappingURL=paged-runtime.d.ts.map