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.
This commit is contained in:
Miguel Ángel
2026-09-23 21:15:14 -04:00
parent cf9a6563da
commit e8f3cc9a0b
2 changed files with 29 additions and 2 deletions
@@ -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(<Probe projectId="project-a" />));
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();
+6 -2
View File
@@ -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));