Files
hyperframes/packages/core/src/runtime/webglContextRecovery.ts
T
Miguel Ángel e8ffaa410f fix(core): preview recovers its WebGL scenes and effects after a lost context (#7028)
* fix(core): preview repaints and rebuilds effects after the GPU restores a lost WebGL context

* fix(core): a paused scene redraws and a playing one repaints effects after a context restore

* test(core): a paused scene is redrawn after a WebGL context restore

* test(core): give the restore redraw test a clip

* fix(core): shorten the context recovery header comment

* fix(core): rebuild effects only after every lost context has restored

* test(core): the restore rebuild waits while another context is still lost

* test(core): count context requests to show the rebuild waits

* test(core): the mock reports the lost flag the test sets
2026-11-02 17:47:44 +00:00

22 lines
845 B
TypeScript

export function watchSceneWebglContexts(onRestored: () => void): () => void {
let frame: number | null = null;
// Without preventDefault the browser never restores the context.
const onLost = (event: Event) => event.preventDefault();
const onRestore = () => {
if (frame !== null) return;
frame = requestAnimationFrame(() => {
frame = null;
onRestored();
});
};
// These events do not bubble, so only a capture listener sees every canvas.
document.addEventListener("webglcontextlost", onLost, true);
document.addEventListener("webglcontextrestored", onRestore, true);
return () => {
document.removeEventListener("webglcontextlost", onLost, true);
document.removeEventListener("webglcontextrestored", onRestore, true);
if (frame !== null) cancelAnimationFrame(frame);
frame = null;
};
}