fix(core): stop script loading time from shifting WAAPI frames (#5017)

This commit is contained in:
cooleryu
2026-11-05 03:13:48 +00:00
committed by GitHub
parent 6e8454d6d5
commit 982070388e
3 changed files with 253 additions and 4 deletions
@@ -0,0 +1,146 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { createWaapiAdapter } from "./waapi";
class TestAnimation {
constructor(public currentTime = 0) {}
playState = "running";
playbackRate = 1;
timeline: object | null = timeline;
addEventListener = vi.fn();
pause = vi.fn(() => {
this.playState = "paused";
});
}
class TestCssAnimation extends TestAnimation {}
class TestCssTransition extends TestAnimation {}
const timeline = {};
describe("initial WAAPI baseline", () => {
let items: TestAnimation[];
const create = (time = 300) => {
const animation = new TestAnimation(time);
items.push(animation);
return animation;
};
beforeEach(() => {
items = [];
vi.stubGlobal("document", { timeline, getAnimations: () => items });
vi.stubGlobal("CSSAnimation", TestCssAnimation);
vi.stubGlobal("CSSTransition", TestCssTransition);
vi.stubGlobal("Element", undefined);
});
afterEach(() => vi.unstubAllGlobals());
it.each([0, 16, 90, 300, 1500])(
"does not retain %d ms of initial wall-clock progress",
(time) => {
const animation = create(time);
const adapter = createWaapiAdapter();
adapter.discover();
for (const t of [0, 2, 3.5, 2]) {
adapter.seek({ time: t });
expect(animation.currentTime).toBe(t * 1000);
expect(animation.playState).toBe("paused");
}
},
);
it.each(["seek", "pause"])("also anchors when %s is called before discover", (method) => {
const animation = create();
const adapter = createWaapiAdapter();
if (method === "pause") adapter.pause();
adapter.seek({ time: 2 });
expect(animation.currentTime).toBe(2000);
});
it("rewinds an animation that finished before initial discovery", () => {
const animation = create(80);
animation.playState = "finished";
const adapter = createWaapiAdapter();
adapter.discover();
adapter.seek({ time: 0.04 });
expect(animation.currentTime).toBe(40);
});
it("does not reinterpret an idle animation", () => {
const animation = create(500);
animation.playState = "idle";
const adapter = createWaapiAdapter();
adapter.discover();
adapter.seek({ time: 2 });
expect(animation.currentTime).toBe(2500);
});
it("keeps the captured baseline across repeated discovery at composition zero", () => {
const animation = create(300);
const adapter = createWaapiAdapter();
adapter.discover();
animation.currentTime = 900;
adapter.discover();
adapter.seek({ time: 2 });
expect(animation.currentTime).toBe(2000);
});
it.each([0, 500])("preserves a paused authored offset of %d ms", (time) => {
const animation = create(time);
animation.playState = "paused";
const adapter = createWaapiAdapter();
adapter.discover();
adapter.seek({ time: 2 });
expect(animation.currentTime).toBe(2000 + time);
});
it.each([TestCssAnimation, TestCssTransition])("leaves $name baselines unchanged", (Type) => {
const animation = new Type(500);
items.push(animation);
const adapter = createWaapiAdapter();
adapter.discover();
adapter.seek({ time: 2 });
expect(animation.currentTime).toBe(2500);
});
it.each([null, {}])("leaves an alternate or absent timeline unchanged", (otherTimeline) => {
const animation = create(500);
animation.timeline = otherTimeline;
const adapter = createWaapiAdapter();
adapter.discover();
adapter.seek({ time: 2 });
expect(animation.currentTime).toBe(2500);
});
it.each([-1, 0, 2])("does not reinterpret a playback rate of %d", (rate) => {
const animation = create(500);
animation.playbackRate = rate;
const adapter = createWaapiAdapter();
adapter.discover();
adapter.seek({ time: 2 });
expect(animation.currentTime).toBe(2500);
});
it("preserves late discovery and rediscovery relative to composition time", () => {
const adapter = createWaapiAdapter();
create(100);
adapter.discover();
adapter.seek({ time: 1 });
const late = create(0);
adapter.seek({ time: 1.2 });
expect(late.currentTime).toBe(0);
adapter.discover();
adapter.seek({ time: 2 });
expect(late.currentTime).toBe(800);
});
it("can capture initial animations again after revert", () => {
const adapter = createWaapiAdapter();
const animation = create();
adapter.discover();
adapter.seek({ time: 2 });
adapter.revert();
animation.currentTime = 300;
animation.playState = "running";
adapter.discover();
adapter.seek({ time: 2 });
expect(animation.currentTime).toBe(2000);
});
});
+15 -4
View File
@@ -53,6 +53,15 @@ export function createWaapiAdapter(): RuntimeDeterministicAdapter {
return animationTimeMs;
};
const shouldAnchorAtCompositionOrigin = (animation: Animation, compositionTimeMs: number) =>
!didDiscover &&
compositionTimeMs === 0 &&
(animation.playState === "running" || animation.playState === "finished") &&
animation.playbackRate === 1 &&
!!document.timeline &&
animation.timeline === document.timeline &&
!isBrowserCreatedCss(animation);
const ensureBaseline = (animation: Animation, compositionTimeMs: number) => {
const existing = baselines.get(animation);
if (existing) {
@@ -61,9 +70,11 @@ export function createWaapiAdapter(): RuntimeDeterministicAdapter {
const baseline = {
compositionTimeMs,
animationTimeMs: didDiscover
? normalizeInitialAnimationTimeMs(readAnimationTimeMs(animation), compositionTimeMs)
: readAnimationTimeMs(animation),
animationTimeMs: shouldAnchorAtCompositionOrigin(animation, compositionTimeMs)
? 0
: didDiscover
? normalizeInitialAnimationTimeMs(readAnimationTimeMs(animation), compositionTimeMs)
: readAnimationTimeMs(animation),
};
baselines.set(animation, baseline);
return baseline;
@@ -147,9 +158,9 @@ export function createWaapiAdapter(): RuntimeDeterministicAdapter {
return {
name: "waapi",
discover: () => {
didDiscover = true;
installAnimateHook();
trackAnimations(snapshotAnimations(), lastSeekTimeMs);
didDiscover = true;
},
seek: (ctx) => {
const timeMs = Math.max(0, (Number(ctx.time) || 0) * 1000);
@@ -199,6 +199,98 @@ describe("core runtime browser contract", () => {
});
});
it.each([
{ name: "running after 80 ms", elapsed: 80 },
{ name: "running after 250 ms", elapsed: 250 },
{ name: "positive delay", elapsed: 80, delay: 500 },
{ name: "negative delay", elapsed: 80, delay: -500 },
{ name: "reverse keyframes", elapsed: 80, reverse: true },
{ name: "finished before discovery", elapsed: 200, duration: 200 },
{ name: "paused authored offset", elapsed: 80, paused: true, offset: 500 },
{ name: "paused at zero", elapsed: 80, paused: true, offset: 0 },
{ name: "running currentTime is not a baseline offset", elapsed: 80, offset: 500 },
])(
"seeks initial WAAPI in composition time: $name",
async ({
elapsed,
delay = 0,
duration = 4000,
reverse = false,
paused = false,
offset = 0,
}) => {
const animationPage = await browser.newPage();
try {
await animationPage.setContent(`<!doctype html>
<div data-composition-id="root" data-root="true" data-no-timeline
data-start="0" data-duration="4" data-width="320" data-height="180">
<div id="waapi-box" class="clip" data-start="0" data-duration="4"></div>
</div>`);
await animationPage.evaluate(
({ delay, duration, reverse, paused, offset }) => {
const box = document.getElementById("waapi-box");
if (!box) throw new Error("waapi-box missing");
const animation = box.animate(
[{ transform: "translateX(0px)" }, { transform: "translateX(100px)" }],
{ duration, delay, direction: reverse ? "reverse" : "normal", fill: "both" },
);
if (paused) animation.pause();
if (offset || paused) animation.currentTime = offset;
},
{ delay, duration, reverse, paused, offset },
);
if (!paused) {
// Wait for actual browser progress, not a guessed sleep duration.
await animationPage.waitForFunction(
(minimumTime) =>
Number(document.getElementById("waapi-box")?.getAnimations()[0]?.currentTime) >=
minimumTime,
{},
elapsed + offset,
);
}
await animationPage.addScriptTag({ content: readFileSync(RUNTIME_PATH, "utf8") });
await animationPage.waitForFunction(
() =>
(window as unknown as { __playerReady?: boolean }).__playerReady === true &&
(window as unknown as { __renderReady?: boolean }).__renderReady === true,
);
const times = duration === 200 ? [0, 0.1, 1 / 6, 0.1] : [0, 2, 3, 2];
const samples = await animationPage.evaluate(async (times) => {
// The runtime is injected as a built script, outside this test's module graph.
const player = (
window as unknown as {
__player: { seek: (time: number) => void; renderSeek: (time: number) => void };
}
).__player;
const box = document.getElementById("waapi-box");
if (!box || !player) throw new Error("WAAPI fixture did not initialize");
const samples = [];
for (const method of ["seek", "renderSeek"] as const) {
for (const time of times) {
await player[method](time);
samples.push({
time,
x: new DOMMatrixReadOnly(getComputedStyle(box).transform).m41,
playState: box.getAnimations()[0]?.playState,
});
}
}
return samples;
}, times);
for (const sample of samples) {
const localTime = sample.time * 1000 + (paused ? offset : 0) - delay;
const progress = Math.max(0, Math.min(1, localTime / duration));
expect(sample.x).toBeCloseTo((reverse ? 1 - progress : progress) * 100, 3);
expect(sample.playState).toBe("paused");
}
} finally {
await animationPage.close();
}
},
30_000,
);
it.each([24, 30, 60, 30_000 / 1_001])(
"keeps the real public player running across a seek at %s fps",
async (fps) => {