mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(producer): keep page timers on the film's schedule at every worker count (#7390)
This commit is contained in:
@@ -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);");
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -434,6 +434,20 @@ ${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,
|
||||
@@ -441,14 +455,7 @@ ${seededRandomBlock}
|
||||
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;
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user