// The tile scheduler (#69, spec #63 decision 12). Pure: the loader is injected, so the concurrency, // cache and denial rules are unit-tested without a browser or a GPU. // // Rules encoded here: // - at most TILE_CONCURRENCY (8) requests in flight; // - the wanted list is the viewport plus one tile of margin, in viewport order; // - a tile is never fetched twice (a decoded texture is cached by the tile module across a // reconnect; this scheduler remembers settled URLs too); // - a `403`/`404` is a permanent `denied` answer: never retried in a loop (the server said no); // - a transient `error` is retried only when a later viewport sync asks for the tile again; // - queued tiles that leave the viewport before they start are dropped, so a fast pan never // prefetches beyond the margin. export const TILE_CONCURRENCY = 8 export type TileLoadResult = 'ok' | 'denied' | 'error' export type TileStatus = 'queued' | 'loading' | 'ok' | 'denied' | 'error' export interface TileSchedulerOptions { load(url: string): Promise onSettled?(url: string, result: TileLoadResult): void onIdle?(): void concurrency?: number } function sameList(a: readonly string[], b: readonly string[]): boolean { if (a.length !== b.length) return false for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false return true } export class TileScheduler { readonly #load: (url: string) => Promise readonly #onSettled: ((url: string, result: TileLoadResult) => void) | undefined readonly #onIdle: (() => void) | undefined readonly #concurrency: number #states = new Map() #queue: string[] = [] #desired = new Set() #lastDesired: readonly string[] = [] #active = 0 #idle = true constructor(options: TileSchedulerOptions) { this.#load = options.load this.#onSettled = options.onSettled this.#onIdle = options.onIdle this.#concurrency = Math.max(1, options.concurrency ?? TILE_CONCURRENCY) } status(url: string): TileStatus | undefined { return this.#states.get(url) } get active(): number { return this.#active } /** What the viewport wants now. `urls` is already in viewport order. */ setDesired(urls: readonly string[]): void { // A settle calls back into the renderer, which syncs again with the same window; skip the // O(n) rebuild in that common case. It still pumps, which is what opens the next slot. if (sameList(urls, this.#lastDesired)) { this.#pump() return } this.#lastDesired = [...urls] this.#desired = new Set(urls) // Keep queued entries that are still wanted, in the new viewport order; forget the rest so a // later viewport that asks for the tile again can queue it fresh. const order = new Map(urls.map((url, index) => [url, index])) const dropped = this.#queue.filter(url => !this.#desired.has(url)) for (const url of dropped) this.#states.delete(url) this.#queue = this.#queue .filter(url => this.#desired.has(url)) .sort((a, b) => (order.get(a) ?? 0) - (order.get(b) ?? 0)) for (const url of urls) { const existing = this.#states.get(url) if (existing === 'error') this.#states.delete(url) else if (existing !== undefined) continue this.#states.set(url, 'queued') this.#queue.push(url) } this.#pump() } /** A texture already in the cache (e.g. a renderer remount) needs no request. */ markLoaded(url: string): void { if (!this.#states.has(url)) this.#states.set(url, 'ok') } /** Forget transient failures: the next sync (e.g. after a reconnect) may ask again. */ retryFailed(): void { for (const [url, status] of this.#states) if (status === 'error') this.#states.delete(url) this.#lastDesired = [] } /** True when every wanted tile settled with an `ok` or `denied` answer. */ isComplete(urls: readonly string[]): boolean { return urls.every(url => { const status = this.#states.get(url) return status === 'ok' || status === 'denied' }) } #pump(): void { while (this.#active < this.#concurrency && this.#queue.length > 0) { const url = this.#queue.shift() if (url === undefined) break if (!this.#desired.has(url) || this.#states.get(url) !== 'queued') continue this.#states.set(url, 'loading') this.#active += 1 this.#idle = false void this.#load(url).then( result => this.#settle(url, result), () => this.#settle(url, 'error'), ) } if (this.#active === 0 && this.#queue.length === 0 && !this.#idle) { this.#idle = true this.#onIdle?.() } } #settle(url: string, result: TileLoadResult): void { this.#active -= 1 if (result === 'ok' || result === 'denied') { this.#states.set(url, result) } else { // Transient: remember the failure, but never retry it on the same viewport; only a new // window or `retryFailed()` (reconnect) asks again. A denied 403 is never retried at all. this.#states.set(url, 'error') } this.#onSettled?.(url, result) this.#pump() } }