From 689fda4987a4a22dba9378a1c53edc4c8a8f287a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Tue, 22 Sep 2026 21:51:02 -0400 Subject: [PATCH] perf(studio): park the editor overlays until the preview boots The overlay frame loop polled preview geometry and off-canvas indicators from the first frame, forcing layout on a preview document that was still loading, on the thread its boot runs on: ~200 ms before the first frame on a large film. The loop now skips its subscribers until the preview has booted, then wakes. --- .../src/components/editor/DomEditOverlay.test.ts | 5 +++++ .../editor/PreviewOverlayProvider.test.tsx | 7 ++++++- .../editor/offCanvasIndicatorRefresh.test.tsx | 7 ++++++- .../components/editor/overlayFrameLoop.test.ts | 16 ++++++++++++++++ .../src/components/editor/overlayFrameLoop.ts | 11 +++++++++++ .../editor/usePreviewCompositionRect.test.tsx | 7 ++++++- 6 files changed, 50 insertions(+), 3 deletions(-) diff --git a/packages/studio/src/components/editor/DomEditOverlay.test.ts b/packages/studio/src/components/editor/DomEditOverlay.test.ts index a7270849cd..2aef34ef98 100644 --- a/packages/studio/src/components/editor/DomEditOverlay.test.ts +++ b/packages/studio/src/components/editor/DomEditOverlay.test.ts @@ -18,6 +18,7 @@ import { hoverCacheDescribesPoint, resolveResizeCenterAnchorOffset, } from "./domEditOverlayGestures"; +import { usePlayerStore } from "../../player/store/playerStore"; // React 19 warns unless the test environment opts into act(). globalThis.IS_REACT_ACT_ENVIRONMENT = true; @@ -242,6 +243,10 @@ function dispatchOverlayPointerDown(target: Element, clientX = 120, clientY = 80 }); } +beforeEach(() => { + usePlayerStore.setState({ previewBooted: true }); +}); + describe("focusDomEditOverlayElement", () => { it("focuses the canvas overlay without scrolling", () => { const calls: Array = []; diff --git a/packages/studio/src/components/editor/PreviewOverlayProvider.test.tsx b/packages/studio/src/components/editor/PreviewOverlayProvider.test.tsx index 5a21b80adb..788cea4c58 100644 --- a/packages/studio/src/components/editor/PreviewOverlayProvider.test.tsx +++ b/packages/studio/src/components/editor/PreviewOverlayProvider.test.tsx @@ -2,10 +2,11 @@ import { act } from "react"; import { createRoot } from "react-dom/client"; -import { afterEach, describe, expect, it } from "vitest"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { GridOverlay } from "./GridOverlay"; import { PreviewOverlayProvider } from "./PreviewOverlayProvider"; import { usePreviewIframeStore } from "../../player/store/previewIframeStore"; +import { usePlayerStore } from "../../player/store/playerStore"; (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; @@ -27,6 +28,10 @@ function compositionFrame() { return iframe; } +beforeEach(() => { + usePlayerStore.setState({ previewBooted: true }); +}); + describe("PreviewOverlayProvider", () => { it("derives grid geometry after the iframe prop becomes available", async () => { window.localStorage.setItem( diff --git a/packages/studio/src/components/editor/offCanvasIndicatorRefresh.test.tsx b/packages/studio/src/components/editor/offCanvasIndicatorRefresh.test.tsx index 8e75289046..aecc411176 100644 --- a/packages/studio/src/components/editor/offCanvasIndicatorRefresh.test.tsx +++ b/packages/studio/src/components/editor/offCanvasIndicatorRefresh.test.tsx @@ -2,9 +2,10 @@ import React, { act } from "react"; import { createRoot, type Root } from "react-dom/client"; -import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest"; import { DomEditOverlay } from "./DomEditOverlay"; import { RECOMPUTE_INTERVAL_MS } from "./offCanvasIndicatorRefresh"; +import { usePlayerStore } from "../../player/store/playerStore"; Reflect.set(globalThis, "IS_REACT_ACT_ENVIRONMENT", true); @@ -131,6 +132,10 @@ function mountOverlayWithHeadline(initialLeft: number): OverlayHarness { }; } +beforeEach(() => { + usePlayerStore.setState({ previewBooted: true }); +}); + describe("off-canvas indicator refresh", () => { it("removes the indicator when an off-canvas element moves in-canvas (off->on)", async () => { const h = mountOverlayWithHeadline(760); diff --git a/packages/studio/src/components/editor/overlayFrameLoop.test.ts b/packages/studio/src/components/editor/overlayFrameLoop.test.ts index a0cf26c0c0..7601e0555c 100644 --- a/packages/studio/src/components/editor/overlayFrameLoop.test.ts +++ b/packages/studio/src/components/editor/overlayFrameLoop.test.ts @@ -7,6 +7,7 @@ import { resetOverlayFrameLoopForTests, subscribeOverlayFrame, } from "./overlayFrameLoop"; +import { usePlayerStore } from "../../player/store/playerStore"; /** * The editor's overlay polls run on one shared, parkable frame loop. The two @@ -37,6 +38,7 @@ describe("overlay frame loop", () => { }; beforeEach(() => { + usePlayerStore.setState({ previewBooted: true }); vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "performance", "Date"] }); queued = []; window.requestAnimationFrame = ((callback: FrameRequestCallback) => { @@ -55,6 +57,20 @@ describe("overlay frame loop", () => { window.cancelAnimationFrame = originalCancelRaf; }); + it("does not poll the preview before it boots, and wakes when it does", () => { + usePlayerStore.setState({ previewBooted: false }); + let runs = 0; + subscribeOverlayFrame(() => { + runs += 1; + }); + framesOver(1000); + expect(runs).toBe(0); + + usePlayerStore.getState().markPreviewBooted(); + framesOver(32); + expect(runs).toBeGreaterThan(0); + }); + it("runs every subscriber on one frame, not one frame each", () => { const calls: string[] = []; subscribeOverlayFrame(() => calls.push("a")); diff --git a/packages/studio/src/components/editor/overlayFrameLoop.ts b/packages/studio/src/components/editor/overlayFrameLoop.ts index 62938b92f2..c770523368 100644 --- a/packages/studio/src/components/editor/overlayFrameLoop.ts +++ b/packages/studio/src/components/editor/overlayFrameLoop.ts @@ -25,6 +25,8 @@ * the next couple of hundred milliseconds, and no further event is dispatched * while it does. */ +import { usePlayerStore } from "../../player/store/playerStore"; + const AWAKE_MS = 400; /** How often a parked loop runs one frame anyway, in case a wake was missed. */ export const IDLE_POLL_MS = 250; @@ -35,6 +37,7 @@ let frameId: number | null = null; let idleTimerId: ReturnType | null = null; let awakeUntil = 0; let listenersAttached = false; +let stopBootWake: (() => void) | null = null; /** Wake sources. All passive reads; none of them can be cancelled by us. */ const WINDOW_EVENTS = [ @@ -111,6 +114,9 @@ function runFrame(): void { // it — and a shared loop makes that failure four overlays wide plus the // idle-poll safety net, permanently, rather than one overlay's own problem. schedule(); + // Nothing to track until the live preview boots, and a poll before then forces layout + // on a document that is still loading, on the thread the boot runs on. + if (!usePlayerStore.getState().previewBooted) return; for (const subscriber of subscribers) { try { subscriber(); @@ -154,6 +160,9 @@ function attachListeners(): void { window.addEventListener(type, requestOverlayFrames, { capture: true, passive: true }); } window.addEventListener("message", onPreviewMessage, { capture: true, passive: true }); + stopBootWake = usePlayerStore.subscribe((state, prev) => { + if (state.previewBooted && !prev.previewBooted) requestOverlayFrames(); + }); } function detachListeners(): void { @@ -163,6 +172,8 @@ function detachListeners(): void { window.removeEventListener(type, requestOverlayFrames, { capture: true }); } window.removeEventListener("message", onPreviewMessage, { capture: true }); + stopBootWake?.(); + stopBootWake = null; } /** diff --git a/packages/studio/src/components/editor/usePreviewCompositionRect.test.tsx b/packages/studio/src/components/editor/usePreviewCompositionRect.test.tsx index 096d8b6e01..e020e98f70 100644 --- a/packages/studio/src/components/editor/usePreviewCompositionRect.test.tsx +++ b/packages/studio/src/components/editor/usePreviewCompositionRect.test.tsx @@ -2,12 +2,13 @@ import { act, useRef, useState } from "react"; import { createRoot } from "react-dom/client"; -import { afterEach, describe, expect, it } from "vitest"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { resetOverlayFrameLoopForTests } from "./overlayFrameLoop"; import { usePreviewCompositionRect, type PreviewCompositionRect, } from "./usePreviewCompositionRect"; +import { usePlayerStore } from "../../player/store/playerStore"; (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; @@ -59,6 +60,10 @@ function compositionFrame(box: [number, number, number, number], width: number, const settle = () => new Promise((r) => setTimeout(r, 400)); +beforeEach(() => { + usePlayerStore.setState({ previewBooted: true }); +}); + describe("usePreviewCompositionRect", () => { it("reports the live preview iframe's box relative to the overlay, scaled to the composition", async () => { const probe = mountProbe();