perf(studio): keep lint off the boot

Opening a project started two lint processes while Studio booted: the
terminal's startup lint and Studio's background lint, ~4 s of CPU next to the
boot. Studio's background lint now waits until the live preview has booted,
and lint processes run at the lowest CPU priority so they only take idle CPU.
This commit is contained in:
Miguel Ángel
2026-09-22 22:26:57 -04:00
parent b833b02fb1
commit 17744297ab
6 changed files with 64 additions and 15 deletions
@@ -0,0 +1,22 @@
import { constants } from "node:os";
import { afterEach, expect, it, vi } from "vitest";
const os = vi.hoisted(() => ({ setPriority: vi.fn() }));
vi.mock("node:os", async (importOriginal) => ({
...(await importOriginal<typeof import("node:os")>()),
setPriority: os.setPriority,
}));
vi.mock("./utils/lintProject.js", () => ({ lintProject: vi.fn(async () => ({ ok: true })) }));
afterEach(() => vi.restoreAllMocks());
it("lints at the lowest CPU priority so it never competes with a Studio boot", async () => {
process.argv[2] = "lint";
process.env.HYPERFRAMES_RENDER_SETUP_INPUT = JSON.stringify({ projectDir: "/project" });
const write = vi.spyOn(process.stdout, "write").mockImplementation(() => true);
await import("./renderSetupWorker.js");
expect(os.setPriority).toHaveBeenCalledWith(constants.priority.PRIORITY_LOW);
expect(write).toHaveBeenCalledWith(expect.stringContaining('{"ok":true}'));
});
+3
View File
@@ -1,4 +1,5 @@
// fallow-ignore-file unused-file
import { constants, setPriority } from "node:os";
import { ensureBrowser, releaseOwnedBrowserInstallLock } from "./browser/manager.js";
import { lintProject } from "./utils/lintProject.js";
import { killOrphanedProcesses } from "./utils/orphanCleanup.js";
@@ -19,6 +20,8 @@ let result: unknown;
if (mode === "browser") {
result = await ensureBrowser(input);
} else if (mode === "lint") {
// Nobody waits on a background lint the way they wait on a Studio boot, so it takes idle CPU only.
setPriority(constants.priority.PRIORITY_LOW);
result = await lintProject(
input.projectDir,
input.entryFile,
@@ -5,7 +5,7 @@
*/
import type { GsapAnimation, GsapKeyframesData, ParsedGsap } from "@hyperframes/core/gsap-parser";
import { isStudioHoldSet } from "@hyperframes/core/gsap-parser";
import { usePlayerStore } from "../player/store/playerStore";
import { usePlayerStore, whenPreviewBooted } from "../player/store/playerStore";
import { replaceKeyframeCacheForFile } from "./gsapKeyframeCacheHelpers";
import { resolveClipTimingBasis, resolveSelectorElementIds, toClipKeyframes } from "./gsapShared";
import {
@@ -70,23 +70,12 @@ export function fetchParsedAnimations(
return request;
}
/** The parse feeds keyframes and the Design panel, not the first frame, and it runs on the
* server's only event loop, so it waits until the project's live preview has booted. */
function afterPreviewBoot(): Promise<void> {
return new Promise((resolve) => {
const stop = usePlayerStore.subscribe((state) => {
if (!state.previewBooted) return;
stop();
resolve();
});
});
}
async function requestParsedAnimations(
projectId: string,
sourceFile: string,
): Promise<ParsedGsapAnimations | null> {
if (!usePlayerStore.getState().previewBooted) await afterPreviewBoot();
// The parse runs on the server's only event loop and feeds keyframes, not the first frame.
if (!usePlayerStore.getState().previewBooted) await whenPreviewBooted();
try {
const res = await fetch(
`/api/projects/${encodeURIComponent(projectId)}/gsap-animations/${encodeURIComponent(sourceFile)}`,
@@ -2,8 +2,9 @@
import { act } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { useLintModal } from "./useLintModal";
import { usePlayerStore } from "../player/store/playerStore";
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
@@ -15,6 +16,10 @@ function Probe() {
return null;
}
beforeEach(() => {
usePlayerStore.setState({ previewBooted: true });
});
afterEach(() => {
if (root) act(() => root?.unmount());
root = null;
@@ -63,3 +68,17 @@ describe("useLintModal hasLintError", () => {
expect(latest?.hasLintError).toBe(true);
});
});
describe("useLintModal background lint", () => {
it("waits for the live preview to boot before asking the server to lint", async () => {
usePlayerStore.setState({ previewBooted: false });
const fetchStub = vi.fn(lintResponse([]));
vi.stubGlobal("fetch", fetchStub);
root = createRoot(document.createElement("div"));
await act(async () => root?.render(<Probe />));
expect(fetchStub).not.toHaveBeenCalled();
await act(async () => usePlayerStore.getState().markPreviewBooted());
expect(fetchStub).toHaveBeenCalledTimes(1);
});
});
@@ -2,6 +2,7 @@ import { buildProjectApiPath } from "../utils/projectRouting";
import { useState, useCallback, useEffect, useRef, useMemo } from "react";
import type { LintFinding } from "../components/LintModal";
import { usePlayerStore } from "../player";
import { whenPreviewBooted } from "../player/store/playerStore";
interface RawFinding {
severity?: string;
@@ -35,6 +36,8 @@ export function useLintModal(projectId: string | null, refreshKey?: number) {
async (opts?: { background?: boolean }) => {
if (!projectId) return;
if (!opts?.background) setLinting(true);
// A background lint is a separate process on the server; it must not compete with the boot.
else if (!usePlayerStore.getState().previewBooted) await whenPreviewBooted();
try {
const res = await fetch(buildProjectApiPath(projectId, `/lint`));
const data = await res.json();
@@ -551,3 +551,16 @@ export const usePlayerStore = create<PlayerState>((set, get) => ({
}));
attachPlayerStoreDevHandle(usePlayerStore);
/** Resolves once the project's live preview has booted; open-time work that the first frame
* does not need (server parses, lint) waits on it instead of competing with the boot. */
export function whenPreviewBooted(): Promise<void> {
return new Promise((resolve) => {
if (usePlayerStore.getState().previewBooted) return resolve();
const stop = usePlayerStore.subscribe((state) => {
if (!state.previewBooted) return;
stop();
resolve();
});
});
}