From e8f3cc9a0b96cf1868ca6ad772e1682fc7a9d666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Tue, 22 Sep 2026 21:50:14 -0400 Subject: [PATCH] perf(studio): open the editing session after the preview boots Opening a project read the composition source and parsed it for the editing session (linkedom plus hf-id assignment) on the main thread while the live preview was booting on the same thread: ~300 ms on a large film. The session now opens once the preview has booted; nothing the first frame shows needs it. --- .../hooks/useSdkSession.lifecycle.test.tsx | 23 +++++++++++++++++++ packages/studio/src/hooks/useSdkSession.ts | 8 +++++-- 2 files changed, 29 insertions(+), 2 deletions(-) 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));