Skip to content

Commit 5ae9a8a

Browse files
Codexmiguel-heygen
authored andcommitted
perf(studio): virtualize timeline thumbnail media
1 parent 9cd0e30 commit 5ae9a8a

8 files changed

Lines changed: 370 additions & 443 deletions

File tree

‎packages/studio/src/player/components/CompositionThumbnail.test.ts‎

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import React, { act } from "react";
44
import { createRoot, type Root } from "react-dom/client";
5-
import { afterEach, beforeEach, describe, expect, it } from "vitest";
5+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
6+
import { thumbnailScheduler } from "../lib/thumbnailScheduler";
67
import { buildCompositionThumbnailUrl, CompositionThumbnail } from "./CompositionThumbnail";
78

89
Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
@@ -31,12 +32,18 @@ class MockImage {
3132

3233
const originalResizeObserver = globalThis.ResizeObserver;
3334
const originalImage = globalThis.Image;
35+
const originalFetch = globalThis.fetch;
36+
const originalCreateObjectURL = URL.createObjectURL;
37+
const originalRevokeObjectURL = URL.revokeObjectURL;
3438
let host: HTMLDivElement;
3539
let root: Root | null = null;
3640

3741
beforeEach(() => {
3842
globalThis.ResizeObserver = MockResizeObserver as unknown as typeof ResizeObserver;
3943
globalThis.Image = MockImage as unknown as typeof Image;
44+
globalThis.fetch = vi.fn(async () => new Response(new Blob(["thumbnail"]), { status: 200 }));
45+
URL.createObjectURL = vi.fn(() => "blob:composition-thumbnail");
46+
URL.revokeObjectURL = vi.fn();
4047
MockImage.instances = [];
4148
host = document.createElement("div");
4249
document.body.append(host);
@@ -45,8 +52,12 @@ beforeEach(() => {
4552
afterEach(() => {
4653
act(() => root?.unmount());
4754
root = null;
55+
thumbnailScheduler.invalidateProject("/api/projects/demo/preview");
4856
globalThis.ResizeObserver = originalResizeObserver;
4957
globalThis.Image = originalImage;
58+
globalThis.fetch = originalFetch;
59+
URL.createObjectURL = originalCreateObjectURL;
60+
URL.revokeObjectURL = originalRevokeObjectURL;
5061
document.body.replaceChildren();
5162
});
5263

@@ -68,46 +79,58 @@ describe("buildCompositionThumbnailUrl", () => {
6879
});
6980

7081
describe("CompositionThumbnail", () => {
71-
function renderThumbnail(): MockImage {
82+
async function renderThumbnail(): Promise<MockImage> {
7283
root = createRoot(host);
73-
act(() => {
84+
await act(async () => {
7485
root!.render(
7586
React.createElement(CompositionThumbnail, {
7687
previewUrl: "/api/projects/demo/preview",
7788
label: "",
7889
labelColor: "#fff",
7990
}),
8091
);
92+
await new Promise((resolve) => setTimeout(resolve, 0));
8193
});
8294
const probe = MockImage.instances[0];
8395
if (!probe) throw new Error("Expected an image probe");
8496
return probe;
8597
}
8698

87-
it("renders visible tiles after the off-DOM probe loads", () => {
88-
const probe = renderThumbnail();
99+
it("renders visible tiles after the scheduled off-DOM probe loads", async () => {
100+
const probe = await renderThumbnail();
89101

90-
act(() => {
102+
expect(globalThis.fetch).toHaveBeenCalledWith(
103+
expect.stringContaining("/api/projects/demo/thumbnail/index.html"),
104+
expect.objectContaining({ signal: expect.any(AbortSignal) }),
105+
);
106+
expect(probe.src).toBe("blob:composition-thumbnail");
107+
108+
await act(async () => {
91109
probe.naturalWidth = 1920;
92110
probe.naturalHeight = 1080;
93111
probe.onload?.();
112+
await new Promise((resolve) => setTimeout(resolve, 0));
94113
});
95114

96115
const tiles = [...host.querySelectorAll("img")];
97116
expect(tiles.length).toBeGreaterThan(0);
98117
expect(tiles.every((tile) => !tile.classList.contains("hidden"))).toBe(true);
99118
});
100119

101-
it("aborts its off-DOM image probe when unmounted", () => {
102-
const probe = renderThumbnail();
120+
it("aborts its scheduled off-DOM image probe when unmounted", async () => {
121+
const probe = await renderThumbnail();
103122
expect(host.querySelector("img")).toBeNull();
104-
expect(probe.src).toContain("/api/projects/demo/thumbnail/index.html");
123+
expect(probe.src).toBe("blob:composition-thumbnail");
105124

106-
act(() => root?.unmount());
125+
await act(async () => {
126+
root?.unmount();
127+
await new Promise((resolve) => setTimeout(resolve, 0));
128+
});
107129
root = null;
108130

109131
expect(probe.onload).toBeNull();
110132
expect(probe.onerror).toBeNull();
111133
expect(probe.src).toBe("");
134+
expect(URL.revokeObjectURL).toHaveBeenCalledWith("blob:composition-thumbnail");
112135
});
113136
});

‎packages/studio/src/player/components/CompositionThumbnail.tsx‎

Lines changed: 78 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import { memo, useCallback, useEffect, useState, useRef } from "react";
1+
import { memo, useCallback, useMemo, useRef, useState } from "react";
22
import { useMountEffect } from "../../hooks/useMountEffect";
3+
import { useThumbnailLease } from "../../hooks/useThumbnailLease";
4+
import { createThumbnailKey, type ThumbnailPriority } from "../lib/thumbnailScheduler";
5+
import { TIMELINE_VIEWPORT_BUDGETS } from "../lib/timelineViewportBudgets";
6+
import { computeThumbnailStrip, probeImageAspect } from "./thumbnailUtils";
37

48
interface CompositionThumbnailProps {
59
previewUrl: string;
@@ -11,6 +15,10 @@ interface CompositionThumbnailProps {
1115
duration?: number;
1216
width?: number;
1317
height?: number;
18+
projectId?: string;
19+
sessionEpoch?: number;
20+
priority?: ThumbnailPriority;
21+
rich?: boolean;
1422
}
1523

1624
const CLIP_HEIGHT = 66;
@@ -34,9 +42,8 @@ export function buildCompositionThumbnailUrl({
3442
const thumbnailBase = previewUrl
3543
.replace("/preview/comp/", "/thumbnail/")
3644
.replace(/\/preview$/, "/thumbnail/index.html");
37-
const midTime = seekTime + duration / 2;
3845
const thumbnailUrl = new URL(thumbnailBase, origin);
39-
thumbnailUrl.searchParams.set("t", midTime.toFixed(2));
46+
thumbnailUrl.searchParams.set("t", (seekTime + duration / 2).toFixed(2));
4047
thumbnailUrl.searchParams.set("v", THUMBNAIL_URL_VERSION);
4148
if (selector) {
4249
thumbnailUrl.searchParams.set("selector", selector);
@@ -47,6 +54,29 @@ export function buildCompositionThumbnailUrl({
4754
return thumbnailUrl.toString();
4855
}
4956

57+
async function loadCompositionImage(url: string, signal: AbortSignal) {
58+
const response = await fetch(url, { signal });
59+
if (!response.ok) throw new Error(`Composition thumbnail failed (${response.status})`);
60+
const blob = await response.blob();
61+
if (signal.aborted) throw new DOMException("Aborted", "AbortError");
62+
const objectUrl = URL.createObjectURL(blob);
63+
try {
64+
const aspect = await probeImageAspect(objectUrl, signal);
65+
return {
66+
value: { kind: "image" as const, url: objectUrl, aspect },
67+
weight:
68+
TIMELINE_VIEWPORT_BUDGETS.posterMaxPhysicalWidth *
69+
TIMELINE_VIEWPORT_BUDGETS.posterMaxPhysicalHeight *
70+
4,
71+
dispose: () => URL.revokeObjectURL(objectUrl),
72+
};
73+
} catch (error) {
74+
URL.revokeObjectURL(objectUrl);
75+
throw error;
76+
}
77+
}
78+
79+
/** Server-rendered composition poster, deduplicated and budgeted by project/session. */
5080
export const CompositionThumbnail = memo(function CompositionThumbnail({
5181
previewUrl,
5282
label,
@@ -55,31 +85,12 @@ export const CompositionThumbnail = memo(function CompositionThumbnail({
5585
selectorIndex,
5686
seekTime = 2,
5787
duration = 5,
88+
projectId = previewUrl,
89+
sessionEpoch = 0,
90+
priority = "visible",
5891
}: CompositionThumbnailProps) {
5992
const [containerWidth, setContainerWidth] = useState(0);
60-
const [loaded, setLoaded] = useState(false);
61-
const [aspect, setAspect] = useState(16 / 9);
62-
const roRef = useRef<ResizeObserver | null>(null);
63-
64-
const setContainerRef = useCallback((el: HTMLDivElement | null) => {
65-
roRef.current?.disconnect();
66-
if (!el) return;
67-
68-
const measured = el.parentElement?.clientWidth || el.clientWidth;
69-
// fallow-ignore-next-line code-duplication
70-
setContainerWidth(measured);
71-
72-
const target = el.parentElement || el;
73-
roRef.current = new ResizeObserver(([entry]) => {
74-
setContainerWidth(entry.contentRect.width);
75-
});
76-
roRef.current.observe(target);
77-
}, []);
78-
79-
useMountEffect(() => () => {
80-
roRef.current?.disconnect();
81-
});
82-
93+
const observerRef = useRef<ResizeObserver | null>(null);
8394
const url = buildCompositionThumbnailUrl({
8495
previewUrl,
8596
seekTime,
@@ -88,52 +99,56 @@ export const CompositionThumbnail = memo(function CompositionThumbnail({
8899
selectorIndex,
89100
origin: window.location.origin,
90101
});
102+
const request = useMemo(
103+
() => ({
104+
key: createThumbnailKey({ kind: "composition", url }),
105+
projectId,
106+
sessionEpoch,
107+
kind: "composition" as const,
108+
priority,
109+
rich: true,
110+
load: (signal: AbortSignal) => loadCompositionImage(url, signal),
111+
}),
112+
[priority, projectId, sessionEpoch, url],
113+
);
114+
const snapshot = useThumbnailLease(request);
115+
const value =
116+
snapshot.status === "ready" && snapshot.value.kind === "image" ? snapshot.value : null;
117+
const { frameW, frameCount } = computeThumbnailStrip(
118+
containerWidth,
119+
value?.aspect ?? 16 / 9,
120+
CLIP_HEIGHT,
121+
48,
122+
);
91123

92-
// Probe outside React's DOM and explicitly abort on URL changes/unmount. A
93-
// hidden React <img> keeps its Fiber reachable from Blink's pending-activity
94-
// queue while a request is unresolved.
95-
// eslint-disable-next-line no-restricted-syntax
96-
useEffect(() => {
97-
let cancelled = false;
98-
setLoaded(false);
99-
const probe = new Image();
100-
probe.onload = () => {
101-
if (cancelled) return;
102-
if (probe.naturalWidth > 0 && probe.naturalHeight > 0) {
103-
setAspect(probe.naturalWidth / probe.naturalHeight);
104-
}
105-
setLoaded(true);
106-
};
107-
probe.onerror = () => {
108-
if (!cancelled) setLoaded(false);
109-
};
110-
probe.src = url;
111-
return () => {
112-
cancelled = true;
113-
probe.onload = null;
114-
probe.onerror = null;
115-
probe.src = "";
116-
};
117-
}, [url]);
124+
const setContainerRef = useCallback((element: HTMLDivElement | null) => {
125+
observerRef.current?.disconnect();
126+
if (!element) return;
127+
const target = element.parentElement ?? element;
128+
setContainerWidth(target.clientWidth);
129+
observerRef.current = new ResizeObserver(([entry]) =>
130+
setContainerWidth(entry.contentRect.width),
131+
);
132+
observerRef.current.observe(target);
133+
}, []);
118134

119-
const frameW = Math.max(48, Math.round(CLIP_HEIGHT * aspect));
120-
const frameCount = containerWidth > 0 ? Math.max(1, Math.ceil(containerWidth / frameW)) : 1;
135+
useMountEffect(() => () => observerRef.current?.disconnect());
121136

122137
return (
123138
<div ref={setContainerRef} className="absolute inset-0 overflow-hidden">
124-
{loaded && (
139+
{value && (
125140
<div
126141
className="absolute inset-0 flex"
127142
style={{ animation: "hf-thumb-fade 200ms ease-out", mixBlendMode: "lighten" }}
128143
>
129-
{Array.from({ length: frameCount }).map((_, i) => (
144+
{Array.from({ length: frameCount }, (_, index) => (
130145
<div
131-
key={i}
146+
key={index}
132147
className="relative h-full flex-shrink-0 overflow-hidden"
133148
style={{ width: frameW }}
134149
>
135150
<img
136-
src={url}
151+
src={value.url}
137152
alt=""
138153
draggable={false}
139154
className="absolute inset-0 h-full w-full object-cover"
@@ -143,14 +158,16 @@ export const CompositionThumbnail = memo(function CompositionThumbnail({
143158
))}
144159
</div>
145160
)}
146-
161+
{snapshot.status === "loading" && (
162+
<div className="absolute inset-0 animate-pulse bg-white/[0.035]" />
163+
)}
147164
{label && (
148-
<div className="absolute left-3 top-0 bottom-0 flex items-center" style={{ zIndex: 10 }}>
165+
<div className="absolute inset-y-0 left-3 z-10 flex items-center">
149166
<span
150167
className="block max-w-full truncate text-[10px] font-semibold leading-none"
151168
style={{
152169
color: labelColor,
153-
textShadow: loaded ? "0 1px 4px rgba(0,0,0,0.9), 0 0 8px rgba(0,0,0,0.6)" : "none",
170+
textShadow: value ? "0 1px 4px rgba(0,0,0,0.9), 0 0 8px rgba(0,0,0,0.6)" : "none",
154171
}}
155172
>
156173
{label}

0 commit comments

Comments
 (0)