import { globalState } from '../../../app/global-state'; /** * What a component hands its ChunkedRenderWindow. Every member is read when it * is needed, never cached, so the window always sees the owner's current state. */ export interface ChunkedRenderOwner { /** Id of the sentinel element the owner renders after its last rendered row. */ sentinelId: () => string; /** Rows in the owner's current list; the budget never grows past it. */ rowCount: () => number; /** The owner's reactive row budget. */ getLimit: () => number; setLimit: (limit: number) => void; /** The sentinel is in view with every row rendered (DropdownList: ask the asyncLoader for the next page). */ onExhausted?: () => void; } /** * Chunked list rendering for and (GOBO-500). * * Neither component renders a long option list whole. Each renders the first * `limit` rows of its own list, then a sentinel element, and one * ChunkedRenderWindow per shown panel grows that budget by CHUNK rows whenever * the sentinel scrolls into view. There is no row-height model: the rendered * rows are laid out by the browser exactly as a fully rendered list would be, * so variable row heights, option groups, custom renderers and sticky section * labels need nothing special. * * The window owns the policy and its state: the chunk size, the observer, the * growth step, when to re-observe, and the one fallback for hosts without * IntersectionObserver (see `initialLimit`). A component owns its reactive * budget, the list it spends it on and the markup that spends it. It holds its * window through `markRaw` (like `ModalScrollLock`), so Vue never proxies it. * * The sentinel is resolved by element id, never through `$el`: the mobile * teleports DropdownList's panel out of the component. */ export default class ChunkedRenderWindow { /** Rows added per growth step. */ static readonly CHUNK: number = 40; /** * Grow this far before the sentinel reaches the viewport. The observer's root is * the viewport, so the margin applies to page scrolling (inline lists, panels * reaching below the fold); inside a panel's own scroller the sentinel counts as * visible once it is scrolled into the panel. */ private static readonly ROOT_MARGIN: string = '240px'; private readonly owner: ChunkedRenderOwner; /** Null where IntersectionObserver does not exist: the window is then inert. */ private readonly observer: IntersectionObserver | null; /** What the observer was last pointed at: the sentinel element and how many rows were rendered then. */ private armed: { sentinel: Element | null; rows: number } | null = null; private constructor(owner: ChunkedRenderOwner) { this.owner = owner; this.observer = ChunkedRenderWindow.isSupported() ? new globalState.IntersectionObserver((entries: IntersectionObserverEntry[]) => { if (entries.some(entry => entry.isIntersecting)) { this.grow(); } }, { rootMargin: ChunkedRenderWindow.ROOT_MARGIN }) : null; } /** An IntersectionObserver exists, so a budget can grow. False on the server, in jsdom and in legacy browsers. */ static isSupported(): boolean { return globalState.windowExists === true && typeof globalState.IntersectionObserver === 'function'; } /** * Budget for a freshly shown list whose first `alwaysRendered` rows must all be * present. A browser without IntersectionObserver (jsdom, legacy) could never * grow it, so there every row renders: a list must never be silently * truncated. This is the one place that fallback lives — owners start and * reset their budget only through here. The server renders the first chunk, * exactly as the hydrating client does. */ static initialLimit(alwaysRendered: number = 0): number { if (globalState.windowExists === true && !ChunkedRenderWindow.isSupported()) { return Number.MAX_SAFE_INTEGER; } return alwaysRendered + ChunkedRenderWindow.CHUNK; } /** Smallest whole-chunk budget, never below `current`, that renders row `index`. */ static limitFor(current: number, index: number): number { // NaN (keyboard nav over an empty list) must never poison the budget. if (Number.isNaN(index) || index < current) { return current; } return Math.ceil((index + 1) / ChunkedRenderWindow.CHUNK) * ChunkedRenderWindow.CHUNK; } /** * Starts growing a shown list and points the observer at its current sentinel. * Without IntersectionObserver the window is inert: the owner's budget from * `initialLimit` already renders every row. */ static attach(owner: ChunkedRenderOwner): ChunkedRenderWindow { const instance = new ChunkedRenderWindow(owner); instance.rearm(); return instance; } /** * Points the observer at the sentinel after a render that changed the rows. * * An observer only reports CHANGES of visibility, so a sentinel still in view * after its chunk landed (a fast scrollbar drag, rows shorter than the panel) * would never report again and the list would stall. Observing it afresh * delivers its current state once more, so the budget keeps growing until the * sentinel leaves the view. That happens when rows were added or the sentinel * element changed — never merely because rows were removed: a failed async page * drops its loading row while the sentinel stays in view, and re-observing then * would re-request the failed page in a loop instead of on the next scroll. */ rearm(): void { if (this.observer == null) { return; } const sentinel = document.getElementById(this.owner.sentinelId()); const rows = Math.min(this.owner.getLimit(), this.owner.rowCount()); const armed = this.armed; if (armed != null && sentinel === armed.sentinel && rows <= armed.rows) { if (rows < armed.rows) { this.armed = { sentinel, rows }; } return; } this.observer.disconnect(); if (sentinel != null) { this.observer.observe(sentinel); } this.armed = { sentinel, rows }; } /** The owner shows a new list (a new query or new results): the next `rearm` observes its sentinel afresh. */ reset(): void { this.armed = null; } /** Stops observing for good. Idempotent. */ dispose(): void { this.observer?.disconnect(); this.armed = null; } /** The sentinel is in view: render the next chunk, or tell the owner every row is rendered. */ private grow(): void { const limit = this.owner.getLimit(); if (limit < this.owner.rowCount()) { this.owner.setLimit(limit + ChunkedRenderWindow.CHUNK); return; } this.owner.onExhausted?.(); } }