From b975bbed4badcccd084718430dcf6bd67796f8ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Wed, 25 Nov 2026 05:59:45 +0000 Subject: [PATCH] fix(studio): a sealed preview no longer shows the frame before the playhead after a save (#8818) --- packages/core/src/runtime/init.test.ts | 33 +++++++++++++++++++ .../src/hooks/timelineTrackVisibility.ts | 2 +- .../hooks/timelineSyncHydration.test.ts | 24 ++++++++++++++ .../src/player/hooks/timelineSyncHydration.ts | 11 +++++-- .../player/hooks/useTimelineSyncCallbacks.ts | 2 +- 5 files changed, 68 insertions(+), 4 deletions(-) diff --git a/packages/core/src/runtime/init.test.ts b/packages/core/src/runtime/init.test.ts index 7369879648..c69ff871cb 100644 --- a/packages/core/src/runtime/init.test.ts +++ b/packages/core/src/runtime/init.test.ts @@ -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 = `
`; + 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 = `
`; const box = document.getElementById("box")!; diff --git a/packages/studio/src/hooks/timelineTrackVisibility.ts b/packages/studio/src/hooks/timelineTrackVisibility.ts index 2de3d04d13..ed303ba954 100644 --- a/packages/studio/src/hooks/timelineTrackVisibility.ts +++ b/packages/studio/src/hooks/timelineTrackVisibility.ts @@ -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( diff --git a/packages/studio/src/player/hooks/timelineSyncHydration.test.ts b/packages/studio/src/player/hooks/timelineSyncHydration.test.ts index cf7cd85df6..a4e904fce7 100644 --- a/packages/studio/src/player/hooks/timelineSyncHydration.test.ts +++ b/packages/studio/src/player/hooks/timelineSyncHydration.test.ts @@ -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() })); +vi.mock("../lib/previewChannel", async (importOriginal) => ({ + ...(await importOriginal()), + previewFrameOpaque: (iframe: HTMLIFrameElement) => sealedFrames.has(iframe), +})); + const clip = (over: Partial): 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]]); + }); +}); diff --git a/packages/studio/src/player/hooks/timelineSyncHydration.ts b/packages/studio/src/player/hooks/timelineSyncHydration.ts index d8666afb24..654db81453 100644 --- a/packages/studio/src/player/hooks/timelineSyncHydration.ts +++ b/packages/studio/src/player/hooks/timelineSyncHydration.ts @@ -302,8 +302,14 @@ export function resolveReloadSeekTime(input: { type SeekablePreview = Pick; /** 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; } diff --git a/packages/studio/src/player/hooks/useTimelineSyncCallbacks.ts b/packages/studio/src/player/hooks/useTimelineSyncCallbacks.ts index d624a11c72..581c299e3f 100644 --- a/packages/studio/src/player/hooks/useTimelineSyncCallbacks.ts +++ b/packages/studio/src/player/hooks/useTimelineSyncCallbacks.ts @@ -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.