Skip to content

Commit d0404b4

Browse files
fix(studio): undo and redo are no longer held behind thumbnail renders (#4842)
* fix(studio): undo and redo are no longer held behind thumbnail renders * test(studio): composition card tests share one mount, and the bench's 2 s undo rule is tested * fix(studio): a composition card and its timeline clip share one thumbnail render * test(studio): bank opening a film at 15 thumbnail requests, one per composition * fix(studio): composition cards load near the viewport and keep their last frame until the next * fix(studio): a composition card keeps its last frame under cache pressure, and loads 200 px early
1 parent 0db5b0a commit d0404b4

13 files changed

Lines changed: 480 additions & 157 deletions

‎packages/studio/src/components/nle/NLEPreview.test.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,14 @@ describe("NLEPreview", () => {
413413
describe("a missing poster", () => {
414414
const renderUrl =
415415
"/api/projects/timeline-edit-playground/thumbnail/index.html?t=0&output=source";
416-
const fetchSpy = vi.fn(() => Promise.resolve(new Response()));
416+
const fetchSpy = vi.fn((_url: string, _init?: RequestInit) => Promise.resolve(new Response()));
417417
beforeEach(() => {
418418
fetchSpy.mockClear();
419419
vi.stubGlobal("fetch", fetchSpy);
420420
});
421421
afterEach(() => vi.unstubAllGlobals());
422422

423+
const renders = () => fetchSpy.mock.calls.map(([url]) => url);
423424
const settle = (
424425
view: ReturnType<typeof renderPreview>,
425426
steps: Array<"ready" | "missing" | "loaded">,
@@ -437,7 +438,10 @@ describe("NLEPreview", () => {
437438
it("is rendered for the next open when the live frame is ready first", () => {
438439
const view = renderPreview();
439440
settle(view, ["ready", "missing"]);
440-
expect(fetchSpy.mock.calls).toEqual([[renderUrl]]);
441+
expect(renders()).toEqual([renderUrl]);
442+
expect(fetchSpy.mock.calls[0]?.[1]?.signal, "rendered as scheduler work").toBeInstanceOf(
443+
AbortSignal,
444+
);
441445
expect(view.stage.querySelector('[data-testid="preview-poster"]')).toBeNull();
442446
view.cleanup();
443447
});
@@ -447,7 +451,7 @@ describe("NLEPreview", () => {
447451
settle(view, ["missing"]);
448452
expect(fetchSpy).not.toHaveBeenCalled();
449453
settle(view, ["ready", "ready"]);
450-
expect(fetchSpy.mock.calls).toEqual([[renderUrl]]);
454+
expect(renders()).toEqual([renderUrl]);
451455
view.cleanup();
452456
});
453457

@@ -458,7 +462,7 @@ describe("NLEPreview", () => {
458462
settle(view, ["ready"]);
459463
view.render();
460464
settle(view, ["ready", "missing"]);
461-
expect(fetchSpy.mock.calls).toEqual([[renderUrl]]);
465+
expect(renders()).toEqual([renderUrl]);
462466
view.cleanup();
463467
});
464468
});

‎packages/studio/src/components/nle/PreviewPoster.tsx‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { useCallback, useRef, useState } from "react";
22
import { buildProjectApiPath } from "../../utils/projectRouting";
3+
import {
4+
createThumbnailKey,
5+
thumbnailScheduler,
6+
type ThumbnailRequest,
7+
} from "../../player/lib/thumbnailScheduler";
38
import type { PreviewCompositionSize } from "../../utils/previewCompositionSize";
49

510
/** Frame 0 as the thumbnail route last rendered it; `cachedOnly` never starts a render. */
@@ -65,7 +70,7 @@ export function usePreviewPoster(
6570
const renderMissingPoster = useCallback(() => {
6671
if (missingForRef.current !== visit || liveReadyForRef.current !== visit) return;
6772
missingForRef.current = null;
68-
void fetch(previewPosterUrl(projectId, false)).catch(() => {});
73+
renderPosterForNextOpen(projectId);
6974
}, [visit, projectId]);
7075
const onLiveReadyToShowChange = useCallback(
7176
(ready: boolean) => {
@@ -91,6 +96,29 @@ export function usePreviewPoster(
9196
};
9297
}
9398

99+
/** Renders frame 0 under the thumbnail cap; the lease ends once the render settles. */
100+
export function renderPosterForNextOpen(projectId: string): void {
101+
const url = previewPosterUrl(projectId, false);
102+
const request: ThumbnailRequest = {
103+
key: createThumbnailKey({ kind: "poster-render", url }),
104+
projectId,
105+
sessionEpoch: 0,
106+
kind: "composition",
107+
priority: "overscan",
108+
discardWhenReleased: true,
109+
load: async (signal) => {
110+
await (await fetch(url, { signal })).arrayBuffer();
111+
return { value: { kind: "image", url, aspect: 16 / 9 }, weight: 0 };
112+
},
113+
};
114+
const settled = () => ["ready", "error"].includes(thumbnailScheduler.getSnapshot(request).status);
115+
let lease: { release(): void } | null = null;
116+
lease = thumbnailScheduler.acquire(request, () => {
117+
if (settled()) lease?.release();
118+
});
119+
if (settled()) lease.release();
120+
}
121+
94122
function useVisitNumber(key: string): number {
95123
const [seen, setSeen] = useState({ key, visit: 0 });
96124
if (seen.key === key) return seen.visit;

‎packages/studio/src/components/sidebar/CompositionsTab.drag.test.tsx‎

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,18 @@
11
// @vitest-environment happy-dom
22

33
import { act } from "react";
4-
import { createRoot, type Root } from "react-dom/client";
5-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
6-
import { usePlayerStore } from "../../player/store/playerStore";
4+
import { describe, expect, it, vi } from "vitest";
75
import { TIMELINE_COMPOSITION_MIME } from "../../utils/timelineCompositionDrop";
8-
import { CompositionsTab } from "./CompositionsTab";
9-
10-
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
11-
(
12-
window as unknown as { happyDOM: { settings: { disableIframePageLoading: boolean } } }
13-
).happyDOM.settings.disableIframePageLoading = true;
14-
15-
let root: Root | null = null;
16-
17-
beforeEach(() => {
18-
usePlayerStore.setState({ previewBooted: true });
19-
});
20-
21-
afterEach(() => {
22-
if (root) act(() => root?.unmount());
23-
root = null;
24-
document.body.innerHTML = "";
25-
usePlayerStore.setState({ thumbnailRevisions: {} });
26-
});
6+
import { mountCompositionsTab } from "./compositionsTabTestUtils";
277

288
function mount(onSelect = vi.fn(), onAddToTimeline = vi.fn()) {
29-
const host = document.createElement("div");
30-
document.body.append(host);
31-
root = createRoot(host);
32-
act(() => {
33-
root?.render(
34-
<CompositionsTab
35-
projectId="demo"
36-
compositions={["compositions/headline.html"]}
37-
activeComposition={null}
38-
onSelect={onSelect}
39-
onAddToTimeline={onAddToTimeline}
40-
/>,
41-
);
42-
});
9+
const host = mountCompositionsTab({ onSelect, onAddToTimeline });
4310
const card = host.querySelector<HTMLElement>('[draggable="true"]');
4411
if (!card) throw new Error("composition card did not render");
4512
return { host, card, onSelect, onAddToTimeline };
4613
}
4714

4815
describe("composition card drag", () => {
49-
it("uses a cached image instead of eagerly mounting a live preview iframe", () => {
50-
const { host } = mount();
51-
const thumbnail = host.querySelector<HTMLImageElement>('img[src*="/thumbnail/"]');
52-
expect(thumbnail).not.toBeNull();
53-
expect(new URL(thumbnail?.src ?? "").searchParams.get("t")).toBe("3.00");
54-
expect(host.querySelector("iframe")).toBeNull();
55-
});
56-
57-
it("shows a fallback when the cached thumbnail fails", () => {
58-
const { host } = mount();
59-
const thumbnail = host.querySelector<HTMLImageElement>('img[src*="/thumbnail/"]');
60-
if (!thumbnail) throw new Error("composition thumbnail did not render");
61-
62-
act(() => thumbnail.dispatchEvent(new Event("error")));
63-
64-
expect(host.textContent).toContain("Preview unavailable");
65-
expect(host.querySelector('img[src*="/thumbnail/"]')).toBeNull();
66-
});
67-
68-
it("retries a failed thumbnail at the next persisted content revision", () => {
69-
const { host } = mount();
70-
const thumbnail = host.querySelector<HTMLImageElement>('img[src*="/thumbnail/"]');
71-
if (!thumbnail) throw new Error("composition thumbnail did not render");
72-
act(() => thumbnail.dispatchEvent(new Event("error")));
73-
expect(host.textContent).toContain("Preview unavailable");
74-
75-
act(() => usePlayerStore.getState().bumpThumbnailRevisions(null));
76-
77-
const retry = host.querySelector<HTMLImageElement>('img[src*="/thumbnail/"]');
78-
expect(retry).not.toBeNull();
79-
expect(new URL(retry?.src ?? "").searchParams.get("revision")).toBe("1");
80-
});
81-
8216
it("mounts one live preview only after sustained hover and removes it on leave", () => {
8317
vi.useFakeTimers();
8418
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});

‎packages/studio/src/components/sidebar/CompositionsTab.rootBadge.test.tsx‎

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,11 @@
11
// @vitest-environment happy-dom
22

3-
import { act } from "react";
4-
import { createRoot, type Root } from "react-dom/client";
5-
import { afterEach, describe, expect, it, vi } from "vitest";
6-
import { usePlayerStore } from "../../player/store/playerStore";
3+
import { describe, expect, it } from "vitest";
74
import { resolveMasterCompositionPath } from "../../utils/studioUrlState";
8-
import { CompositionsTab } from "./CompositionsTab";
5+
import { mountCompositionsTab } from "./compositionsTabTestUtils";
96

10-
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
11-
(
12-
window as unknown as { happyDOM: { settings: { disableIframePageLoading: boolean } } }
13-
).happyDOM.settings.disableIframePageLoading = true;
14-
15-
let root: Root | null = null;
16-
17-
afterEach(() => {
18-
if (root) act(() => root?.unmount());
19-
root = null;
20-
document.body.innerHTML = "";
21-
usePlayerStore.setState({ thumbnailRevisions: {} });
22-
});
23-
24-
function mount(compositions: string[], masterCompositionPath: string | null) {
25-
const host = document.createElement("div");
26-
document.body.append(host);
27-
root = createRoot(host);
28-
act(() => {
29-
root?.render(
30-
<CompositionsTab
31-
projectId="demo"
32-
compositions={compositions}
33-
activeComposition={null}
34-
masterCompositionPath={masterCompositionPath}
35-
onSelect={vi.fn()}
36-
/>,
37-
);
38-
});
39-
return host;
40-
}
41-
42-
describe("CompositionsTab card thumbnails", () => {
43-
it("waits for the live preview to boot and keeps them through edit reloads", () => {
44-
usePlayerStore.getState().reset();
45-
const host = mount(["index.html"], "index.html");
46-
expect(host.querySelector("img")).toBeNull();
47-
48-
act(() => usePlayerStore.getState().markPreviewBooted());
49-
expect(host.querySelector("img")?.getAttribute("src")).toContain("/thumbnail/index.html");
50-
51-
act(() => usePlayerStore.getState().setTimelineReady(false));
52-
expect(host.querySelector("img")).not.toBeNull();
53-
});
54-
});
7+
const mount = (compositions: string[], masterCompositionPath: string | null) =>
8+
mountCompositionsTab({ compositions, masterCompositionPath });
559

5610
describe("CompositionsTab root badge", () => {
5711
it("marks the composition matching masterCompositionPath as root", () => {

0 commit comments

Comments
 (0)