From 4a37b0737760ea2d8914c4eacc8c502b376a9a04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Wed, 23 Sep 2026 21:31:10 -0400 Subject: [PATCH] fix(studio): hold play on a composition switch until the new preview loads Readiness was reset only when the project changed. Drilling into another composition kept the old ready flag while the new preview loaded, so Play or Space could start a play that its load step then undid. --- .../player/hooks/useShadowPreviewReload.ts | 3 +++ .../hooks/useTimelinePlayer.play.test.ts | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/packages/studio/src/player/hooks/useShadowPreviewReload.ts b/packages/studio/src/player/hooks/useShadowPreviewReload.ts index 500c339eef..d893afc474 100644 --- a/packages/studio/src/player/hooks/useShadowPreviewReload.ts +++ b/packages/studio/src/player/hooks/useShadowPreviewReload.ts @@ -14,6 +14,7 @@ import { } from "./useTimelineSyncCallbacks"; import type { PlaybackAdapter } from "../lib/playbackTypes"; import { thumbnailScheduler } from "../lib/thumbnailScheduler"; +import { usePlayerStore } from "../store/playerStore"; // The single wait budget for a shadow: the player's 8s asset cap plus its 0.42s loader fade // leaves about 6.5s for the document load and runtime boot. Nothing shorter may fail the swap. @@ -191,6 +192,8 @@ export function useShadowPreviewReload({ shadowIframeRef.current = null; isRefreshingRef.current = false; pendingSeekRef.current = null; + // The new composition's preview isn't playable yet; its load step marks it ready again. + usePlayerStore.getState().setTimelineReady(false); setPreviewSlots(planShadowDiscard); thumbnailScheduler.setPreviewReloading(false); }, [stopPendingShadow, isRefreshingRef, pendingSeekRef]); diff --git a/packages/studio/src/player/hooks/useTimelinePlayer.play.test.ts b/packages/studio/src/player/hooks/useTimelinePlayer.play.test.ts index c11f4abe5c..830086a611 100644 --- a/packages/studio/src/player/hooks/useTimelinePlayer.play.test.ts +++ b/packages/studio/src/player/hooks/useTimelinePlayer.play.test.ts @@ -38,3 +38,23 @@ it("does not start playback before the timeline is ready, so iframe load cannot expect(usePlayerStore.getState().isPlaying).toBe(true); act(() => root.unmount()); }); + +it("does not start playback while a switched-to composition's preview is still loading", () => { + const { api, root } = renderTimelinePlayerHarness(); + act(() => { + api.iframeRef.current = makeFakeIframe(makeAdapterWindow().win); + api.onIframeLoad(); + usePlayerStore.setState({ timelineReady: true }); + }); + + // Drill into another composition: a fresh preview whose runtime is up but whose load step hasn't run. + const next = makeAdapterWindow(); + act(() => { + api.resetPreviewSlots(); + api.iframeRef.current = makeFakeIframe(next.win); + }); + act(() => api.play()); + expect(next.adapter.play).not.toHaveBeenCalled(); + expect(usePlayerStore.getState().isPlaying).toBe(false); + act(() => root.unmount()); +});