diff --git a/packages/cli/src/server/thumbnailPages.test.ts b/packages/cli/src/server/thumbnailPages.test.ts index 501dba5ead..95cf9cf8a8 100644 --- a/packages/cli/src/server/thumbnailPages.test.ts +++ b/packages/cli/src/server/thumbnailPages.test.ts @@ -56,4 +56,45 @@ describe("createThumbnailPages", () => { await thumbnails.withPage(browser, "/preview", "v2", load, async () => null); expect(load).toHaveBeenCalledTimes(3); }); + + it("closes a page nobody has used for a while, so an idle Studio runs no composition", async () => { + vi.useFakeTimers(); + try { + const { browser, pages } = fakeBrowser(); + const thumbnails = createThumbnailPages(2, 10_000); + const load = vi.fn(async () => {}); + + await thumbnails.withPage(browser, "/preview", "v1", load, async () => null); + await vi.advanceTimersByTimeAsync(9_999); + expect(pages[0]?.close).not.toHaveBeenCalled(); + await vi.advanceTimersByTimeAsync(1); + expect(pages[0]?.close).toHaveBeenCalled(); + + await thumbnails.withPage(browser, "/preview", "v1", load, async () => null); + expect(load).toHaveBeenCalledTimes(2); + } finally { + vi.useRealTimers(); + } + }); + + it("never closes a page under a frame that is still being taken", async () => { + const { browser, pages } = fakeBrowser(); + const thumbnails = createThumbnailPages(1); + const load = vi.fn(async () => {}); + let finish: () => void = () => {}; + const slow = thumbnails.withPage( + browser, + "/a", + "v1", + load, + () => new Promise((r) => (finish = r)), + ); + await vi.waitFor(() => expect(pages).toHaveLength(1)); + + await thumbnails.withPage(browser, "/b", "v1", load, async () => null); + expect(pages[0]?.close).not.toHaveBeenCalled(); + finish(); + await slow; + await vi.waitFor(() => expect(pages[0]?.close).toHaveBeenCalled()); + }); }); diff --git a/packages/cli/src/server/thumbnailPages.ts b/packages/cli/src/server/thumbnailPages.ts index 2804a4e159..8b6b4af12b 100644 --- a/packages/cli/src/server/thumbnailPages.ts +++ b/packages/cli/src/server/thumbnailPages.ts @@ -5,17 +5,25 @@ interface LoadedPage { version: string; page: Promise; queue: Promise; + idleTimer?: ReturnType; } /** One loaded preview page per document URL: a thumbnail is a seek and a screenshot on it, not a - * fresh load of the whole composition. A new `version` (project content) reloads the page, and - * calls on one page run one at a time so their seeks never interleave. */ -export function createThumbnailPages(maxPages = 2) { + * fresh load of the whole composition. A new `version` (project content) reloads the page, calls on + * one page run one at a time so their seeks never interleave, and a page unused for `idleMs` closes + * so an idle Studio keeps no composition running. */ +export function createThumbnailPages(maxPages = 2, idleMs = 10_000) { const pages = new Map(); const drop = (url: string) => { const entry = pages.get(url); + if (!entry) return; pages.delete(url); - void entry?.page.then((page) => page.close()).catch(() => {}); + clearTimeout(entry.idleTimer); + // After any frame still running on it. + void entry.queue + .then(() => entry.page) + .then((page) => page.close()) + .catch(() => {}); }; return { async withPage( @@ -40,6 +48,7 @@ export function createThumbnailPages(maxPages = 2) { while (pages.size > maxPages) drop(pages.keys().next().value!); } const current = entry; + clearTimeout(current.idleTimer); const run = current.queue.then(async () => use(await current.page)); current.queue = run.catch(() => {}); try { @@ -47,6 +56,14 @@ export function createThumbnailPages(maxPages = 2) { } catch (error) { if (pages.get(url) === current) drop(url); throw error; + } finally { + if (pages.get(url) === current) { + clearTimeout(current.idleTimer); + current.idleTimer = setTimeout(() => { + if (pages.get(url) === current) drop(url); + }, idleMs); + current.idleTimer.unref?.(); + } } }, closeAll(): void {