diff --git a/packages/studio/src/player/components/Player.test.ts b/packages/studio/src/player/components/Player.test.ts index 2f027be97a..a134365b65 100644 --- a/packages/studio/src/player/components/Player.test.ts +++ b/packages/studio/src/player/components/Player.test.ts @@ -129,6 +129,21 @@ describe("preview errors", () => { root = null; }); + it("leaves a callback ref alone on unmount, so a frame its owner made live since is kept", async () => { + // A shadow reload's promotion makes its frame the live one without calling the live Player's ref. + const ref = vi.fn(); + const { player } = await mountPlayer({ ref } as Partial); + const frame = ref.mock.calls[0]?.[0] as HTMLIFrameElement; + player.shadowRoot!.append(frame); // where keeps its frame + expect(frame.isConnected).toBe(true); + + act(() => root?.unmount()); + root = null; + + expect(ref).toHaveBeenCalledTimes(1); + expect(frame.isConnected).toBe(false); + }); + it("puts the preview on the player's once-a-second paused heartbeat", async () => { const { player } = await mountPlayer(); expect(player.hasAttribute("low-power-idle")).toBe(true); diff --git a/packages/studio/src/player/components/Player.tsx b/packages/studio/src/player/components/Player.tsx index 5907947706..78370ae000 100644 --- a/packages/studio/src/player/components/Player.tsx +++ b/packages/studio/src/player/components/Player.tsx @@ -377,11 +377,11 @@ export const Player = forwardRef( // During crossfade refreshes the retiring Player unmounts after the // new Player has already assigned its iframe to the same ref — blindly // nulling it would break seeking in the new Player. - // Callback refs are skipped — we can't read back the current value to - // guard against clobbering a newer assignment. The mutable-ref branch - // (the only path used today) is guarded by identity check. + // Callback refs are left alone: a shadow reload's promotion repoints + // the live frame without calling them, so a null here would wipe it. + // Their owners check `isConnected` instead (useShadowPreviewReload). if (typeof ref === "function") { - // no-op: can't safely guard callback refs + // no-op } else if (ref) { const mutableRef = ref as React.MutableRefObject; if (mutableRef.current === iframe) { diff --git a/packages/studio/src/player/hooks/timelinePlayerTestHarness.ts b/packages/studio/src/player/hooks/timelinePlayerTestHarness.ts index edfe23733b..94843e2a5b 100644 --- a/packages/studio/src/player/hooks/timelinePlayerTestHarness.ts +++ b/packages/studio/src/player/hooks/timelinePlayerTestHarness.ts @@ -67,6 +67,7 @@ export function makeFakeIframe(iframeWindow: Record): HTMLIFram value: document.implementation.createHTMLDocument("preview"), configurable: true, }); + document.body.appendChild(iframe); return iframe; } diff --git a/packages/studio/src/player/hooks/useShadowPreviewReload.ts b/packages/studio/src/player/hooks/useShadowPreviewReload.ts index 8868dc4617..a8520a8675 100644 --- a/packages/studio/src/player/hooks/useShadowPreviewReload.ts +++ b/packages/studio/src/player/hooks/useShadowPreviewReload.ts @@ -114,6 +114,7 @@ export function useShadowPreviewReload({ const budgetsSpentRef = useRef(0); // The shadow still owed a wait budget, so a hidden tab can resume it when it becomes visible. const budgetGenRef = useRef(null); + const shadowFrameLeftPageRef = useRef(false); const cancelPendingLoadRef = useRef<() => void>(() => {}); const stopHoldRef = useRef<() => void>(() => {}); const shadowUrlRef = useRef(""); @@ -127,6 +128,7 @@ export function useShadowPreviewReload({ const stopPendingShadow = useCallback(() => { clearTimeout(readyTimerRef.current); budgetGenRef.current = null; + shadowFrameLeftPageRef.current = false; cancelPendingLoadRef.current(); stopHoldRef.current(); pendingCommitRef.current = null; @@ -296,14 +298,10 @@ export function useShadowPreviewReload({ [promoteWhenReady], ); - const setShadowIframeNode = useCallback((node: HTMLIFrameElement | null) => { - shadowIframeRef.current = node; - }, []); - const armReadyTimer = useCallback( (gen: number) => { clearTimeout(readyTimerRef.current); - if (isDocumentHidden()) return; + if (isDocumentHidden() || shadowFrameLeftPageRef.current) return; let drew = false; let starved = false; requestAnimationFrame(() => { @@ -317,6 +315,12 @@ export function useShadowPreviewReload({ logReload("shadow-starved", { gen }); return; } + const frame = shadowIframeRef.current; + if (frame && !frame.isConnected) { + shadowFrameLeftPageRef.current = true; + logReload("shadow-detached", { gen }); + return; + } budgetsSpentRef.current += 1; if (budgetsSpentRef.current < SHADOW_READY_BUDGETS) { logReload("shadow-slow", { budgetsSpent: budgetsSpentRef.current }); @@ -331,6 +335,17 @@ export function useShadowPreviewReload({ const armReadyTimerRef = useRef(armReadyTimer); armReadyTimerRef.current = armReadyTimer; + const setShadowIframeNode = useCallback((node: HTMLIFrameElement | null) => { + shadowIframeRef.current = node; + const gen = budgetGenRef.current; + if (!node || !shadowFrameLeftPageRef.current || gen == null || gen !== shadowGenRef.current) + return; + shadowFrameLeftPageRef.current = false; + budgetsSpentRef.current = 0; + logReload("shadow-reattached", { gen }); + armReadyTimerRef.current(gen); + }, []); + const beginShadowReload = useCallback( (url: string) => { retry.started(); diff --git a/packages/studio/src/player/hooks/useTimelinePlayer.shadowReload.test.ts b/packages/studio/src/player/hooks/useTimelinePlayer.shadowReload.test.ts index f1423905b4..5309976b8a 100644 --- a/packages/studio/src/player/hooks/useTimelinePlayer.shadowReload.test.ts +++ b/packages/studio/src/player/hooks/useTimelinePlayer.shadowReload.test.ts @@ -533,6 +533,52 @@ describe("shadow reload readiness and failure", () => { unmount(root); }); + it("keeps a reload whose frame left the page, and promotes it once a new frame mounts", () => { + vi.useFakeTimers(); + const onPreviewReloadFailed = vi.fn(); + const { getApi, gen, live, root } = beginReload({ onPreviewReloadFailed }); + const first = makeShadowWithSpies(); + act(() => getApi().setShadowIframeNode(first.iframe)); + + // The pane holding the preview unmounts mid-reload: its Players remove their frames. + act(() => first.iframe.remove()); + act(() => void vi.advanceTimersByTime(FULL_WAIT_MS * 2)); + expect(onPreviewReloadFailed).not.toHaveBeenCalled(); + expect(getApi().iframeRef.current).toBe(live); + + // The pane comes back: a new Player for the same slot mounts a new frame, which loads. + const again = makeShadowWithSpies(); + act(() => { + getApi().setShadowIframeNode(again.iframe); + getApi().onShadowIframeLoad(gen); + }); + act(() => getApi().onShadowReadyChange(gen, true)); + expect(getApi().iframeRef.current).toBe(again.iframe); + expect(onPreviewReloadFailed).not.toHaveBeenCalled(); + unmount(root); + }); + + it("gives a frame that mounts again its full budgets, then drops it if it stays silent", () => { + vi.useFakeTimers(); + vi.spyOn(console, "error").mockImplementation(() => {}); + const onPreviewReloadFailed = vi.fn(); + const { getApi, live, root } = beginReload({ onPreviewReloadFailed }); + const first = makeShadowWithSpies(); + act(() => getApi().setShadowIframeNode(first.iframe)); + act(() => void vi.advanceTimersByTime(SHADOW_READY_TIMEOUT_MS + 1000)); + act(() => first.iframe.remove()); + act(() => void vi.advanceTimersByTime(FULL_WAIT_MS)); + expect(onPreviewReloadFailed).not.toHaveBeenCalled(); + + act(() => getApi().setShadowIframeNode(makeShadowWithSpies().iframe)); + act(() => void vi.advanceTimersByTime(FULL_WAIT_MS - 1)); + expect(onPreviewReloadFailed).not.toHaveBeenCalled(); + act(() => void vi.advanceTimersByTime(1)); + expect(onPreviewReloadFailed).toHaveBeenCalledWith(expect.stringContaining("too long")); + expect(getApi().iframeRef.current).toBe(live); + unmount(root); + }); + it("does not spend the budget while the tab is hidden, and promotes once it is visible", () => { vi.useFakeTimers(); const onPreviewReloadFailed = vi.fn();