mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 00:32:21 +00:00
Report thrown render loop errors and keep one loop after an inline Retry
An error thrown outside Scene's own error handling (a postRender listener, the clock's onTick, a resize) stopped Cesium's loop without a renderError, and with Cesium's panel turned off the globe froze silently. The notice now raises such an error as a render error. In an inline panel whose host stopped animation frames, Retry left the panel's timer latched off, and once frames resumed a second render loop drew every frame twice. The timer now follows Cesium's loop, and keeps the loop marked running when its next frame is still queued.
This commit is contained in:
1 parent
abfc85e03d
commit
628de8386d
7 files changed
+339
-11
No files matched your search
@@ -105,10 +105,45 @@ export function installRenderFailureNotice(
|
||||
};
|
||||
|
||||
const removeRenderError = scene.renderError.addEventListener(onRenderError);
|
||||
// An error thrown outside Scene's own error handling (a preRender or
|
||||
// postRender listener, the clock's onTick, a resize) also stops Cesium's
|
||||
// render loop, but raises no renderError, and Cesium reports it only in the
|
||||
// panel the app turns off. Raise it as a render error, so Cesium's handler,
|
||||
// an inline panel's timer and this notice all see it.
|
||||
const unguard = ['resize', 'render'].map((name) =>
|
||||
reportThrown(viewer.cesiumWidget, name, (error) =>
|
||||
scene.renderError.raiseEvent(scene, error),
|
||||
),
|
||||
);
|
||||
canvas?.addEventListener?.('webglcontextlost', onContextLost);
|
||||
return () => {
|
||||
removeRenderError();
|
||||
unguard.forEach((restore) => restore());
|
||||
canvas?.removeEventListener?.('webglcontextlost', onContextLost);
|
||||
clear();
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap `target[name]` so an error it throws is reported and thrown on, for
|
||||
* the caller to handle as before. Returns a function that unwraps it.
|
||||
*/
|
||||
function reportThrown(target, name, report) {
|
||||
const original = target?.[name];
|
||||
if (typeof original !== 'function') return () => {};
|
||||
const own = Object.hasOwn(target, name);
|
||||
const guarded = function (...args) {
|
||||
try {
|
||||
return original.apply(this, args);
|
||||
} catch (error) {
|
||||
report(error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
target[name] = guarded;
|
||||
return () => {
|
||||
if (target[name] !== guarded) return;
|
||||
if (own) target[name] = original;
|
||||
else delete target[name];
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
// An error thrown in the render loop outside Scene's tryAndCatchError
|
||||
// (a preRender/postRender listener, clock.onTick, resize) must still reach
|
||||
// the render failure notice. Drives Cesium's real startRenderLoop through the
|
||||
// CesiumWidget.prototype.useDefaultRenderLoop setter.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import * as Cesium from 'cesium';
|
||||
import { installRenderFailureNotice } from './renderFailure.js';
|
||||
|
||||
// What src/app/scene.js passes to the viewer (showRenderLoopErrors).
|
||||
const SHOW_RENDER_LOOP_ERRORS = false;
|
||||
|
||||
test('an error thrown outside tryAndCatchError still shows the notice', () => {
|
||||
const frames = [];
|
||||
const previousRaf = globalThis.requestAnimationFrame;
|
||||
globalThis.requestAnimationFrame = (fn) => frames.push(fn);
|
||||
try {
|
||||
const scene = {
|
||||
renderError: new Cesium.Event(),
|
||||
preUpdate: new Cesium.Event(),
|
||||
postRender: new Cesium.Event(),
|
||||
requestRender() {},
|
||||
initializeFrame() {},
|
||||
// Scene.render raises preRender outside tryAndCatchError
|
||||
// (Scene.js:4439), so a listener's exception propagates.
|
||||
render() {
|
||||
this.preUpdate.raiseEvent(this);
|
||||
throw new TypeError(
|
||||
"Cannot read properties of undefined (reading 'x')",
|
||||
);
|
||||
},
|
||||
};
|
||||
const panels = [];
|
||||
const widget = Object.create(Cesium.CesiumWidget.prototype);
|
||||
Object.assign(widget, {
|
||||
_scene: scene,
|
||||
_clock: { tick() {} },
|
||||
_canRender: true,
|
||||
_useDefaultRenderLoop: false,
|
||||
_renderLoopRunning: false,
|
||||
_showRenderLoopErrors: SHOW_RENDER_LOOP_ERRORS,
|
||||
_targetFrameRate: undefined,
|
||||
isDestroyed: () => false,
|
||||
resize() {},
|
||||
showErrorPanel: (...args) => panels.push(args),
|
||||
});
|
||||
const viewer = {
|
||||
scene,
|
||||
cesiumWidget: widget,
|
||||
canvas: { addEventListener() {}, removeEventListener() {} },
|
||||
get useDefaultRenderLoop() {
|
||||
return widget.useDefaultRenderLoop;
|
||||
},
|
||||
set useDefaultRenderLoop(value) {
|
||||
widget.useDefaultRenderLoop = value;
|
||||
},
|
||||
render: () => widget.render(),
|
||||
resize: () => widget.resize(),
|
||||
};
|
||||
const store = {
|
||||
renderFailure: null,
|
||||
publishRenderFailure(next) {
|
||||
this.renderFailure = next;
|
||||
},
|
||||
};
|
||||
const logged = [];
|
||||
const uninstall = installRenderFailureNotice(viewer, {
|
||||
store,
|
||||
locationRef: { href: 'https://gev.example/', reload() {}, assign() {} },
|
||||
logError: (error) => logged.push(error),
|
||||
});
|
||||
viewer.useDefaultRenderLoop = true; // starts Cesium's loop
|
||||
while (frames.length) frames.shift()(16);
|
||||
// Cesium's catch stopped the loop.
|
||||
assert.equal(widget.useDefaultRenderLoop, false);
|
||||
// The user is told, and the error is logged.
|
||||
assert.equal(store.renderFailure?.reason, 'render-error');
|
||||
assert.equal(logged.length, 1);
|
||||
uninstall();
|
||||
} finally {
|
||||
globalThis.requestAnimationFrame = previousRaf;
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user