diff --git a/src/app/features/scene/ridge/Backdrop.test.ts b/src/app/features/scene/ridge/Backdrop.test.ts index a0adc569..1292e382 100644 --- a/src/app/features/scene/ridge/Backdrop.test.ts +++ b/src/app/features/scene/ridge/Backdrop.test.ts @@ -240,6 +240,26 @@ describe('createBackdrop', () => { residentTiles(manifest.view, 220, 36 + LOAD_MARGIN_TILES * 11.25).forEach((t) => expect(held.has(t.tile)).toBe(true)); }); + it('tells a listener of each tile as it is made, not of a tile that failed, and no longer once it stops listening or the backdrop is disposed', async () => { + const r = rig((url) => tileOf(url) === 17); + const bd = createBackdrop(manifest, sky, { ...r, lighter: r.lighter }); + const seen: number[] = []; + const off = bd.onTile(() => seen.push(bd.litTiles().length)); + await bd.ensure({ fromDeg: 180, toDeg: 181 }); // tiles 15 to 17 and the margin: 17 fails + const made = new Set(bd.litTiles().map((t) => t.tile)); + expect(made.has(17)).toBe(false); + expect(seen).toHaveLength(made.size); // one call per tile held, each with its tile already held + seen.forEach((n, i) => expect(n).toBeGreaterThan(i)); + off(); + await bd.ensure({ fromDeg: 0, toDeg: 1 }); + expect(seen).toHaveLength(made.size); + const late = vi.fn(); + bd.onTile(late); + bd.dispose(); + await bd.ensure({ fromDeg: 100, toDeg: 101 }); + expect(late).not.toHaveBeenCalled(); + }); + it('disposes: the canvases are dropped, the lighting context released, and a later ensure does nothing', async () => { const r = rig(); const lighter = r.lighter(); diff --git a/src/app/features/scene/ridge/Backdrop.ts b/src/app/features/scene/ridge/Backdrop.ts index 0cf80775..79dbf778 100644 --- a/src/app/features/scene/ridge/Backdrop.ts +++ b/src/app/features/scene/ridge/Backdrop.ts @@ -31,6 +31,8 @@ export interface Backdrop { ensure(arc: Arc, first?: Arc): Promise; /** Every card of every tile held, in no order. */ litTiles(): readonly LitTile[]; + /** Hear each tile as it is made and held, however late (a still scene has no timer of its own and draws only what it held); not a tile that failed. Returns the way to stop. */ + onTile(cb: () => void): () => void; /** Let go of everything, the lighting context included. Idempotent. */ dispose(): void; } @@ -83,6 +85,7 @@ export function createBackdrop( const held = new Map(); /** Tiles that failed, when they may be asked for again and the pause after the next failure. */ const failed = new Map(); + const heard = new Set<() => void>(); let disposed = false; let running: Promise | null = null; let want: { arc: Arc; first?: Arc } | null = null; @@ -173,6 +176,7 @@ export function createBackdrop( } held.set(tile, made); failed.delete(tile); + heard.forEach((cb) => cb()); } catch { made.forEach((t) => (t.canvas.width = 0)); const pause = Math.min(RETRY_MAX_MS, (failed.get(tile)?.pause ?? RETRY_FIRST_MS / 2) * 2); @@ -234,9 +238,14 @@ export function createBackdrop( return running; }, litTiles: () => [...held.values()].flat(), + onTile(cb) { + heard.add(cb); + return () => heard.delete(cb); + }, dispose() { if (disposed) return; disposed = true; + heard.clear(); want = null; current?.abort(); // a scene that did not wait for its tiles, or was replaced, leaves nothing asking for them [...held.keys()].forEach(drop); diff --git a/src/app/features/scene/ridge/Ridge.test.ts b/src/app/features/scene/ridge/Ridge.test.ts index 420c658b..dc33f5a6 100644 --- a/src/app/features/scene/ridge/Ridge.test.ts +++ b/src/app/features/scene/ridge/Ridge.test.ts @@ -568,6 +568,7 @@ describe('the ridge cards in the scene', () => { manifest, ensure: vi.fn(async (_arc: Arc): Promise => undefined), litTiles: () => lit, + onTile: () => () => undefined, dispose: vi.fn(), }; }; @@ -867,6 +868,7 @@ describe('the ridge cards in the scene', () => { manifest: m, ensure: vi.fn(async (_arc: Arc, _first?: Arc): Promise => undefined), litTiles: () => lit, + onTile: () => () => undefined, dispose: vi.fn(), }; }; @@ -1106,6 +1108,93 @@ describe('the ridge cards in the scene', () => { expect(parent.children).toEqual([canvas]); }); + describe('a still whose tiles arrive after its paint', () => { + /** A backdrop holding the tiles of `held`, and tiles that arrive afterwards: `arrive` holds one more and tells whoever listens, as the loader does. */ + const ring = parseManifest(wireManifest({}, 3, 32), '/d/'); // 32 tiles of 11.25 degrees: the rest view shows tiles 16 to 21 + const arriving = (held: number[]) => { + const all = ring.tiles.flatMap((row, tile) => + row.flatMap((e, card) => { + if (!e) return []; + const canvas = fakeEl('canvas'); + canvas.id = `t${String(tile).padStart(2, '0')}c${card}`; + canvas.width = 202; + return [{ tile, card, canvas: canvas as unknown as HTMLCanvasElement }]; + }) + ); + const lit = all.filter((t) => held.includes(t.tile)); + const heard = new Set<() => void>(); + const bd = { + manifest: ring, + ensure: vi.fn(async (_arc: Arc): Promise => undefined), + litTiles: () => lit, + onTile: (cb: () => void) => (heard.add(cb), () => void heard.delete(cb)), + dispose: vi.fn(), + }; + const arrive = (tile: number) => { + lit.push(...all.filter((t) => t.tile === tile)); + heard.forEach((cb) => cb()); + }; + return { bd, arrive, listeners: () => heard.size }; + }; + const drawnTiles = () => new Set(images.filter((a) => (a[0] as any).id).map((a: any) => a[0].id.slice(0, 3))); + const timers = () => { + vi.useFakeTimers(); + (window as any).setTimeout = setTimeout; + (window as any).clearTimeout = clearTimeout; + }; + afterEach(() => { + vi.useRealTimers(); + }); + + it('repaints once for a burst of arrivals, with every tile held by then, asks for nothing, and is done when the tiles stop', () => { + timers(); + const { bd, arrive } = arriving([18]); + const { canvas } = host(); + const s = createRidgeScene({ canvas, tier: 'still', sky: DAY, backdrop: bd })!; + expect(drawnTiles()).toEqual(new Set(['t18'])); + const first = images.length; + arrive(17); + vi.advanceTimersByTime(100); + arrive(19); // a burst: the wait starts again + vi.advanceTimersByTime(200); + expect(images.length).toBe(first); // not yet + vi.advanceTimersByTime(60); + expect(drawnTiles()).toEqual(new Set(['t18', 't17', 't19'])); // one paint, with all of them + const second = images.length; + expect(second).toBeGreaterThan(first); + vi.advanceTimersByTime(600_000); + expect(images.length).toBe(second); // no loop, no timer left + expect(bd.ensure).not.toHaveBeenCalled(); + arrive(20); + vi.advanceTimersByTime(300); + expect(drawnTiles().has('t20')).toBe(true); // a straggler is drawn too + s.dispose(); + }); + + it('is the same after a settle from the live tier, and stops listening with the scene', () => { + timers(); + const { bd, arrive, listeners } = arriving([18]); + const { canvas } = host(); + const s = createRidgeScene({ canvas, tier: 'full', sky: DAY, backdrop: bd })!; + arrive(17); + vi.advanceTimersByTime(1_000); + expect(images.length).toBe(0); // the live tier mounts tiles from its own timer: nothing is painted into the host canvas + runFrames(100, 400); // a poor frame rate: it settles to a still + const settled = images.length; + expect(drawnTiles()).toEqual(new Set(['t18', 't17'])); + arrive(19); + vi.advanceTimersByTime(300); + expect(images.length).toBeGreaterThan(settled); + expect(drawnTiles().has('t19')).toBe(true); + s.dispose(); + expect(listeners()).toBe(0); + const after = images.length; + arrive(20); + vi.advanceTimersByTime(1_000); + expect(images.length).toBe(after); + }); + }); + it('settles to a still with the tiles painted when the frame rate is poor', () => { const { parent, canvas } = host(); createRidgeScene({ canvas, tier: 'full', sky: DAY, backdrop: litCards() }); diff --git a/src/app/features/scene/ridge/Ridge.ts b/src/app/features/scene/ridge/Ridge.ts index 69ddc000..d26186d5 100644 --- a/src/app/features/scene/ridge/Ridge.ts +++ b/src/app/features/scene/ridge/Ridge.ts @@ -610,7 +610,8 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null { sampler.reset(); wake(); }; - const onResize = () => { + /** Make the scene again at the size it has now and settle as it was: after a resize, and after tiles arrive into a still. A burst of either is one rebuild, 250 ms after the last. */ + const rebuild = () => { clearTimeout(resizeTimer); resizeTimer = window.setTimeout(() => { if (disposed) return; @@ -623,8 +624,13 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null { } }, 250); }; + // A still draws once and has no timer to ask for tiles, so it is told of each one that is made after its paint (the deadline of a scene start leaves the rest of the + // first arc loading); the live tier mounts them on its own timer. Nothing asks for more: a repaint adds no request, so the fill-in ends with the last tile. + const offTile = opts.backdrop?.onTile(() => { + if (still && !disposed) rebuild(); + }); document.addEventListener('visibilitychange', onVisibility); - window.addEventListener('resize', onResize); + window.addEventListener('resize', rebuild); const onMotionChange = () => { built?.rig?.sync(); syncPlayState(); @@ -639,10 +645,11 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null { if (disposed) return; disposed = true; cancelAnimationFrame(raf); + offTile?.(); opts.backdrop?.dispose(); clearTimeout(resizeTimer); document.removeEventListener('visibilitychange', onVisibility); - window.removeEventListener('resize', onResize); + window.removeEventListener('resize', rebuild); reducedMotion?.removeEventListener?.('change', onMotionChange); teardown(); readout?.remove();