diff --git a/packages/studio/src/player/hooks/useShadowPreviewReload.ts b/packages/studio/src/player/hooks/useShadowPreviewReload.ts index f1a920444e..8468999cd7 100644 --- a/packages/studio/src/player/hooks/useShadowPreviewReload.ts +++ b/packages/studio/src/player/hooks/useShadowPreviewReload.ts @@ -137,6 +137,7 @@ export function useShadowPreviewReload({ // Invalidation counter, separate from the live slot's key: bumping it never remounts the live Player. const shadowGenRef = useRef(0); const pendingCommitRef = useRef<{ gen: number; commit: () => void } | null>(null); + const acceptedTimelineGenRef = useRef(null); const visuallyReadyGenRef = useRef(null); const onPromotedRef = useRef(onPromoted); onPromotedRef.current = onPromoted; @@ -198,18 +199,26 @@ export function useShadowPreviewReload({ cancelPendingLoadRef.current(); stopHoldRef.current(); pendingCommitRef.current = null; + acceptedTimelineGenRef.current = null; visuallyReadyGenRef.current = null; }, []); const failShadow = useCallback( (gen: number, cause: string) => { if (gen !== shadowGenRef.current) return; - const waitingFor = !shadowPlayerReachable(shadowIframeRef.current) - ? "the page and its runtime to load" - : pendingCommitRef.current?.gen !== gen - ? "its timeline to be ready" - : "its loaders to clear"; - const got = pageGotTo(shadowIframeRef.current); + const shadow = shadowIframeRef.current; + const sealed = shadow !== null && previewFrameOpaque(shadow); + const waitingFor = + !sealed && !shadowPlayerReachable(shadow) + ? "the page and its runtime to load" + : pendingCommitRef.current?.gen === gen + ? "its loaders to clear" + : acceptedTimelineGenRef.current === gen + ? "its restore seek to paint" + : sealed + ? "its runtime to report a timeline" + : "its timeline to be ready"; + const got = sealed ? "" : pageGotTo(shadow); shadowGenRef.current += 1; const failedGen = shadowGenRef.current; stopPendingShadow(); @@ -328,6 +337,7 @@ export function useShadowPreviewReload({ const markAdapterReady = useCallback( (iframe: HTMLIFrameElement | null, gen: number | undefined, commit: () => void) => { if (gen == null || gen !== shadowGenRef.current) return; + acceptedTimelineGenRef.current = gen; const register = () => { if (gen !== shadowGenRef.current) return; pendingCommitRef.current = { gen, commit }; diff --git a/packages/studio/src/player/hooks/useTimelinePlayer.shadowReload.test.ts b/packages/studio/src/player/hooks/useTimelinePlayer.shadowReload.test.ts index 18a0e24ed0..0973d01d2f 100644 --- a/packages/studio/src/player/hooks/useTimelinePlayer.shadowReload.test.ts +++ b/packages/studio/src/player/hooks/useTimelinePlayer.shadowReload.test.ts @@ -16,6 +16,7 @@ import { renderTimelinePlayerHarness, resetPlayerStore, } from "./timelinePlayerTestHarness"; +import { sealedPreviewFrame } from "../lib/sealedPreviewFrame.test-helpers"; globalThis.IS_REACT_ACT_ENVIRONMENT = true; @@ -476,6 +477,49 @@ describe("shadow reload readiness and failure", () => { unmount(root); }); + // fails without the fix: a sealed shadow refuses every read, so it was always reported as a page that never loaded. + it("says only what a sealed shadow reported when it fails to reload", () => { + vi.useFakeTimers(); + vi.spyOn(console, "error").mockImplementation(() => {}); + const onPreviewReloadFailed = vi.fn(); + const { getApi, gen, root } = beginReload({ onPreviewReloadFailed }); + const sealed = sealedPreviewFrame().iframe; + Object.defineProperty(sealed, "isConnected", { value: true }); + act(() => { + getApi().setShadowIframeNode(sealed); + getApi().onShadowIframeLoad(gen); + }); + + act(() => void vi.advanceTimersByTime(FULL_WAIT_MS)); + + const [message] = onPreviewReloadFailed.mock.calls[0]!; + expect(message).toContain("still waiting for its runtime to report a timeline"); + expect(message).not.toContain("Its page had got to"); + unmount(root); + }); + + // fails without the fix: a timeline accepted but not yet committed (restore seek painting) read as none. + it("names the restore seek when a shadow's timeline arrived but its seek never painted", () => { + vi.useFakeTimers(); + vi.spyOn(console, "error").mockImplementation(() => {}); + const onPreviewReloadFailed = vi.fn(); + const { getApi, gen, root } = beginReload({ onPreviewReloadFailed }); + const shadow = makeShadowWithSpies(); + Object.assign(shadow.iframe.contentWindow!, { + __hfWaitForSeekCompletion: () => new Promise(() => {}), + }); + act(() => { + getApi().setShadowIframeNode(shadow.iframe); + getApi().onShadowIframeLoad(gen); + }); + + act(() => void vi.advanceTimersByTime(FULL_WAIT_MS)); + + const [message] = onPreviewReloadFailed.mock.calls[0]!; + expect(message).toContain("still waiting for its restore seek to paint"); + unmount(root); + }); + it("names the loaders when a shadow with a ready timeline never clears them", () => { vi.useFakeTimers(); vi.spyOn(console, "error").mockImplementation(() => {});