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.
This commit is contained in:
Miguel Ángel
2026-09-23 21:15:14 -04:00
parent e8f3cc9a0b
commit 689fda4987
6 changed files with 50 additions and 3 deletions
@@ -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<FocusOptions | undefined> = [];
@@ -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(
@@ -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);
@@ -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"));
@@ -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<typeof setTimeout> | 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;
}
/**
@@ -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();