Files
FFF_vtt_frozen/client/src/tiles.ts
T
SirStone 4534ed6426 feat(client): map tiles, grid and pan/zoom (#69)
T6 of spec #63. Tiles are requested only for the viewport plus one tile of
margin, in viewport order, at most 8 at a time (TileScheduler); decoded
textures are cached by URL so a reconnect refetches nothing; a 403/404 is
remembered as denied and never retried. The browser image loader is the hot
path, with one status fetch only to classify an error.

Camera: drag to pan, wheel/pinch to zoom around the pointer/midpoint; players
clamp at 0.5 (ADR-0013) and the GM has no player floor; both stop at 4.0.
Grid is one TilingSprite toggled by #grid-toggle. Camera is local UI state,
not a reducer slice, so hello/reconnect does not move the view.

Tests: viewport/tile unit tests (clamps, margin window, 8-wide queue, denial,
cache) and client/e2e/map.test.mjs (mobile viewport fill gate (a), grid,
pinch, cache across reconnect, GM zoom, 403 no-retry) against the real
server through TLS/h2. Tile-size gate (a) documented in client/README.md.
2026-11-21 01:11:12 +01:00

139 lines
5.0 KiB
TypeScript

// 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<TileLoadResult>
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<TileLoadResult>
readonly #onSettled: ((url: string, result: TileLoadResult) => void) | undefined
readonly #onIdle: (() => void) | undefined
readonly #concurrency: number
#states = new Map<string, TileStatus>()
#queue: string[] = []
#desired = new Set<string>()
#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()
}
}