Skip to content

Commit 4c39ad1

Browse files
committed
fix(studio): a late ready from the replaced preview page no longer claims the new page
1 parent 8143380 commit 4c39ad1

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎packages/studio/src/player/components/Player.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ describe("callbacks after the player is already mounted", () => {
203203
const iframe = (player as TestHyperframesPlayer).iframeElement;
204204
let page = {};
205205
Object.defineProperty(iframe, "contentDocument", { get: () => page });
206+
Object.defineProperty(iframe, "contentWindow", { get: () => ({ __playerReady: true }) });
206207
const announced = vi.fn();
207208
onPreviewDocumentLoaded(iframe, announced);
208209

@@ -219,6 +220,26 @@ describe("callbacks after the player is already mounted", () => {
219220
expect(announced).toHaveBeenCalledTimes(2);
220221
});
221222

223+
it("leaves a ready that arrives before the page's runtime boots to the page's own load", async () => {
224+
const onLoad = vi.fn();
225+
const { player } = await mountPlayer({ onLoad });
226+
const iframe = (player as TestHyperframesPlayer).iframeElement;
227+
const page = {};
228+
let runtime: { __playerReady?: boolean } = {};
229+
Object.defineProperty(iframe, "contentDocument", { get: () => page });
230+
Object.defineProperty(iframe, "contentWindow", { get: () => runtime });
231+
232+
act(() => void player.dispatchEvent(new Event("ready")));
233+
expect(onLoad).not.toHaveBeenCalled();
234+
235+
act(() => void iframe.dispatchEvent(new Event("load")));
236+
expect(onLoad).toHaveBeenCalledTimes(1);
237+
238+
runtime = { __playerReady: true };
239+
act(() => void player.dispatchEvent(new Event("ready")));
240+
expect(onLoad).toHaveBeenCalledTimes(1);
241+
});
242+
222243
it("reports the preview error cause", async () => {
223244
const onPreviewError = vi.fn();
224245
const { player } = await mountPlayer({ onPreviewError });

‎packages/studio/src/player/components/Player.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { applyPreviewVariablesToUrl } from "../../hooks/previewVariablesStore";
66
import { HyperframesLoader } from "../../components/ui";
77
import { usePlayerStore } from "../store/playerStore";
88
import { announcePreviewDocumentLoaded } from "../sceneSwap";
9+
import type { IframeWindow } from "../lib/playbackTypes";
910
// Importing "@hyperframes/player" registers a class extending HTMLElement at
1011
// module load, which throws under SSR, hence the dynamic import behind a
1112
// `typeof window` guard. Kicking it here rather than in the mount effect puts
@@ -137,6 +138,9 @@ export function hasUnloadedAssets(iframe: HTMLIFrameElement, lastResult: boolean
137138
* forwarded ref so useTimelinePlayer can access it for clip manifest parsing,
138139
* timeline probing, and DOM inspection.
139140
*/
141+
const runtimeBootedIn = (iframe: HTMLIFrameElement) =>
142+
(iframe.contentWindow as IframeWindow | null)?.__playerReady === true;
143+
140144
export const Player = forwardRef<HTMLIFrameElement, PlayerProps>(
141145
(
142146
{
@@ -233,7 +237,7 @@ export const Player = forwardRef<HTMLIFrameElement, PlayerProps>(
233237
const handleReady = () => {
234238
setPreviewError(null);
235239
setCompositionLoading(false);
236-
if (iframe.contentDocument) handleLoad();
240+
if (iframe.contentDocument && runtimeBootedIn(iframe)) handleLoad();
237241
};
238242
const handlePainted = () => {
239243
setPainted(true);

‎packages/studio/src/player/lib/playbackTypes.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export interface ClipManifest {
5959

6060
export type IframeWindow = Window & {
6161
__player?: RuntimePlaybackAdapter;
62+
__playerReady?: boolean;
6263
__timeline?: TimelineLike;
6364
__timelines?: Record<string, TimelineLike>;
6465
__clipManifest?: ClipManifest;

0 commit comments

Comments
 (0)