Skip to content

Commit fbfffb1

Browse files
fix(studio): release retained preview resources (#2924)
1 parent a3c8f89 commit fbfffb1

6 files changed

Lines changed: 308 additions & 92 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// @vitest-environment jsdom
2+
3+
import React, { act } from "react";
4+
import { afterEach, describe, expect, it, vi } from "vitest";
5+
import { mountReactHarness } from "./domSelectionTestHarness";
6+
import { useGestureRecording } from "./useGestureRecording";
7+
8+
Reflect.set(globalThis, "IS_REACT_ACT_ENVIRONMENT", true);
9+
10+
afterEach(() => {
11+
vi.unstubAllGlobals();
12+
document.body.replaceChildren();
13+
});
14+
15+
describe("useGestureRecording", () => {
16+
it("releases the preview runtime when unmounted during a recording", () => {
17+
const cancelAnimationFrame = vi.fn();
18+
vi.stubGlobal(
19+
"requestAnimationFrame",
20+
vi.fn(() => 17),
21+
);
22+
vi.stubGlobal("cancelAnimationFrame", cancelAnimationFrame);
23+
24+
const iframe = document.createElement("iframe");
25+
document.body.append(iframe);
26+
const previewDocument = iframe.contentDocument!;
27+
const element = previewDocument.createElement("div");
28+
element.id = "card";
29+
element.style.visibility = "hidden";
30+
element.style.setProperty("translate", "10px 20px");
31+
element.style.setProperty("--hf-studio-offset-x", "12px");
32+
element.style.setProperty("--hf-studio-offset-y", "-8px");
33+
previewDocument.body.append(element);
34+
35+
const set = vi.fn();
36+
const seek = vi.fn();
37+
Reflect.set(iframe.contentWindow!, "gsap", {
38+
getProperty: vi.fn(() => 0),
39+
set,
40+
});
41+
Reflect.set(iframe.contentWindow!, "__timelines", {
42+
main: { duration: () => 5, seek },
43+
});
44+
Reflect.set(iframe.contentWindow!, "__player", { getTime: () => 1 });
45+
46+
let recording: ReturnType<typeof useGestureRecording> | null = null;
47+
function Harness() {
48+
recording = useGestureRecording();
49+
return null;
50+
}
51+
const root = mountReactHarness(<Harness />);
52+
53+
act(() => recording?.startRecording(element, iframe, 4));
54+
expect(element.style.getPropertyValue("--hf-studio-offset-x")).toBe("0px");
55+
expect(element.style.getPropertyValue("--hf-studio-offset-y")).toBe("0px");
56+
57+
act(() => root.unmount());
58+
59+
expect(cancelAnimationFrame).toHaveBeenCalledWith(17);
60+
expect(set).toHaveBeenCalledWith("#card", {
61+
clearProps: "x,y,scale,scaleX,scaleY,rotation,rotationX,rotationY,opacity,z",
62+
});
63+
expect(element.style.visibility).toBe("hidden");
64+
expect(element.style.getPropertyValue("translate")).toBe("10px 20px");
65+
expect(element.style.getPropertyValue("--hf-studio-offset-x")).toBe("12px");
66+
expect(element.style.getPropertyValue("--hf-studio-offset-y")).toBe("-8px");
67+
});
68+
});

‎packages/studio/src/hooks/useGestureRecording.ts‎

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ interface BasePosition {
2828
}
2929

3030
interface GsapRuntime {
31-
seek: (t: number) => void;
32-
set: (target: string, vars: Record<string, number | string>) => void;
31+
timeline: { seek: (t: number) => void };
32+
gsap: { set: (target: string, vars: Record<string, number | string>) => void };
3333
selector: string;
3434
element: HTMLElement;
3535
startTime: number;
@@ -100,8 +100,8 @@ function connectGsapRuntime(
100100
if (win?.gsap?.set && tl?.seek && selector) {
101101
const tlDuration = tl.duration();
102102
return {
103-
seek: tl.seek.bind(tl),
104-
set: win.gsap.set.bind(win.gsap),
103+
timeline: tl,
104+
gsap: win.gsap,
105105
selector,
106106
element,
107107
startTime: win.__player?.getTime() ?? 0,
@@ -123,9 +123,9 @@ function applyRuntimePreview(
123123
properties: Record<string, number>,
124124
): void {
125125
const seekTime = Math.min(runtime.startTime + time, runtime.maxSeekTime);
126-
runtime.seek(seekTime);
126+
runtime.timeline.seek(seekTime);
127127
runtime.element.style.setProperty("translate", "none");
128-
runtime.set(runtime.selector, { ...properties });
128+
runtime.gsap.set(runtime.selector, { ...properties });
129129
runtime.element.style.visibility = "visible";
130130
liveTime.notify(seekTime);
131131
usePlayerStore.getState().setCurrentTime(seekTime);
@@ -237,6 +237,26 @@ function createRecordingRefs(): RecordingRefs {
237237
};
238238
}
239239

240+
function releaseRuntimePreview(r: RecordingRefs): void {
241+
const runtime = r.runtime;
242+
if (!runtime) return;
243+
const { element, savedVisibility, savedTranslate } = runtime;
244+
element.style.visibility = savedVisibility;
245+
element.style.setProperty("translate", savedTranslate || "");
246+
try {
247+
runtime.gsap.set(runtime.selector, {
248+
clearProps: "x,y,scale,scaleX,scaleY,rotation,rotationX,rotationY,opacity,z",
249+
});
250+
} catch {
251+
/* runtime gone */
252+
}
253+
if (r.cssVarOffset.x || r.cssVarOffset.y) {
254+
element.style.setProperty("--hf-studio-offset-x", `${r.cssVarOffset.x}px`);
255+
element.style.setProperty("--hf-studio-offset-y", `${r.cssVarOffset.y}px`);
256+
}
257+
r.runtime = null;
258+
}
259+
240260
// ---------------------------------------------------------------------------
241261
// Hook
242262
// ---------------------------------------------------------------------------
@@ -259,9 +279,10 @@ export function useGestureRecording() {
259279
useEffect(() => {
260280
const r = refs.current;
261281
return () => {
282+
isRecordingRef.current = false;
283+
releaseRuntimePreview(r);
262284
r.cleanup?.();
263285
r.cleanup = null;
264-
isRecordingRef.current = false;
265286
};
266287
}, []);
267288

@@ -290,15 +311,15 @@ export function useGestureRecording() {
290311
// this scale to convert them to the iframe's composition pixels.
291312
r.scale = computeIframeScale(iframeEl);
292313

293-
// Now clear the optimistic path offset (already folded into baseX/baseY).
294-
if (base.cssOffX || base.cssOffY) {
295-
element.style.setProperty("--hf-studio-offset-x", "0px");
296-
element.style.setProperty("--hf-studio-offset-y", "0px");
297-
}
298-
299314
// --- Phase 3: Connect to the iframe GSAP runtime ---
300315
const selector = element.id ? `#${element.id}` : null;
301316
r.runtime = connectGsapRuntime(element, iframeEl, selector, elementEndTime);
317+
// Clear the optimistic path offset only while a live runtime owns the
318+
// preview. releaseRuntimePreview restores it on every exit path.
319+
if (r.runtime && (base.cssOffX || base.cssOffY)) {
320+
element.style.setProperty("--hf-studio-offset-x", "0px");
321+
element.style.setProperty("--hf-studio-offset-y", "0px");
322+
}
302323

303324
// --- Phase 5: Attach event listeners ---
304325
const handlePointerMove = (e: PointerEvent) => {
@@ -378,7 +399,7 @@ export function useGestureRecording() {
378399
} catch {
379400
// Preview failed — disable it for the rest of the gesture (recording
380401
// continues). `r.runtime` is nulled so we don't retry on every frame.
381-
r.runtime = null;
402+
releaseRuntimePreview(r);
382403
}
383404
}
384405

@@ -407,31 +428,7 @@ export function useGestureRecording() {
407428
if (!isRecordingRef.current) return [];
408429
isRecordingRef.current = false;
409430
const r = refs.current;
410-
if (r.runtime) {
411-
const { element: el, savedVisibility, savedTranslate } = r.runtime;
412-
el.style.visibility = savedVisibility;
413-
el.style.setProperty("translate", savedTranslate || "");
414-
// Drop the gesture's inline gsap transform before re-applying the path
415-
// offset below, so the two don't briefly stack (the recorded keyframes
416-
// already encode the full position, offset included). On commit the
417-
// re-seek lands on the gesture's first keyframe; on cancel this leaves the
418-
// element at its pre-recording position.
419-
try {
420-
r.runtime.set(r.runtime.selector, {
421-
clearProps: "x,y,scale,scaleX,scaleY,rotation,rotationX,rotationY,opacity,z",
422-
});
423-
} catch {
424-
/* runtime gone */
425-
}
426-
}
427-
if (r.cssVarOffset.x || r.cssVarOffset.y) {
428-
const el = r.runtime?.element;
429-
if (el) {
430-
el.style.setProperty("--hf-studio-offset-x", `${r.cssVarOffset.x}px`);
431-
el.style.setProperty("--hf-studio-offset-y", `${r.cssVarOffset.y}px`);
432-
}
433-
}
434-
r.runtime = null;
431+
releaseRuntimePreview(r);
435432
r.cleanup?.();
436433
r.cleanup = null;
437434
const frozen = r.samples.slice();

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

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,54 @@
1-
import { describe, expect, it } from "vitest";
2-
import { buildCompositionThumbnailUrl } from "./CompositionThumbnail";
1+
// @vitest-environment happy-dom
2+
3+
import React, { act } from "react";
4+
import { createRoot, type Root } from "react-dom/client";
5+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
6+
import { buildCompositionThumbnailUrl, CompositionThumbnail } from "./CompositionThumbnail";
7+
8+
Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
9+
configurable: true,
10+
value: true,
11+
});
12+
13+
class MockResizeObserver {
14+
observe() {}
15+
disconnect() {}
16+
unobserve() {}
17+
}
18+
19+
class MockImage {
20+
static instances: MockImage[] = [];
21+
onload: (() => void) | null = null;
22+
onerror: (() => void) | null = null;
23+
naturalWidth = 0;
24+
naturalHeight = 0;
25+
src = "";
26+
27+
constructor() {
28+
MockImage.instances.push(this);
29+
}
30+
}
31+
32+
const originalResizeObserver = globalThis.ResizeObserver;
33+
const originalImage = globalThis.Image;
34+
let host: HTMLDivElement;
35+
let root: Root | null = null;
36+
37+
beforeEach(() => {
38+
globalThis.ResizeObserver = MockResizeObserver as unknown as typeof ResizeObserver;
39+
globalThis.Image = MockImage as unknown as typeof Image;
40+
MockImage.instances = [];
41+
host = document.createElement("div");
42+
document.body.append(host);
43+
});
44+
45+
afterEach(() => {
46+
act(() => root?.unmount());
47+
root = null;
48+
globalThis.ResizeObserver = originalResizeObserver;
49+
globalThis.Image = originalImage;
50+
document.body.replaceChildren();
51+
});
352

453
describe("buildCompositionThumbnailUrl", () => {
554
it("includes selector and occurrence index for precise element thumbnails", () => {
@@ -17,3 +66,48 @@ describe("buildCompositionThumbnailUrl", () => {
1766
);
1867
});
1968
});
69+
70+
describe("CompositionThumbnail", () => {
71+
function renderThumbnail(): MockImage {
72+
root = createRoot(host);
73+
act(() => {
74+
root!.render(
75+
React.createElement(CompositionThumbnail, {
76+
previewUrl: "/api/projects/demo/preview",
77+
label: "",
78+
labelColor: "#fff",
79+
}),
80+
);
81+
});
82+
const probe = MockImage.instances[0];
83+
if (!probe) throw new Error("Expected an image probe");
84+
return probe;
85+
}
86+
87+
it("renders visible tiles after the off-DOM probe loads", () => {
88+
const probe = renderThumbnail();
89+
90+
act(() => {
91+
probe.naturalWidth = 1920;
92+
probe.naturalHeight = 1080;
93+
probe.onload?.();
94+
});
95+
96+
const tiles = [...host.querySelectorAll("img")];
97+
expect(tiles.length).toBeGreaterThan(0);
98+
expect(tiles.every((tile) => !tile.classList.contains("hidden"))).toBe(true);
99+
});
100+
101+
it("aborts its off-DOM image probe when unmounted", () => {
102+
const probe = renderThumbnail();
103+
expect(host.querySelector("img")).toBeNull();
104+
expect(probe.src).toContain("/api/projects/demo/thumbnail/index.html");
105+
106+
act(() => root?.unmount());
107+
root = null;
108+
109+
expect(probe.onload).toBeNull();
110+
expect(probe.onerror).toBeNull();
111+
expect(probe.src).toBe("");
112+
});
113+
});

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

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { memo, useCallback, useState, useRef } from "react";
1+
import { memo, useCallback, useEffect, useState, useRef } from "react";
22
import { useMountEffect } from "../../hooks/useMountEffect";
33

44
interface CompositionThumbnailProps {
@@ -88,26 +88,39 @@ export const CompositionThumbnail = memo(function CompositionThumbnail({
8888
selectorIndex,
8989
origin: window.location.origin,
9090
});
91+
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]);
118+
91119
const frameW = Math.max(48, Math.round(CLIP_HEIGHT * aspect));
92120
const frameCount = containerWidth > 0 ? Math.max(1, Math.ceil(containerWidth / frameW)) : 1;
93121

94122
return (
95123
<div ref={setContainerRef} className="absolute inset-0 overflow-hidden">
96-
<img
97-
src={url}
98-
alt=""
99-
draggable={false}
100-
loading="eager"
101-
onLoad={(e) => {
102-
const img = e.currentTarget;
103-
if (img.naturalWidth > 0 && img.naturalHeight > 0) {
104-
setAspect(img.naturalWidth / img.naturalHeight);
105-
}
106-
setLoaded(true);
107-
}}
108-
className="hidden"
109-
/>
110-
111124
{loaded && (
112125
<div
113126
className="absolute inset-0 flex"

0 commit comments

Comments
 (0)