Skip to content

Commit 0cf805d

Browse files
committed
fix(core): any positive declared length stops the film, even a single frame
1 parent 1cf60af commit 0cf805d

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎packages/core/src/runtime/init.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,8 @@ export function initSandboxRuntimeModular(): void {
12261226
const declaredDuration = parseStrictFiniteTimingNumber(
12271227
resolveRootCompositionElement()?.getAttribute("data-duration"),
12281228
);
1229-
if (isUsableTimelineDuration(declaredDuration)) {
1229+
// Any positive length counts, even one frame: the render accepts it too.
1230+
if (declaredDuration !== null && declaredDuration > 0) {
12301231
if (window.__hf?.durationSource) delete window.__hf.durationSource;
12311232
return declaredDuration;
12321233
}

‎packages/core/src/runtime/transportPark.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,33 @@ describe("parked transport loop", () => {
222222
});
223223
});
224224

225+
it.each([
226+
["under a second", "0.2", 0.2, 6],
227+
["one 60 fps frame", "0.016666666666666666", 1 / 60, Math.round(30 / 60)],
228+
])(
229+
"stops a film declared %s long at that length, and says so",
230+
(_label, declared, seconds, frame) => {
231+
let nowMs = 1000;
232+
vi.spyOn(performance, "now").mockImplementation(() => nowMs);
233+
document.body.innerHTML = `<div id="root" data-composition-id="main" data-root="true" data-start="0" data-duration="${declared}"></div>`;
234+
window.__timelines = { main: createMockTimeline(5) };
235+
initSandboxRuntimeModular();
236+
quiesce();
237+
238+
window.__player!.play();
239+
for (let step = 0; step < 200 && window.__player!.isPlaying(); step += 1) {
240+
nowMs += 10;
241+
raf.step(10);
242+
}
243+
244+
const states = posted.filter((m) => m["type"] === "state");
245+
expect(states.at(-1)).toMatchObject({ isPlaying: false, ended: true, frame });
246+
expect(window.__player!.getDuration()).toBeCloseTo(seconds, 9);
247+
const timeline = posted.filter((m) => m["type"] === "timeline").at(-1);
248+
expect(timeline?.["durationSeconds"]).toBeCloseTo(seconds, 9);
249+
},
250+
);
251+
225252
it("keeps asking for animation frames while playing", () => {
226253
mount();
227254
initSandboxRuntimeModular();

‎packages/player/src/hyperframes-player.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,25 @@ describe("HyperframesPlayer loop end-state handling", () => {
14641464
expect(player._currentTime).toBe(player._duration);
14651465
});
14661466

1467+
it("takes a length under a second from the runtime and ends there", () => {
1468+
const ended = vi.fn();
1469+
player.addEventListener("ended", ended);
1470+
player.loop = false;
1471+
player._onMessage(
1472+
new MessageEvent("message", {
1473+
source: frameWindow,
1474+
data: { source: "hf-preview", type: "timeline", durationSeconds: 0.2, durationInFrames: 6 },
1475+
}),
1476+
);
1477+
expect(player.duration).toBe(0.2);
1478+
player._paused = false;
1479+
1480+
postState(6, false, 0.2, true);
1481+
1482+
expect(ended).toHaveBeenCalledTimes(1);
1483+
expect(player.currentTime).toBe(0.2);
1484+
});
1485+
14671486
it("loops a film whose length falls between two frames", () => {
14681487
const seek = vi.spyOn(player, "seek");
14691488
player.loop = true;

0 commit comments

Comments
 (0)