diff --git a/packages/cli/src/renderSetupWorker.priority.test.ts b/packages/cli/src/renderSetupWorker.priority.test.ts new file mode 100644 index 0000000000..65fae559c7 --- /dev/null +++ b/packages/cli/src/renderSetupWorker.priority.test.ts @@ -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()), + 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}')); +}); diff --git a/packages/cli/src/renderSetupWorker.ts b/packages/cli/src/renderSetupWorker.ts index 6126f2cf2e..658e4020ba 100644 --- a/packages/cli/src/renderSetupWorker.ts +++ b/packages/cli/src/renderSetupWorker.ts @@ -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, diff --git a/packages/studio/src/hooks/keyframeCacheAstLoad.ts b/packages/studio/src/hooks/keyframeCacheAstLoad.ts index de8a720c89..30ff1b6a0a 100644 --- a/packages/studio/src/hooks/keyframeCacheAstLoad.ts +++ b/packages/studio/src/hooks/keyframeCacheAstLoad.ts @@ -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 { - return new Promise((resolve) => { - const stop = usePlayerStore.subscribe((state) => { - if (!state.previewBooted) return; - stop(); - resolve(); - }); - }); -} - async function requestParsedAnimations( projectId: string, sourceFile: string, ): Promise { - 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)}`, diff --git a/packages/studio/src/hooks/useLintModal.test.tsx b/packages/studio/src/hooks/useLintModal.test.tsx index e85494abe2..553daa38af 100644 --- a/packages/studio/src/hooks/useLintModal.test.tsx +++ b/packages/studio/src/hooks/useLintModal.test.tsx @@ -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()); + expect(fetchStub).not.toHaveBeenCalled(); + + await act(async () => usePlayerStore.getState().markPreviewBooted()); + expect(fetchStub).toHaveBeenCalledTimes(1); + }); +}); diff --git a/packages/studio/src/hooks/useLintModal.ts b/packages/studio/src/hooks/useLintModal.ts index 43ee501514..3b8ee081f3 100644 --- a/packages/studio/src/hooks/useLintModal.ts +++ b/packages/studio/src/hooks/useLintModal.ts @@ -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(); diff --git a/packages/studio/src/player/store/playerStore.ts b/packages/studio/src/player/store/playerStore.ts index 39be33fcde..51e7a1bf6d 100644 --- a/packages/studio/src/player/store/playerStore.ts +++ b/packages/studio/src/player/store/playerStore.ts @@ -551,3 +551,16 @@ export const usePlayerStore = create((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 { + return new Promise((resolve) => { + if (usePlayerStore.getState().previewBooted) return resolve(); + const stop = usePlayerStore.subscribe((state) => { + if (!state.previewBooted) return; + stop(); + resolve(); + }); + }); +}