feat(time): the app clock on the engine

src/time/clock.js is the app's own time in ms since the epoch: by default
Date.now exactly (read at each call), with freeze/offset/rate/reset for QA
and tests, and one frame time latched per engine frame (beginFrame). The
contract gains engine.clock { now, frameTime, ticked }, in the fake engine
(a controllable wall clock, fake.advanceTime, fake.appClock), the contract
suite (one frame time for every listener of a frame, latched before
preUpdate, never decreasing), Cesium and Navara.

Cesium latches on the viewer clock's onTick, which CesiumWidget runs right
before scene.render, so scene listeners the app added before the engine
existed read the same frame time; the subscription appears on first use
and destroy removes it. The viewer clock itself is untouched: in the app
it never animates (shouldAnimate false, multiplier 1, its time frozen at
boot), and its time still drives Cesium's lighting as before. Navara
latches in the engine's handler of the view's preUpdate, and the look
reads the engine clock's frame time for the sun, so the app clock is the
one time source on Navara too.

src/time is a portable root in check-import-directions: no engine
packages, no browser globals.
This commit is contained in:
German Khokhlov committed 2026-10-16 15:01:36 +04:00
1 parent 27e28373d0
commit f1a2df5cfe
20 files changed
+771 -13

No files matched your search

+71
View File
@@ -359,6 +359,77 @@ export function createContractSuite(
);
},
],
[
'clock: one frame time per frame, latched before preUpdate, never decreasing',
async () => {
const records = [];
const read = (name, value) =>
records.push({
name,
value,
frame: engine.clock.frameTime(),
now: engine.clock.now(),
});
const removers = [
engine.clock.ticked.addEventListener((time) => read('ticked', time)),
...['preUpdate', 'preRender', 'postRender'].map((name) =>
engine.frames[name].addEventListener(() => read(name)),
),
];
try {
await until(
() =>
records.filter(({ name }) => name === 'postRender').length >= 3,
'three drawn frames',
);
} finally {
for (const remove of removers) remove();
}
ensure(
Number.isFinite(engine.clock.now()) &&
Number.isFinite(engine.clock.frameTime()),
'clock.now or clock.frameTime is not a number',
);
// From the first tick on: every frame event belongs to the frame the
// last tick opened, and reads that tick's frame time.
const start = records.findIndex(({ name }) => name === 'ticked');
ensure(start >= 0, 'clock.ticked never fired');
let tick = null;
let ticksSincePreUpdate = 0;
let previousFrame = -Infinity;
for (const record of records.slice(start)) {
ensure(
record.frame <= record.now,
`${record.name}: frameTime ${record.frame} is after now ${record.now}`,
);
if (record.name === 'ticked') {
ensure(
record.value === record.frame,
`ticked passed ${record.value}, frameTime read ${record.frame}`,
);
ensure(
record.frame >= previousFrame,
`frame time went back from ${previousFrame} to ${record.frame}`,
);
previousFrame = record.frame;
tick = record;
ticksSincePreUpdate++;
continue;
}
ensure(
record.frame === tick.frame,
`${record.name} read frame time ${record.frame}, its frame's is ${tick.frame}`,
);
if (record.name === 'preUpdate') {
ensure(
ticksSincePreUpdate >= 1,
'preUpdate ran without a tick before it',
);
ticksSincePreUpdate = 0;
}
}
},
],
[
'render: continuous mode toggles',
async () => {