Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/producer/src/services/fileServer-virtualTimers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ describe("virtual-time shim timers", () => {
expect(run("fired")).toBe(11);
});

it("keeps the film's timer schedule on a worker whose first frame is past the start", () => {
const { run } = page();
run(`window.at = [];
requestAnimationFrame(() => (function step() { setTimeout(() => { at.push(Date.now()); step(); }, 100); })());`);
run("window.__HF_VIRTUAL_TIME__.seekToTime(4000 / 30);");
expect(run("at")).toEqual([100]);
run("for (let f = 5; f <= 30; f++) window.__HF_VIRTUAL_TIME__.seekToTime(f * 1000 / 30);");
expect(run("at")).toEqual([100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]);
});

it("runs due timers before the frame's animation callbacks", () => {
const { run } = page();
run("window.__HF_VIRTUAL_TIME__.seekToTime(0);");
Expand Down
25 changes: 16 additions & 9 deletions packages/producer/src/services/fileServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export function buildVirtualTimeShim(options: VirtualTimeShimOptions): string {
// The seekToTime hook reseeds when seeding is on; under seedRandomFromFrame=false
// we emit no extra call so the function body is byte-identical to the
// unseeded shim.
const seekToTimeReseedCall = seedRandomFromFrame ? "reseedRngFromTime(safeTimeMs);\n " : "";
const seekToTimeReseedCall = seedRandomFromFrame ? "reseedRngFromTime(safeTimeMs);\n " : "";
return String.raw`(function() {
if (window.__HF_VIRTUAL_TIME__) return;

Expand Down Expand Up @@ -434,21 +434,28 @@ ${seededRandomBlock}
if (!cancelTimer(id)) originalClearInterval(id);
};

function seekToTime(nextTimeMs) {
var safeTimeMs = Math.max(0, Number(nextTimeMs) || 0);
if (!rendering && window.__renderReady === true) {
rendering = true;
// A worker whose first frame is past the start plays the film's first frame first, so timers the
// page starts there keep the schedule a one-worker render gives them.
if (safeTimeMs > 0) seekToTime(0);
}
${seekToTimeReseedCall}fireDueTimers(safeTimeMs);
virtualNowMs = safeTimeMs;
flushAnimationFrame();
return virtualNowMs;
}

window.__HF_VIRTUAL_TIME__ = {
originalSetTimeout: originalSetTimeout,
originalClearTimeout: originalClearTimeout,
originalSetInterval: originalSetInterval,
originalClearInterval: originalClearInterval,
originalRequestAnimationFrame: originalRequestAnimationFrame,
originalCancelAnimationFrame: originalCancelAnimationFrame,
seekToTime: function(nextTimeMs) {
var safeTimeMs = Math.max(0, Number(nextTimeMs) || 0);
if (window.__renderReady === true) rendering = true;
${seekToTimeReseedCall}fireDueTimers(safeTimeMs);
virtualNowMs = safeTimeMs;
flushAnimationFrame();
return virtualNowMs;
},
seekToTime: seekToTime,
getTime: function() {
return virtualNowMs;
},
Expand Down
Loading