fix(embed): an inline panel's timer frames move the camera, take input and run the clock again

When a host stops animation frames, the panel drew from a timer with the
viewer's render(): the camera's flight and input, the clock's tick, then a
draw if due. Through the frame port it called renderNow(), which on Cesium
only forced a draw, so flights stood still, drags and zooms did nothing
and the clock stopped. renderNow takes {ifDue: true}: one tick of the
loop as its own would run it, drawing only a frame that is due (the
widget's render() on Cesium; the stub's tick without a forced request).
This commit is contained in:
Sameh Khamis committed 2026-11-14 01:43:13 -08:00
1 parent f4beb676ae
commit 03a7d8827a
9 files changed
+86 -22

No files matched your search

+12 -8
View File
@@ -103,19 +103,23 @@ export function willDraw(scene, time) {
}
/**
* The contract's `renderNow` on `scene`: draw a frame at `time` even when
* nothing asked for one, and throw the error Cesium caught and raised on
* `renderError` when the frame failed.
* @param {object} scene
* @param {object} [time] A Cesium JulianDate.
* The contract's `renderNow` on `owner`'s scene: draw a frame at the
* clock's time even when nothing asked for one, or with `ifDue` run one
* frame of the widget's own loop (`render()`: the camera's flight and
* input, the clock's tick, then a draw if one is due); throw the error
* Cesium caught and raised on `renderError` when the frame failed.
* @param {object} owner A Cesium `Viewer` or `CesiumWidget`.
* @param {{ifDue?: boolean}} [options]
*/
export function renderNow(scene, time) {
export function renderNow(owner, { ifDue = false } = {}) {
const scene = owner.scene;
let failure = null;
const off = scene.renderError?.addEventListener((_scene, error) => {
failure ??= { error };
});
try {
scene.forceRender(time);
if (ifDue) owner.render();
else scene.forceRender(owner.clock?.currentTime);
} finally {
off?.();
}
@@ -259,7 +263,7 @@ export function createSceneFrame(viewer) {
viewer.useDefaultRenderLoop = suspended ? false : ownLoop;
if (!suspended && !gone()) scene.requestRender();
},
renderNow: () => renderNow(scene, viewer.clock?.currentTime),
renderNow: (options) => renderNow(viewer, options),
renderOnce({ timeoutMs } = {}) {
if (gone()) return Promise.resolve();
return new Promise((resolve, reject) => {
+31 -2
View File
@@ -202,8 +202,37 @@ test('renderNow throws the error Cesium raises on renderError', () => {
if (fail) for (const cb of listeners) cb(scene, failure);
},
};
renderNow(scene);
const owner = { scene, clock: { currentTime: 'now' } };
renderNow(owner);
fail = true;
assert.throws(() => renderNow(scene), failure);
assert.throws(() => renderNow(owner), failure);
assert.equal(listeners.size, 0, 'it stops listening either way');
});
test('renderNow ifDue runs a frame of the widget’s own loop, as its render() does', () => {
const listeners = new Set();
const failure = new Error('worker failed');
const calls = [];
let fail = false;
const scene = {
renderError: {
addEventListener: (cb) => (listeners.add(cb), () => listeners.delete(cb)),
},
forceRender: () => calls.push('forceRender'),
};
// The widget's render(): the camera's flight and input, the clock's
// tick, then a draw if one is due.
const owner = {
scene,
clock: { currentTime: 'now' },
render() {
calls.push('render');
if (fail) for (const cb of listeners) cb(scene, failure);
},
};
renderNow(owner, { ifDue: true });
assert.deepEqual(calls, ['render']);
fail = true;
assert.throws(() => renderNow(owner, { ifDue: true }), failure);
assert.equal(listeners.size, 0);
});
+1 -1
View File
@@ -348,7 +348,7 @@ export function createCesiumPorts(
if (!suspended) scene.requestRender();
},
// Draws even when nothing asked for a frame; throws when it fails.
renderNow: () => renderNow(scene, widget.clock.currentTime),
renderNow: (options) => renderNow(widget, options),
renderOnce: (options) => nextPostRender(options),
setTargetFps(fps) {
targetFps = fps > 0 ? fps : 0;
+16
View File
@@ -705,6 +705,22 @@ export function portChecks() {
equal(drawnFrames, before + 1, 'renderNow drew synchronously');
},
),
check(
'frame.renderNow.ifDue',
'renderNow({ifDue}) ticks the loop now and draws a frame that is due',
async (h, r) => {
let ticks = 0;
let drawnFrames = 0;
r.frame.on('preUpdate', () => (ticks += 1));
r.frame.on('postRender', () => (drawnFrames += 1));
await h.frame(r);
const [ticked, drawn] = [ticks, drawnFrames];
r.frame.requestRender('conformance');
r.frame.renderNow({ ifDue: true });
equal(ticks, ticked + 1, 'one tick, synchronously');
equal(drawnFrames, drawn + 1, 'the asked-for frame drawn');
},
),
check('frame.suspend', 'a suspended loop does not tick', async (h, r) => {
let ticks = 0;
r.frame.on('preUpdate', () => (ticks += 1));
+7 -2
View File
@@ -72,8 +72,13 @@ export interface FramePort {
diagnostics(): FrameDiagnostics;
/** Stop ticking while the page is hidden; resume on `false`. */
suspend(hidden: boolean): void;
/** Draw a frame now, synchronously; throws when the frame fails to draw. */
renderNow(): void;
/**
* Draw a frame now, synchronously; throws when the frame fails to draw.
* `ifDue`: run one tick of the loop now as its own would (camera flight,
* input, clock), drawing only if a frame is due, for a host that stops
* the animation frames the loop runs on.
*/
renderNow(options?: { ifDue?: boolean }): void;
/** Resolves after the next frame has been drawn and presented. */
renderOnce(options?: { timeoutMs?: number }): Promise<void>;
/** Cap the loop's rate; 0 is the display's rate. */
+2 -2
View File
@@ -346,8 +346,8 @@ export function createStubRenderer(options = {}) {
suspend(hidden) {
suspended = Boolean(hidden);
},
renderNow() {
requestRender('renderNow');
renderNow({ ifDue = false } = {}) {
if (!ifDue) requestRender('renderNow');
tick();
},
renderOnce({ timeoutMs } = {}) {