From 27356ff7c420947a69596b003e46a2a97fe4e3d0 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 23 Nov 2026 04:22:47 +0100 Subject: [PATCH] scene start: one lifecycle rule, a new backdrop replaces the shown scene once it holds tiles A build a newer one supersedes is given up at once (its backdrop disposed, its requests aborted) instead of running to the end of its pass: five quick sky picks on a slow link left five backdrops and lighting contexts alive. A rebuild that ends with no tile no longer swaps a ridge that was fine for the sky alone and tells the host its planes failed: the scene that is up stays, its own backdrop is disposed, and the next change of sky tries again. Only a first load that ends with no tile is the sky alone. Co-Authored-By: Claude Sonnet 5.5 --- src/app/features/scene/index.test.ts | 54 +++++++++++++++++++++++++--- src/app/features/scene/index.ts | 47 ++++++++++++++++-------- 2 files changed, 82 insertions(+), 19 deletions(-) diff --git a/src/app/features/scene/index.test.ts b/src/app/features/scene/index.test.ts index 7625257c..ce212cb4 100644 --- a/src/app/features/scene/index.test.ts +++ b/src/app/features/scene/index.test.ts @@ -1009,14 +1009,13 @@ describe('the ridge cards and the live scene', () => { expect(onActive).not.toHaveBeenCalled(); }); - it('a rebuild that makes no scene says nothing about planes and leaves the host inactive', async () => { + it('a rebuild whose scene cannot be made says nothing about planes and leaves the host inactive', async () => { serve(); - const d = loader(); + loader(); createRidgeScene.mockReturnValueOnce(fakeScene()).mockReturnValueOnce(null); const onPlanesFail = vi.fn(); const onInactive = vi.fn(); const scene = await startBackgroundScene(config, caps, { canvas, onPlanesFail, onInactive }); - d.ctl.fail = true; setSkyChoice(NOON); await flush(); expect(onInactive).toHaveBeenCalledOnce(); @@ -1024,6 +1023,50 @@ describe('the ridge cards and the live scene', () => { scene?.dispose(); }); + it('a rebuild that ends with no tile keeps the scene that is up: nothing is swapped, nothing is told, its backdrop is disposed, and the next change of sky tries again', async () => { + serve(); + const d = loader(); + const [first, second] = [fakeScene(), fakeScene()]; + createRidgeScene.mockReturnValueOnce(first).mockReturnValueOnce(second); + const [onPlanesFail, onInactive, onActive] = [vi.fn(), vi.fn(), vi.fn()]; + const scene = await startBackgroundScene(config, caps, { canvas, onPlanesFail, onInactive, onActive }); + d.ctl.fail = true; // the link blinks for the whole pass of the rebuild + setSkyChoice(NOON); + await flush(); + expect(createRidgeScene).toHaveBeenCalledOnce(); + expect(first.dispose).not.toHaveBeenCalled(); // the ridge that was fine is still up + expect(d.ctl.made[1].dispose).toHaveBeenCalledOnce(); + expect([onPlanesFail, onInactive, onActive].map((f) => f.mock.calls.length)).toEqual([0, 0, 0]); + d.ctl.fail = false; + setSkyChoice(DUSK); // the next change of sky: the one that failed was never drawn, and this is built now + await flush(); + expect(createRidgeScene).toHaveBeenCalledTimes(2); + expect(first.dispose).toHaveBeenCalledOnce(); + expect(onActive).toHaveBeenCalledOnce(); + scene?.dispose(); + }); + + it('a build a newer one supersedes is given up at once: its backdrop is disposed when the newer begins, not when its own pass ends', async () => { + serve(); + const d = loader(); + createRidgeScene.mockReturnValue(fakeScene()); + const scene = await startBackgroundScene(config, caps, { canvas }); + d.ctl.hold = true; // a slow link: no pass ends + for (const choice of [NOON, DUSK, NIGHT, NOON, DUSK]) { + setSkyChoice(choice); + await flush(); + } + const [live, ...builds] = d.ctl.made; + expect(builds).toHaveLength(5); + builds.slice(0, 4).forEach((b) => expect(b.dispose).toHaveBeenCalledOnce()); // all but the newest, while their requests are still out + expect(builds[4].dispose).not.toHaveBeenCalled(); + expect(live.dispose).not.toHaveBeenCalled(); // the scene that is up is not theirs to give up + scene?.dispose(); + expect(builds[4].dispose).toHaveBeenCalledOnce(); // and disposing the door gives up the one in flight + await d.release(); + expect(createRidgeScene).toHaveBeenCalledOnce(); + }); + it('tells the host when the live scene loses its planes and when it gets them back', async () => { serve(); const d = loader(); @@ -1042,10 +1085,11 @@ describe('the ridge cards and the live scene', () => { await flush(); expect(onPlanesOk).toHaveBeenCalledOnce(); expect(createRidgeScene.mock.calls[2][0].backdrop).toBeDefined(); - d.ctl.fail = true; + d.ctl.fail = true; // a rebuild that ends with no tile does not take the planes from the scene that has them setSkyChoice(NIGHT); await flush(); - expect(onPlanesFail).toHaveBeenCalledTimes(2); + expect(onPlanesFail).toHaveBeenCalledOnce(); + expect(createRidgeScene).toHaveBeenCalledTimes(3); scene?.dispose(); }); }); diff --git a/src/app/features/scene/index.ts b/src/app/features/scene/index.ts index 66bbcd01..0ffa17dd 100644 --- a/src/app/features/scene/index.ts +++ b/src/app/features/scene/index.ts @@ -36,25 +36,32 @@ interface Prepared { planesFail: Error | null; } -/** How long the first scene start waits for the first tiles, ms: past it the scene draws with the tiles it has and the rest arrive into it; with none, it is the sky alone. A rebuild has no deadline: the scene it replaces stays up until it has its tiles. */ +/** How long the first scene start waits for the first tiles, ms: past it the scene draws with the tiles it has and the rest arrive into it; with none, it is the sky alone. A rebuild has no deadline: the scene it replaces stays up until the new one holds tiles. */ export const PREPARE_DEADLINE_MS = 6_000; async function prepare( config: BackgroundSceneConfig | undefined, options: StartOptions & { tier: SceneTier; debug?: boolean }, choice: SkyChoice, - first: boolean + first: boolean, + superseded: AbortSignal ): Promise { // Whatever throws in making the scene is a compile failure, as whatever throws in loading it. let backdrop: Backdrop | null = null; + /** Give up the backdrop this build made, once: when a newer build supersedes it, or when it is not the scene's. */ + const release = () => { + backdrop?.dispose(); + backdrop = null; + }; + superseded.addEventListener('abort', release, { once: true }); const guarded = (make: () => Scene | null): Prepared => ({ planesFail: null, - discard: () => backdrop?.dispose(), + discard: release, make: () => { try { return make(); } catch (e) { - backdrop?.dispose(); + release(); options.onCompileFail?.(e instanceof Error ? e : new Error(String(e))); return null; } @@ -80,10 +87,7 @@ async function prepare( const manifestSky = ridgeSkyFor(choice, manifest.view); const { createBackdrop } = await import('./ridge/Backdrop'); const heading = await import('./ridge/heading'); - backdrop = createBackdrop( - manifest, - manifestSky.physical as NonNullable - ); + backdrop = createBackdrop(manifest, manifestSky.physical as NonNullable); // A place not shown before faces its rest azimuth; a rebuild for a sky keeps the heading the view has turned to. The speed is bounded from here on, not // from the scene's first frame: the view does not turn further than the place sustains while the tiles load. const now = performance.now(); @@ -102,11 +106,11 @@ async function prepare( } finally { clearTimeout(timer); } + if (superseded.aborted) throw new Error('ridge: superseded'); if (backdrop.litTiles().length === 0) throw new Error('ridge cards did not load'); lit = manifestSky; } catch (e) { - backdrop?.dispose(); - backdrop = null; + release(); planesFail = e instanceof Error ? e : new Error(String(e)); } } @@ -174,21 +178,25 @@ export async function startBackgroundScene( if (!planesFail && planesFailed) options.onPlanesOk?.(); planesFailed = planesFail !== null; }; - const prepared = (choice: SkyChoice, first: boolean) => prepare(config, { ...options, tier, debug }, choice, first); + const prepared = (choice: SkyChoice, first: boolean, stop: AbortSignal) => prepare(config, { ...options, tier, debug }, choice, first, stop); resetSkyPlanes(); const choice = getSkyChoice(); const key0 = config?.style === 'raymarch' ? null : await skyKey(config, choice); - const firstReady = await prepared(choice, true); + const firstReady = await prepared(choice, true, new AbortController().signal); const first = firstReady.make(); if (!first || config?.style === 'raymarch') return first; announce(firstReady.planesFail); let inner: Scene | null = first; let built = key0; + let shown = key0; // the sky key of the scene that is up let loggedIn = false; let disposed = false; let generation = 0; - // A rebuild is for a different sky ('auto' crossing a boundary, a new choice), not for a repeat of the one drawn. + let building: AbortController | undefined; + // A rebuild is for a different sky ('auto' crossing a boundary, a new choice), not for a repeat of the one drawn. What replaces the scene that is up: a new backdrop, + // once it holds tiles; a build a newer one supersedes is given up at once (its requests aborted); a build that ends with no tile never swaps a ridge for the sky alone, + // the next change of sky tries again. Only a first load that ends with no tile is the sky alone. const rebuild = async () => { // The choice can move while the key is awaited; that change has its own rebuild, so this one stands down. const next = getSkyChoice(); @@ -198,16 +206,26 @@ export async function startBackgroundScene( generation += 1; const mine = generation; built = key; + building?.abort(); + const mineAborts = new AbortController(); + building = mineAborts; // The old scene stays up while the next one loads; a build that is stale or after dispose is never made, so it never speaks. - const ready = await prepared(next, false); + const ready = await prepared(next, false, mineAborts.signal); if (disposed || mine !== generation) { ready.discard(); return; } + building = undefined; + if (ready.planesFail && !planesFailed) { + ready.discard(); + built = shown; + return; + } inner?.dispose(); inner = null; const scene = ready.make(); inner = scene; + shown = key; if (scene) announce(ready.planesFail); if (!scene) options.onInactive?.(); else { @@ -227,6 +245,7 @@ export async function startBackgroundScene( if (disposed) return; disposed = true; off(); + building?.abort(); inner?.dispose(); inner = null; },