scene: pin the host's restore-listener removal and stale-loss guard; fix preventDefault comment
Some checks failed
Build pull request / Build pull request (pull_request) Has been cancelled
Some checks failed
Build pull request / Build pull request (pull_request) Has been cancelled
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
3b61f2babf
commit
571a2de3fa
2 changed files with 45 additions and 2 deletions
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue