diff --git a/packages/studio/src/hooks/useSdkSession.lifecycle.test.tsx b/packages/studio/src/hooks/useSdkSession.lifecycle.test.tsx index 7894614166..01f2a8705d 100644 --- a/packages/studio/src/hooks/useSdkSession.lifecycle.test.tsx +++ b/packages/studio/src/hooks/useSdkSession.lifecycle.test.tsx @@ -12,6 +12,7 @@ vi.mock("@hyperframes/sdk", () => ({ import type { Composition } from "@hyperframes/sdk"; import { useSdkSession, type SdkSessionHandle } from "./useSdkSession"; +import { usePlayerStore } from "../player/store/playerStore"; vi.mock("../utils/studioTelemetry", () => ({ trackStudioEvent: vi.fn() })); @@ -19,6 +20,10 @@ import { trackStudioEvent } from "../utils/studioTelemetry"; const trackMock = vi.mocked(trackStudioEvent); +beforeEach(() => { + usePlayerStore.setState({ previewBooted: true }); +}); + function Probe({ projectId }: { projectId: string }) { useSdkSession(projectId, "index.html"); return null; @@ -54,6 +59,24 @@ describe("useSdkSession ownership", () => { vi.unstubAllGlobals(); }); + it("does not read or parse the composition until the live preview has booted", async () => { + usePlayerStore.setState({ previewBooted: false }); + const fetchStub = vi.fn(async () => response("PROJECT_A")); + vi.stubGlobal("fetch", fetchStub); + openComposition.mockImplementation(async () => fakeSession()); + + const root = createRoot(document.createElement("div")); + await act(async () => root.render()); + await flushAsyncEffects(); + expect(fetchStub).not.toHaveBeenCalled(); + expect(openComposition).not.toHaveBeenCalled(); + + await act(async () => usePlayerStore.getState().markPreviewBooted()); + await flushAsyncEffects(); + expect(openComposition).toHaveBeenCalledWith("PROJECT_A", { history: false }); + act(() => root.unmount()); + }); + it("hides project A immediately while project B with the same path is still opening", async () => { const sessionA = fakeSession(); const publishedA = fakeSession(); diff --git a/packages/studio/src/hooks/useSdkSession.ts b/packages/studio/src/hooks/useSdkSession.ts index df327fe39a..69b02bdf14 100644 --- a/packages/studio/src/hooks/useSdkSession.ts +++ b/packages/studio/src/hooks/useSdkSession.ts @@ -5,6 +5,7 @@ import { isSelfWriteEcho } from "./sdkSelfWriteRegistry"; import { trackStudioEvent } from "../utils/studioTelemetry"; import type { PublishSdkSession } from "../utils/sdkCutover"; import { addExternalFileReloadListener } from "./externalFileReloadBus"; +import { whenPreviewBooted } from "../player/store/playerStore"; /** * Why an optional project-file read produced no usable content. `stage: "read"` @@ -393,9 +394,12 @@ export function useSdkSession( generation, }; - readProjectFileOptional(projectId, activeCompPath) + // Parsing the source for editing takes the main thread for hundreds of ms on a large film; + // the first frame and play do not need it, so it waits for the live preview to boot. + whenPreviewBooted() + .then(() => (cancelled ? null : readProjectFileOptional(projectId, activeCompPath))) .then(async (read) => { - if (cancelled) return; + if (!read || cancelled) return; if (!read.ok) { const pathInTree = fileTreeLoaded ? fileTree.includes(activeCompPath) : null; setUnreachableProject(reportReadFailure(read, projectId, pathInTree));