mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
* 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
22 lines
845 B
TypeScript
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;
|
|
};
|
|
}
|