Skip to content

Commit 72a6e7b

Browse files
fix(studio): a closed preview never starts a reload it cannot show (#8811)
* fix(studio): a closed preview never starts a reload it cannot show A reload asked after the player unmounted (its project closed, or the person went Home, while a refresh was still on its way) armed the shadow wait timer with no frame to load into. The next project's preview then counted as changing for 45 s (a press on it waited up to 10 s, composition thumbnails were held), and the give-up and its retry were both reported against that preview as 'no-frame'. beginShadowReload now does nothing once the player has unmounted. * style(studio): shorten unmounted reload test header comment under 120 chars
1 parent 7fcc044 commit 72a6e7b

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎packages/studio/src/player/hooks/useShadowPreviewReload.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ export function useShadowPreviewReload({
177177
// The newest reload asked for while a still is being taken; it starts once the still is up.
178178
const holdingUrlRef = useRef<string | null>(null);
179179
const holdTokenRef = useRef(0);
180+
const mountedRef = useRef(true);
180181

181182
const restoringRef = useRef<(() => void) | null>(null);
182183
const dropRestoring = useCallback(() => {
@@ -466,6 +467,7 @@ export function useShadowPreviewReload({
466467

467468
const beginShadowReload = useCallback(
468469
(url: string) => {
470+
if (!mountedRef.current) return;
469471
const live = iframeRef.current;
470472
const take = holdStillRef.current;
471473
if (heldRef.current || !take || !live?.isConnected) {
@@ -527,6 +529,7 @@ export function useShadowPreviewReload({
527529

528530
// Hiding the tab pauses the budget; showing it again restarts the full budget for a pending shadow.
529531
useMountEffect(() => {
532+
mountedRef.current = true;
530533
const onVisibilityChange = () => {
531534
if (isDocumentHidden()) {
532535
clearTimeout(readyTimerRef.current);
@@ -538,6 +541,7 @@ export function useShadowPreviewReload({
538541
document.addEventListener("visibilitychange", onVisibilityChange);
539542
return () => {
540543
document.removeEventListener("visibilitychange", onVisibilityChange);
544+
mountedRef.current = false;
541545
retry.cancel();
542546
stopPendingShadow();
543547
heldRef.current?.release();
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// @vitest-environment happy-dom
2+
// A reload asked of an unmounted player starts nothing: no late failure and no changing mark on the open preview.
3+
4+
import { act } from "react";
5+
import { afterEach, describe, expect, it, vi } from "vitest";
6+
import { SHADOW_READY_BUDGETS, SHADOW_READY_TIMEOUT_MS } from "./useShadowPreviewReload";
7+
import { isPreviewChanging } from "../previewReloading";
8+
import {
9+
makeAdapterWindow,
10+
makeFakeIframe,
11+
renderTimelinePlayerHarness,
12+
resetPlayerStore,
13+
} from "./timelinePlayerTestHarness";
14+
15+
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
16+
17+
vi.mock("../../utils/gsapSoftReload", async (importOriginal) => ({
18+
...(await importOriginal<typeof import("../../utils/gsapSoftReload")>()),
19+
ensureMotionPathPluginLoaded: vi.fn(),
20+
}));
21+
22+
afterEach(() => {
23+
vi.useRealTimers();
24+
vi.restoreAllMocks();
25+
document.body.innerHTML = "";
26+
resetPlayerStore();
27+
});
28+
29+
describe("a reload asked of an unmounted player", () => {
30+
it("never starts, so it never gives up and the open preview is not held as changing", () => {
31+
vi.useFakeTimers();
32+
vi.spyOn(console, "error").mockImplementation(() => {});
33+
const onPreviewReloadFailed = vi.fn();
34+
const { getApi, root } = renderTimelinePlayerHarness({ onPreviewReloadFailed });
35+
act(() => {
36+
getApi().iframeRef.current = makeFakeIframe(makeAdapterWindow().win);
37+
getApi().onIframeLoad();
38+
});
39+
const api = getApi();
40+
41+
act(() => root.unmount());
42+
api.refreshPlayer();
43+
expect(isPreviewChanging()).toBe(false);
44+
vi.advanceTimersByTime(SHADOW_READY_TIMEOUT_MS * SHADOW_READY_BUDGETS * 3);
45+
46+
expect(onPreviewReloadFailed).not.toHaveBeenCalled();
47+
});
48+
});

0 commit comments

Comments
 (0)