Skip to content

Commit 303c8e9

Browse files
perf(studio): swap an edited scene into the live preview instead of reloading (#4499)
* perf(studio): swap an edited scene into the live preview instead of reloading On a preview reload, Studio fetches the rebuilt preview document and asks the preview to swap in only the scenes that changed. When the preview cannot or will not swap (it predates the swap, a full reload is already running, or the document changed outside its scenes), Studio does the full shadow reload as before. A document from an older reload is never swapped in. * fix(studio): re-sync selection and layers after a scene swap A swap replaces scene DOM without a load event, so selection, manual edits, the Layers panel and the edit callout kept pointing at the replaced nodes. They now listen for either a load or a swap. * fix(studio): drop a stale scene swap once the preview is replaced A swap still pending when the preview reloaded or switched composition could fall back to reloading the old one. Color grading and its compare view are also re-sent after a swap. * docs(studio): say exactly when the preview generation changes * refactor(studio): cut scene swap comments to what names and types do not say * fix(studio): fall back to a full reload when a scene swap download stalls * fix(studio): give up on a scene swap that stalls at any step, not only the download * fix(runtime): a scene swap refuses when an animation outside the scene moves its elements * test(runtime): pin the scene swap volume re-probe, host CSS variables and diagnostics-free swap * fix(runtime): a scene swap keeps a video its scene script animates instead of reloading it * fix(runtime): a scene swap stops every animation the old scene script started * fix(runtime): record scene script animations only on pages a scene swap can act on * docs(core): reword the scene animation recording comment * fix(runtime): watch media as the page parses only on pages a scene swap can act on * perf(runtime): a scene swap checks for outside tweens in one pass over the film's tweens * test(runtime): pin the single revert and the cleared animation record of a scene swap * test(studio): pin that a playing film keeps playing through a scene swap * fix(runtime): a scene swap rebuilds media when the edit changes the scene script * fix(runtime): a scene swap undoes the sets a scene script made * fix(runtime): a scene swap rechecks outside tweens and runs new scripts after every host * test(runtime): pin that a tween started during the caption wait leaves the page untouched * fix(runtime): a scene swap refuses when its script set a value on an element the swap keeps * docs(core): say the scene recording drops a finished tween that the timeline would keep when paused * fix(runtime): a scene swap refuses when two of its animations write to an element it keeps * fix(runtime): a scene swap resets kept media and refuses animations outside the scene * fix(runtime): a scene swap keeps a moved video in place and swaps tweens on plain objects * chore(core): regenerate the inlined position-edits script for the kept-video move reapply * fix(runtime): a scene swap checks the timeline registry as it is after the caption wait * fix(runtime): a scene swap refuses when a scene changed during the wait or a revert re-registers * fix(runtime): a scene swap stops every animation a revert chain registers and rechecks its hosts * fix(runtime): a scene swap refuses a moved host or a cancelled swap and stops at a shortened end * fix(studio): a scene swap that times out is cancelled before the reload starts * fix(runtime): a scene swap refuses when a scene leaves its place or the film during the wait * fix(studio): a newer edit cancels an older scene swap still waiting * fix(runtime): a scene swap checks only its host's place, so a later body append does not refuse it * fix(studio): a scene swap cancelled before it starts does not run * fix(runtime): a scene swap refuses when the film's scene layout changed during the wait * fix(runtime): a scene swap refuses when the order around a scene changed during the wait * test(runtime): pin that a scene swap checks the film root order when every scene is wrapped * fix(runtime): a scene swap gives kept media the mute, rate and loop a fresh load gives it * fix(runtime): a scene swap rebuilds media a script wrote attributes on, keeping the rest * fix(runtime): a scene swap keeps media carrying the runtime classes or reordered attributes * fix(runtime): a scene swap resets a kept video's volume as a load does, not through the volume probe
1 parent 1004b69 commit 303c8e9

12 files changed

Lines changed: 475 additions & 17 deletions

‎packages/studio/src/components/editor/LayersPanel.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { DomEditLayerItem } from "./domEditingTypes";
99
import { createRafThrottle, LayersPanel, sortLayersByZIndex } from "./LayersPanel";
1010
import { isLayerDraggable } from "./useLayerDrag";
1111
import { liveTime } from "../../player";
12+
import { sceneSwapFor } from "../../player/sceneSwap";
1213

1314
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
1415

@@ -292,4 +293,17 @@ describe("LayersPanel preview promotion", () => {
292293
});
293294
expect(collectedRootIds()).toEqual(["b"]);
294295
});
296+
it("recollects layers when edited scenes are swapped into the live document", async () => {
297+
const a = makeIframe("a");
298+
Object.defineProperty(a, "contentWindow", { value: { __hfSwapScenes: async () => {} } });
299+
const fetchSpy = vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response("<html></html>"));
300+
mocks.previewIframeRef.current = a;
301+
usePreviewIframeStore.setState({ iframe: a });
302+
act(() => root.render(createElement(LayersPanel)));
303+
mocks.collect.mockClear();
304+
305+
await act(() => sceneSwapFor(a)!("/preview", () => true));
306+
expect(collectedRootIds()).toEqual(["a"]);
307+
fetchSpy.mockRestore();
308+
});
295309
});

‎packages/studio/src/components/editor/LayersPanel.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useLivePreviewIframe } from "../../player/store/previewIframeStore";
2+
import { onPreviewContentReplaced } from "../../player/sceneSwap";
23
import { memo, useState, useCallback, useEffect, useRef } from "react";
34
import {
45
collectDomEditLayerItems,
@@ -164,8 +165,7 @@ export const LayersPanel = memo(function LayersPanel() {
164165
prevDocVersionRef.current += 1;
165166
collectLayers();
166167
};
167-
iframe.addEventListener("load", handleLoad);
168-
return () => iframe.removeEventListener("load", handleLoad);
168+
return onPreviewContentReplaced(iframe, handleLoad);
169169
}, [previewIframeRef, livePreviewIframe, collectLayers]);
170170

171171
useEffect(() => {

‎packages/studio/src/components/editor/TopologyLens.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useLivePreviewIframe } from "../../player/store/previewIframeStore";
2+
import { onPreviewContentReplaced } from "../../player/sceneSwap";
23
import {
34
useEffect,
45
useLayoutEffect,
@@ -119,8 +120,7 @@ export function TopologyLens({ iframeRef, activeCompositionPath }: TopologyLensP
119120
const iframe = iframeRef.current;
120121
if (!iframe || !callId) return;
121122
const dismiss = () => studioEditLifecycle.dismiss(callId);
122-
iframe.addEventListener("load", dismiss);
123-
return () => iframe.removeEventListener("load", dismiss);
123+
return onPreviewContentReplaced(iframe, dismiss);
124124
}, [callId, iframeRef, livePreviewIframe]);
125125

126126
useEffect(() => {

‎packages/studio/src/components/editor/useColorGradingController.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { normalizeHfColorGrading } from "@hyperframes/core/color-grading";
77
import { runtimeProtocolMetadata } from "@hyperframes/core/runtime/protocol";
88
import { usePreviewIframeStore } from "../../player/store/previewIframeStore";
99
import { useColorGradingController } from "./useColorGradingController";
10+
import { sceneSwapFor } from "../../player/sceneSwap";
1011
import type { DomEditSelection } from "./domEditing";
1112

1213
function brightPopGrading() {
@@ -390,6 +391,29 @@ describe("useColorGradingController", () => {
390391
vi.useRealTimers();
391392
});
392393

394+
it("re-sends the grade to the preview after edited scenes are swapped in", async () => {
395+
vi.useFakeTimers();
396+
const { contentWindow, iframe } = createPreviewFrame();
397+
(contentWindow as unknown as { __hfSwapScenes: () => Promise<void> }).__hfSwapScenes =
398+
async () => {};
399+
vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response("<html></html>"));
400+
const postMessage = vi.spyOn(contentWindow, "postMessage");
401+
const { root, getState } = renderHook(vi.fn(), makeElement(), { current: iframe });
402+
act(() => getState().commitColorGrading(brightPopGrading()));
403+
await act(async () => {
404+
vi.advanceTimersByTime(100);
405+
await vi.runAllTimersAsync();
406+
});
407+
postMessage.mockClear();
408+
409+
await act(() => sceneSwapFor(iframe)!("/preview", () => true));
410+
const replayed = colorGradingMessages(postMessage.mock.calls);
411+
expect(replayed.at(-1)?.grading).toMatchObject({ preset: "bright-pop" });
412+
413+
act(() => root.unmount());
414+
vi.useRealTimers();
415+
});
416+
393417
it("animates only the selected video card without starting the project player", async () => {
394418
vi.useFakeTimers();
395419
const { contentWindow, iframe } = createPreviewFrame();

‎packages/studio/src/components/editor/useColorGradingController.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { mediaMetadataUrl } from "../../utils/studioHelpers";
22
import { useLivePreviewIframe } from "../../player/store/previewIframeStore";
3+
import { onPreviewContentReplaced } from "../../player/sceneSwap";
34
import { useCallback, useEffect, useMemo, useRef, useState, type RefObject } from "react";
45
import {
56
HF_COLOR_GRADING_ATTR,
@@ -445,11 +446,11 @@ export function useColorGradingController({
445446
if (!acceptStudioRuntimeMessage(data)) return;
446447
refreshAndReplay();
447448
};
448-
iframe.addEventListener("load", refreshAndReplay);
449449
window.addEventListener("message", onMessage);
450450
const timer = window.setTimeout(refreshAndReplay, 80);
451+
const stopReplay = onPreviewContentReplaced(iframe, refreshAndReplay);
451452
return () => {
452-
iframe.removeEventListener("load", refreshAndReplay);
453+
stopReplay();
453454
window.removeEventListener("message", onMessage);
454455
window.clearTimeout(timer);
455456
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// @vitest-environment happy-dom
2+
3+
import { createElement, act } from "react";
4+
import { createRoot } from "react-dom/client";
5+
import { describe, expect, it, vi } from "vitest";
6+
import { useDomEditPreviewSync } from "./useDomEditPreviewSync";
7+
import { sceneSwapFor } from "../player/sceneSwap";
8+
9+
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
10+
11+
describe("useDomEditPreviewSync", () => {
12+
it("re-syncs the preview when edited scenes are swapped in, and not when the swap is refused", async () => {
13+
const iframe = document.createElement("iframe");
14+
let refuse = false;
15+
Object.defineProperty(iframe, "contentWindow", {
16+
value: {
17+
__hfSwapScenes: async () => {
18+
if (refuse) throw new Error("the film changed outside its scenes");
19+
},
20+
},
21+
});
22+
const fetchSpy = vi.spyOn(globalThis, "fetch").mockImplementation(async () => new Response(""));
23+
const applyManualEdits = vi.fn(async () => {});
24+
const refreshVersion = vi.fn();
25+
function Harness() {
26+
useDomEditPreviewSync({
27+
previewIframe: iframe,
28+
activeCompPath: null,
29+
captionEditMode: false,
30+
domEditSelectionRef: { current: null },
31+
domEditGroupSelectionsRef: { current: [] },
32+
domEditSelection: null,
33+
refreshDomEditGroupSelectionsFromPreview: async () => {},
34+
applyDomSelection: () => {},
35+
buildDomSelectionFromTarget: async () => null,
36+
refreshPreviewDocumentVersion: refreshVersion,
37+
syncPreviewHotkeys: () => {},
38+
applyStudioManualEditsToPreviewRef: { current: applyManualEdits },
39+
});
40+
return null;
41+
}
42+
const root = createRoot(document.createElement("div"));
43+
act(() => root.render(createElement(Harness)));
44+
applyManualEdits.mockClear();
45+
refreshVersion.mockClear();
46+
47+
refuse = true;
48+
await act(() => sceneSwapFor(iframe)!("/preview", () => true).catch(() => {}));
49+
expect(refreshVersion).not.toHaveBeenCalled();
50+
51+
refuse = false;
52+
await act(() => sceneSwapFor(iframe)!("/preview", () => true));
53+
expect(applyManualEdits).toHaveBeenCalledWith(iframe);
54+
expect(refreshVersion).toHaveBeenCalledTimes(1);
55+
act(() => root.unmount());
56+
fetchSpy.mockRestore();
57+
});
58+
});

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { reapplyPositionEditsAfterSeek } from "../components/editor/manualEdits"
99
import { useDockLayoutStore } from "../components/dock/dockLayoutStore";
1010
import type { PatchTarget } from "../utils/sourcePatcher";
1111
import { logSelect } from "../utils/selectDebug";
12+
import { onPreviewContentReplaced } from "../player/sceneSwap";
1213

1314
interface UseDomEditPreviewSyncParams {
1415
previewIframe: HTMLIFrameElement | null;
@@ -113,10 +114,7 @@ export function useDomEditPreviewSync({
113114
refreshPreviewDocumentVersion();
114115
};
115116

116-
previewIframe.addEventListener("load", handleLoad);
117-
return () => {
118-
previewIframe.removeEventListener("load", handleLoad);
119-
};
117+
return onPreviewContentReplaced(previewIframe, handleLoad);
120118
}, [
121119
activeCompPath,
122120
applyDomSelection,

‎packages/studio/src/player/hooks/useShadowPreviewReload.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export function useShadowPreviewReload({
140140

141141
const getShadowAdapter = useCallback(() => getAdapter(shadowIframeRef.current), [getAdapter]);
142142
const isCurrentShadow = useCallback((gen?: number) => gen === shadowGenRef.current, []);
143+
const previewGeneration = useCallback(() => shadowGenRef.current, []);
143144
const markAdapterReady = useCallback(
144145
(iframe: HTMLIFrameElement | null, gen: number | undefined, commit: () => void) => {
145146
if (gen == null || gen !== shadowGenRef.current) return;
@@ -261,5 +262,6 @@ export function useShadowPreviewReload({
261262
setShadowIframeNode,
262263
beginShadowReload,
263264
resetPreviewSlots,
265+
previewGeneration,
264266
};
265267
}

0 commit comments

Comments
 (0)