diff --git a/src/app/features/scene/ridge/Backdrop.test.ts b/src/app/features/scene/ridge/Backdrop.test.ts index 1292e382..04ee6185 100644 --- a/src/app/features/scene/ridge/Backdrop.test.ts +++ b/src/app/features/scene/ridge/Backdrop.test.ts @@ -380,6 +380,68 @@ describe('createBackdrop', () => { expect(bd.litTiles().some((t) => t.tile === 19)).toBe(false); }); + describe('the loader asks again on its own', () => { + /** Nothing waits on a timer but the retry: files and decodes settle at once. */ + const quick = (fail: (url: string) => boolean) => { + const r = rig(fail); + const decode = vi.fn(async (blob: Blob) => ({ url: (blob as unknown as { url: string }).url, close: () => undefined }) as unknown as ImageBitmap); + return { r, deps: { ...r, decode, lighter: r.lighter } }; + }; + const tile = (r: ReturnType, t: number) => r.fetched.filter((u) => tileOf(u) === t && u.includes('card-1-') && u.endsWith('colour.webp')).length; + + it('a tile that failed is asked for again at the end of its pause with no one asking, and the listener hears it arrive', async () => { + vi.useFakeTimers(); + let broken = true; + const { r, deps } = quick((u) => broken && tileOf(u) === 16 && u.includes('card-1-')); + const bd = createBackdrop(manifest, sky, deps); + const heard = vi.fn(); + bd.onTile(heard); + await bd.ensure({ fromDeg: 180, toDeg: 181 }); // the only ask: a still has no timer to make another + expect(bd.litTiles().some((t) => t.tile === 16)).toBe(false); + const arrived = heard.mock.calls.length; + broken = false; + r.tick(RETRY_FIRST_MS - 1); + await vi.advanceTimersByTimeAsync(RETRY_FIRST_MS - 1); + expect(tile(r, 16)).toBe(1); // not before its pause + r.tick(1); + await vi.advanceTimersByTimeAsync(1); + expect(bd.litTiles().filter((t) => t.tile === 16).map((t) => t.card).sort()).toEqual([0, 1, 2]); + expect(heard.mock.calls.length).toBe(arrived + 1); + const n = r.fetched.length; + r.tick(10 * RETRY_MAX_MS); + await vi.advanceTimersByTimeAsync(10 * RETRY_MAX_MS); + expect(r.fetched.length).toBe(n); // nothing is left to ask for + bd.dispose(); + }); + + it('keeps asking, on a pause that doubles, while the arc wants the tile, and stops when the arc has moved on or the backdrop is gone', async () => { + vi.useFakeTimers(); + const { r, deps } = quick((u) => tileOf(u) === 19); + const bd = createBackdrop(manifest, sky, deps); + await bd.ensure({ fromDeg: 215, toDeg: 216 }); + expect(tile(r, 19)).toBe(1); + for (let s = 0; s < 120; s += 1) { + r.tick(1000); + await vi.advanceTimersByTimeAsync(1000); + } + expect(tile(r, 19)).toBeGreaterThan(3); + expect(tile(r, 19)).toBeLessThanOrEqual(8); // 1 s, 2 s, 4 s, 8 s, 16 s, 32 s: a few a minute, not one a second + await bd.ensure({ fromDeg: 0, toDeg: 1 }); // the view has gone the other way: tile 19 is no longer wanted + const asked = tile(r, 19); + for (let s = 0; s < 300; s += 1) { + r.tick(1000); + await vi.advanceTimersByTimeAsync(1000); + } + expect(tile(r, 19)).toBe(asked); + await bd.ensure({ fromDeg: 215, toDeg: 216 }); + bd.dispose(); + const n = r.fetched.length; + r.tick(10 * RETRY_MAX_MS); + await vi.advanceTimersByTimeAsync(10 * RETRY_MAX_MS); + expect(r.fetched.length).toBe(n); // a disposed backdrop asks for nothing + }); + }); + it('does not ask again for a file the server does not have: the tile is drawn with the cards that came', async () => { const r = rig( () => false, diff --git a/src/app/features/scene/ridge/Backdrop.ts b/src/app/features/scene/ridge/Backdrop.ts index 79dbf778..13ef8d79 100644 --- a/src/app/features/scene/ridge/Backdrop.ts +++ b/src/app/features/scene/ridge/Backdrop.ts @@ -1,8 +1,9 @@ // The ridge cards the scene composes, lit, kept resident by tile: the one owner of which tiles of the ring are in memory. `ensure` is told the arc of // azimuth the view is about to need; it fetches and decodes the tiles of that arc it does not hold, one tile at a time, lights each card of a tile // (`lightCards.ts`, one lighter for the one sky this backdrop was made for) and drops the tiles beyond the arc and its margin. The files of the tiles to make -// are fetched a few at a time, each against a timeout. A tile that fails is a tile the scene draws as absence, asked for again after a growing pause; a file -// the server says is not there (404) is not asked for again while its tile stays: the scene draws the cards of the tile that came. Nothing is thrown into the scene. +// are fetched a few at a time, each against a timeout. A tile that fails is a tile the scene draws as absence, asked for again by the loader itself after a growing +// pause, for as long as the arc last asked for wants it (whoever asked, a scene's timer or none: a still has no timer); a file the server says is not there (404) is +// not asked for again while its tile stays: the scene draws the cards of the tile that came. Nothing is thrown into the scene. import type { RidgeManifest } from './manifest'; import type { PhysicalSky } from './physicalSky'; import { createLighter } from './lightCards'; @@ -89,6 +90,11 @@ export function createBackdrop( let disposed = false; let running: Promise | null = null; let want: { arc: Arc; first?: Arc } | null = null; + /** The arc last asked for: what the loader keeps its tiles for and retries for. */ + let wanted: { arc: Arc; first?: Arc } | null = null; + /** Ask for the arc wanted once more: a pass ends in `schedule`, which is defined before the backdrop that can be asked. */ + let askAgain: () => void = () => undefined; + let retry: ReturnType | undefined; /** The pass that is on the wire: dispose gives up its requests, as a newer arc does. */ let current: AbortController | null = null; @@ -219,19 +225,38 @@ export function createBackdrop( } } + /** When the arc wanted holds a tile that failed, ask for it again at the end of its pause: the loader's own retry, the same for every tier. */ + function schedule(): void { + clearTimeout(retry); + retry = undefined; + if (disposed || !wanted) return; + const { arc } = wanted; + const half = (arc.toDeg - arc.fromDeg) / 2 + LOAD_MARGIN_TILES * tileDeg; + const due = residentTiles(view, (arc.fromDeg + arc.toDeg) / 2, half) + .filter((r) => !held.has(r.tile) && failed.has(r.tile)) + .map((r) => (failed.get(r.tile) as { until: number }).until); + if (!due.length) return; + retry = setTimeout(() => { + retry = undefined; + askAgain(); + }, Math.max(0, Math.min(...due) - now())); + } + async function loop(): Promise { while (want && !disposed) { const { arc, first } = want; want = null; await pass(arc, first); } + schedule(); } - return { + const api: Backdrop = { manifest, ensure(arc, first) { if (disposed) return Promise.resolve(); - want = { arc, first }; + wanted = { arc, first }; + want = wanted; running ??= loop().finally(() => { running = null; }); @@ -246,10 +271,16 @@ export function createBackdrop( if (disposed) return; disposed = true; heard.clear(); + wanted = null; want = null; + clearTimeout(retry); current?.abort(); // a scene that did not wait for its tiles, or was replaced, leaves nothing asking for them [...held.keys()].forEach(drop); lighter.release(); }, }; + askAgain = () => { + if (wanted) api.ensure(wanted.arc, wanted.first).catch(() => undefined); + }; + return api; }