fix(client): tile loading: centre-out, one request per tile, 404 recovery (#86)

The GM survey view asked row-major, at most 8 at a time, rebuilt the whole
wanted window on every settle, and paid two requests for every failed tile
(image error, then a fetch to learn the status). A 404/5xx was stored as
permanent `denied`, so a tile imported after page load stayed black until a
page reload, and `viewport.setScene` swallowed a same-id art_version change
so the old textures were never dropped.

- TILE_CONCURRENCY 8 -> 32, the measured knee on a Full HD scene (min-of-3
  headless: GM fit + player 0.5, tile 32/64; 64 is inside the noise).
- orderTilesCentreOut: nearest the viewport centre first.
- Settle path is incremental (one settle = one tile sprite/flag update);
  the window is rebuilt only on camera/scene changes.
- fetch + createImageBitmap: status from the same response, decode off the
  main thread, so a failure costs one request.
- 403 stays permanent (retried only on fog.fresh); 404/5xx/network retry
  with 250/500 ms backoff up to 3 loads, and `hello`/reconnect or a
  scene/art_version change resets the budget. A failure that lands after a
  reconnect is a fresh first attempt; a failure that leaves the window does
  not spend the budget.
- setScene emits for a same-id scene; a new art_version drops the texture
  cache and reschedules. Tiles are `Cache-Control: private, max-age=1y,
  immutable` (still gated), so a reload reuses them; DESIGN 3.3/11.1 and
  client/README updated.

Tests: vitest for ordering, incremental invariants, retry budget/backoff,
reconnect epoch and art_version invalidation; e2e for a GM survey fill (one
request per succeeded tile) and a 404 tile recovering after the file appears
and a real reconnect. Before -> after (min-of-3, saturated host, Full HD):
GM fit 32px 910->843 ms, player 0.5 32px 972->862 ms, GM fit 64px
320->249 ms, player 0.5 64px 332->368 ms (noise).

Part of #63. Blocks #77.
This commit is contained in:
2026-11-21 22:28:18 +01:00
parent 54dbd68587
commit 52ab50d765
11 changed files with 676 additions and 134 deletions
+35
View File
@@ -12,6 +12,7 @@ import {
ViewportModel,
clampZoom,
centredCamera,
orderTilesCentreOut,
pinchCamera,
screenToWorld,
tileUrl,
@@ -137,6 +138,40 @@ describe('ViewportModel (the render layer\'s UI state)', () => {
model.setScene({ ...scene, id: 2 })
expect(model.state.camera).toEqual(centredCamera({ ...scene, id: 2 }, size))
})
it('emits on a same-scene resync so a new art_version is not swallowed (#86)', () => {
const model = new ViewportModel()
let seen = 0
model.reset(scene, 'player', size)
model.subscribe(() => {
seen += 1
})
const panned = model.state.camera
model.setScene({ ...scene, art_version: 2 })
expect(seen).toBe(1)
expect(model.state.scene?.art_version).toBe(2)
expect(model.state.camera).toBe(panned)
// The same object again is not a change: no extra notification.
model.setScene(model.state.scene)
expect(seen).toBe(1)
})
})
describe('centre-out tile order (#86)', () => {
it('asks for the tile under the viewport centre first, growing outwards', () => {
const camera = centredCamera(scene, size)
const tiles = orderTilesCentreOut(visibleTiles(scene, camera, size), scene, camera, size)
// 320x320 viewport at zoom 1 centred on world (2048,2048): the four middle tiles tie.
expect(tiles.length).toBe(12 * 12)
const distance = (tile: { x: number; y: number }) =>
Math.hypot((tile.x + 0.5) * scene.tile_size - 2048, (tile.y + 0.5) * scene.tile_size - 2048)
const distances = tiles.map(distance)
expect(distances).toEqual([...distances].sort((a, b) => a - b))
expect(Math.abs((tiles[0]?.x ?? -1) - 63)).toBeLessThanOrEqual(1)
expect(Math.abs((tiles[0]?.y ?? -1) - 63)).toBeLessThanOrEqual(1)
// It is a permutation of the row-major window, not a different window.
expect(new Set(tiles.map(tile => tile.url)).size).toBe(12 * 12)
})
})
describe('visible tiles', () => {