Merge main (663b56f62, #1755) into the renderer branch

This commit is contained in:
Sameh Khamis committed 2026-11-15 16:34:05 -08:00
commit a0d299523d
91 files changed
+1793 -396

No files matched your search

+37
View File
@@ -0,0 +1,37 @@
/**
* A wait measured in time spent drawing frames, the same at any display
* refresh rate.
*
* Each call to `tick()` adds the time since the previous call, but at most
* MAX_STEP_MS, so a background tab, a parked scene or a hidden owner (which
* draw no frames for this wait) does not use the wait up while nothing is on
* screen.
*/
/** The most one frame may add to a wait, in milliseconds. */
export const MAX_STEP_MS = 100;
/**
* Start a wait of `limitMs` drawn time.
* @param {number} limitMs How long to wait, in milliseconds.
* @param {() => number} [now] Clock in milliseconds.
* @returns {{tick: () => boolean, pause: () => void}} `tick()` counts one
* drawn frame and answers whether the wait is over; `pause()` makes the
* next tick count nothing for the gap since the last one.
*/
export function createFrameWait(limitMs, now = () => performance.now()) {
let waitedMs = 0;
let lastMs = NaN;
return {
tick() {
const at = now();
if (Number.isFinite(lastMs))
waitedMs += Math.min(Math.max(at - lastMs, 0), MAX_STEP_MS);
lastMs = at;
return waitedMs >= limitMs;
},
pause() {
lastMs = NaN;
},
};
}
+49
View File
@@ -0,0 +1,49 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { createFrameWait, MAX_STEP_MS } from './frameWait.js';
function clock() {
let at = 0;
return { now: () => at, advance: (ms) => (at += ms) };
}
test('the wait lasts the same time at 60 and 240 frames a second', () => {
for (const hz of [60, 240]) {
const c = clock();
const wait = createFrameWait(2_000, c.now);
let frames = 0;
while (!wait.tick()) {
c.advance(1000 / hz);
frames += 1;
}
assert.ok(Math.abs(c.now() - 2_000) <= 1000 / hz + 1e-6, `${hz} Hz ended at ${c.now()}`);
assert.ok(frames > 0);
}
});
test('a long gap between frames adds at most one step', () => {
const c = clock();
const wait = createFrameWait(2_000, c.now);
wait.tick();
c.advance(60_000);
assert.equal(wait.tick(), false, 'a background tab does not use the wait up');
let drawnMs = MAX_STEP_MS;
while (!wait.tick()) {
c.advance(16);
drawnMs += 16;
}
assert.ok(drawnMs >= 2_000 && drawnMs < 2_000 + 16 + MAX_STEP_MS);
});
test('after pause the next tick adds nothing', () => {
const c = clock();
const wait = createFrameWait(150, c.now);
wait.tick();
c.advance(90);
wait.tick();
wait.pause();
c.advance(90);
assert.equal(wait.tick(), false);
c.advance(60);
assert.equal(wait.tick(), true);
});
+19 -3
View File
@@ -10,9 +10,12 @@ import { findFrame } from '../render/rendererOf.js';
* moment the layers read that frame.
* @param {object} viewer
* @param {() => number} readTime Epoch ms the sky is drawn at.
* @param {{subscribe?: Function, requestRender?: () => void}} [options]
* @param {{subscribe?: Function, requestRender?: () => void,
* onPlaying?: (playing: boolean) => void}} [options]
* A world clock to hear moves from, so a resting scene repaints when the
* world time jumps.
* world time jumps. `onPlaying` hears when the world clock starts and stops
* playing away from live (not stepped by a driver and not waiting for a
* layer's data), so the scene can draw every frame meanwhile.
* @returns {() => void} Stop following.
*/
export function followWorldTime(
@@ -21,13 +24,25 @@ export function followWorldTime(
{
subscribe,
requestRender = () => findFrame(viewer)?.requestRender('world-time'),
onPlaying = () => {},
} = {},
) {
let playing = false;
const setPlaying = (next) => {
if (next === playing) return;
playing = next;
onPlaying(next);
};
const frame = findFrame(viewer);
const off = frame?.on('preUpdate', () => frame.setTime(readTime())) ?? null;
let last = readTime();
const unsubscribe =
subscribe?.(() => {
subscribe?.((state) => {
setPlaying(
Boolean(
state?.playing && !state.live && !state.stepping && !state.buffering,
),
);
const next = readTime();
if (next === last) return;
last = next;
@@ -36,5 +51,6 @@ export function followWorldTime(
return () => {
off?.();
unsubscribe();
setPlaying(false);
};
}
+59
View File
@@ -0,0 +1,59 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { followWorldTime } from './viewerClock.js';
function fakeViewer() {
const listeners = new Set();
return {
clock: { currentTime: {} },
scene: {
requestRender() {},
preUpdate: {
addEventListener(fn) {
listeners.add(fn);
return () => listeners.delete(fn);
},
},
},
};
}
test('the scene hears when the world time plays on its own, and when it stops', () => {
let listener = null;
const subscribe = (fn) => {
listener = fn;
return () => (listener = null);
};
const heard = [];
let at = 0;
const stop = followWorldTime(fakeViewer(), () => at, {
subscribe,
requestRender: () => {},
onPlaying: (playing) => heard.push(playing),
});
const state = (fields) => ({
live: false,
playing: false,
stepping: false,
buffering: false,
...fields,
});
listener(state({ live: true, playing: true }));
assert.deepEqual(heard, [], 'live time is not playback');
listener(state({ playing: true }));
listener(state({ playing: true }));
assert.deepEqual(heard, [true], 'heard once');
listener(state({ playing: true, buffering: true }));
assert.deepEqual(heard, [true, false], 'waiting for a layer: still');
listener(state({ playing: true }));
listener(state({ playing: true, stepping: true }));
assert.deepEqual(heard, [true, false, true, false], 'a driver steps it');
listener(state({ playing: true }));
stop();
assert.deepEqual(
heard,
[true, false, true, false, true, false],
'stopping releases',
);
assert.equal(listener, null);
});