diff --git a/src/app/features/scene/Scene.ts b/src/app/features/scene/Scene.ts index de80583c..c2f0423a 100644 --- a/src/app/features/scene/Scene.ts +++ b/src/app/features/scene/Scene.ts @@ -599,8 +599,9 @@ export function createScene(opts: SceneOptions): Scene | null { } listen(window, 'resize', () => wake()); // A lost context takes every texture and program with it and nothing here rebuilds them, so the - // scene ends and the host decides what to show. preventDefault: otherwise the browser never - // restores the context, which is what we want, and a restore would find a dead scene. + // scene ends and the host decides what to show. preventDefault is what lets the browser restore + // the context later (without it a lost context stays lost); the host then builds a new scene on + // `webglcontextrestored`, and this one stays dead. listen(canvas, 'webglcontextlost', (e) => { e.preventDefault(); stop(); diff --git a/src/app/features/scene/host.test.ts b/src/app/features/scene/host.test.ts index 7d6d3cfe..272f502f 100644 --- a/src/app/features/scene/host.test.ts +++ b/src/app/features/scene/host.test.ts @@ -188,6 +188,48 @@ describe('mountBackgroundScene', () => { expect(startBackgroundScene).toHaveBeenCalledOnce(); }); + it('removes the exact restore listener it added when it is disposed', async () => { + const add = vi.spyOn(canvas, 'addEventListener'); + const remove = vi.spyOn(canvas, 'removeEventListener'); + startBackgroundScene.mockResolvedValue(fakeScene()); + const dispose = mountBackgroundScene(config, caps, canvas, { loggedIn: false }); + await flush(); + const added = add.mock.calls.find(([type]) => type === 'webglcontextrestored'); + expect(added).toBeDefined(); + expect(remove).not.toHaveBeenCalled(); + dispose(); + expect(remove).toHaveBeenCalledWith('webglcontextrestored', added?.[1]); + }); + + it('treats a second loss from the same scene as nothing', async () => { + const scene = fakeScene(); + startBackgroundScene.mockResolvedValue(scene); + const onInactive = vi.fn(); + mountBackgroundScene(config, caps, canvas, { loggedIn: false, onInactive }); + await flush(); + lose(); + lose(); + expect(onInactive).toHaveBeenCalledOnce(); + expect(scene.dispose).toHaveBeenCalledOnce(); + }); + + it('ignores the old scene loss callback once a new scene has started', async () => { + const first = fakeScene(); + const second = fakeScene(); + startBackgroundScene.mockResolvedValueOnce(first).mockResolvedValueOnce(second); + const onInactive = vi.fn(); + mountBackgroundScene(config, caps, canvas, { loggedIn: false, onInactive }); + await flush(); + const oldLost = startBackgroundScene.mock.calls[0][2].onContextLost; + lose(); + restore(); + await flush(); + onInactive.mockClear(); + oldLost(); + expect(second.dispose).not.toHaveBeenCalled(); + expect(onInactive).not.toHaveBeenCalled(); + }); + it('drops a scene that resolves after a loss that arrived while it started', async () => { const late = fakeScene(); let resolve!: (s: Scene) => void;