fix(studio): a sealed preview no longer shows the frame before the playhead after a save (#8818)

This commit is contained in:
Miguel Ángel
2026-11-25 05:59:45 +00:00
committed by GitHub
parent 44d49a5219
commit b975bbed4b
5 changed files with 68 additions and 4 deletions
+33
View File
@@ -3747,6 +3747,39 @@ describe("initSandboxRuntimeModular", () => {
expect(seekCalls).toEqual([{ time: 2, suppressEvents: false }]);
});
// A sealed frame gets each seek as its own message, so Studio re-renders it with one seek to the time it shows.
const rootSeeking = (children?: unknown[]) => {
document.body.innerHTML = `<div data-composition-id="main" data-root="true" data-start="0" data-duration="10" data-width="1920" data-height="1080"></div>`;
const seekCalls: Array<{ time: number; suppressEvents?: boolean }> = [];
const rootTimeline = createMockTimeline(10);
const originalTotalTime = rootTimeline.totalTime;
rootTimeline.totalTime = (time: number, suppressEvents?: boolean) => {
if (time !== undefined) seekCalls.push({ time, suppressEvents });
return originalTotalTime?.(time, suppressEvents);
};
if (children) Object.assign(rootTimeline, { getChildren: () => children });
window.__timelines = { main: rootTimeline };
initSandboxRuntimeModular();
window.__player?.seek(2);
seekCalls.length = 0;
window.__player?.seek(2);
return seekCalls;
};
it("redraws the root timeline on a seek to the time it already shows", () => {
expect(rootSeeking()).toEqual([
{ time: 2, suppressEvents: false },
{ time: 1.999, suppressEvents: true },
{ time: 2, suppressEvents: true },
]);
});
it("does not redraw a root timeline holding a zero-duration callback on a seek to its own time", () => {
const callback = { vars: { onComplete: () => {} }, duration: () => 0, totalDuration: () => 0 };
// Known limit: a redraw would fire the callback again, so only the seek itself lands.
expect(rootSeeking([callback])).toEqual([{ time: 2, suppressEvents: false }]);
});
it("keeps a later root tween's value over a nested scene the root already drew", () => {
document.body.innerHTML = `<div data-composition-id="main" data-root="true" data-start="0" data-duration="2" data-width="1920" data-height="1080"><div data-composition-id="child" data-start="0" data-duration="2"><div id="box"></div></div></div>`;
const box = document.getElementById("box")!;
@@ -147,7 +147,7 @@ function patchLiveHiddenState(
export function reseekPreviewRuntime(iframe: HTMLIFrameElement | null): void {
const store = usePlayerStore.getState();
if (applySoftReloadFinalization(iframe, store.currentTime)) return;
reseekPreviewAtTime({ seek: store.requestSeek }, store.currentTime);
reseekPreviewAtTime({ seek: store.requestSeek }, store.currentTime, iframe);
}
export function groupElementsByTargetPath(
@@ -5,9 +5,16 @@ import {
collectSubCompositionDomChildren,
collectSubCompositionHostState,
hydrateTimelineFromPreview,
reseekPreviewAtTime,
} from "./timelineSyncHydration";
import type { ClipManifestClip, PlaybackAdapter } from "../lib/playbackTypes";
const { sealedFrames } = vi.hoisted(() => ({ sealedFrames: new WeakSet<HTMLIFrameElement>() }));
vi.mock("../lib/previewChannel", async (importOriginal) => ({
...(await importOriginal<typeof import("../lib/previewChannel")>()),
previewFrameOpaque: (iframe: HTMLIFrameElement) => sealedFrames.has(iframe),
}));
const clip = (over: Partial<ClipManifestClip>): ClipManifestClip => ({
id: "x",
label: "x",
@@ -165,3 +172,20 @@ describe("hydrateTimelineFromPreview", () => {
expect(attachIframeShortcutListeners).toHaveBeenCalledOnce();
});
});
describe("reseekPreviewAtTime", () => {
// Each seek message is its own task in a sealed frame, so a guard seek paints: at 1 s it showed frame 29.
it.each([0, 0.5, 1, 3.5])("seeks a sealed frame once, to %s", (t) => {
const iframe = document.createElement("iframe");
sealedFrames.add(iframe);
const seek = vi.fn();
reseekPreviewAtTime({ seek }, t, iframe);
expect(seek.mock.calls).toEqual([[t]]);
});
it("seeks a frame Studio can script to a guard time first, so an unchanged time still renders", () => {
const seek = vi.fn();
reseekPreviewAtTime({ seek }, 1, document.createElement("iframe"));
expect(seek.mock.calls).toEqual([[0.999], [1]]);
});
});
@@ -302,8 +302,14 @@ export function resolveReloadSeekTime(input: {
type SeekablePreview = Pick<PlaybackAdapter, "seek">;
/** Re-run the current frame even when the runtime already reports that time. */
export function reseekPreviewAtTime(preview: SeekablePreview, time: number): void {
export function reseekPreviewAtTime(
preview: SeekablePreview,
time: number,
iframe: HTMLIFrameElement | null,
): void {
const target = Number.isFinite(time) && time > 0 ? time : 0;
// A sealed frame's runtime redraws on a seek to its own time, and paints each seek message: a guard would show.
if (iframe && previewFrameOpaque(iframe)) return void preview.seek(target);
preview.seek(target > 0.001 ? Math.max(0, target - 0.001) : 0.001);
preview.seek(target);
}
@@ -311,6 +317,7 @@ export function reseekPreviewAtTime(preview: SeekablePreview, time: number): voi
export function seekAdapterToRestorePoint(
adapter: PlaybackAdapter,
pendingSeekRef: { current: number | null },
iframe: HTMLIFrameElement | null,
): number {
const storeSeek = usePlayerStore.getState().requestedSeekTime;
const startTime = resolveReloadSeekTime({
@@ -321,7 +328,7 @@ export function seekAdapterToRestorePoint(
});
pendingSeekRef.current = null;
if (storeSeek != null) usePlayerStore.getState().clearSeekRequest();
reseekPreviewAtTime(adapter, startTime);
reseekPreviewAtTime(adapter, startTime, iframe);
return startTime;
}
@@ -317,7 +317,7 @@ export function useTimelineSyncCallbacks({
if (!adapter || adapter.getDuration() <= 0) return false;
adapter.pause();
const startTime = seekAdapterToRestorePoint(adapter, pendingSeekRef);
const startTime = seekAdapterToRestorePoint(adapter, pendingSeekRef, iframeRef.current);
const commit = () => {
// Keep non-React listeners such as the time display in sync
// with the initial adapter seek on iframe load.