mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
perf(studio): hold Studio's media reads until the preview's assets settle
This commit is contained in:
@@ -231,6 +231,7 @@ export const Player = forwardRef<HTMLIFrameElement, PlayerProps>(
|
||||
setPreviewError(null);
|
||||
setCompositionLoading(false);
|
||||
};
|
||||
const handleAssetsReady = () => usePlayerStore.getState().markPreviewAssetsSettled();
|
||||
const handlePainted = () => {
|
||||
setPainted(true);
|
||||
onPainted?.({ iframe, startedAt: loadStartedAtRef.current, loadId: loadIdRef.current });
|
||||
@@ -307,6 +308,7 @@ export const Player = forwardRef<HTMLIFrameElement, PlayerProps>(
|
||||
player.addEventListener("shadertransitionstate", handleShaderTransitionState);
|
||||
player.addEventListener("ready", handleReady);
|
||||
player.addEventListener("painted", handlePainted);
|
||||
player.addEventListener("assetsready", handleAssetsReady);
|
||||
player.addEventListener("error", handleError);
|
||||
|
||||
// Bridge the inner iframe to the forwarded ref for useTimelinePlayer.
|
||||
@@ -349,6 +351,7 @@ export const Player = forwardRef<HTMLIFrameElement, PlayerProps>(
|
||||
player.removeEventListener("shadertransitionstate", handleShaderTransitionState);
|
||||
player.removeEventListener("ready", handleReady);
|
||||
player.removeEventListener("painted", handlePainted);
|
||||
player.removeEventListener("assetsready", handleAssetsReady);
|
||||
player.removeEventListener("error", handleError);
|
||||
if (assetPollRef.current) clearInterval(assetPollRef.current);
|
||||
assetPollRef.current = null;
|
||||
@@ -456,7 +459,9 @@ export const Player = forwardRef<HTMLIFrameElement, PlayerProps>(
|
||||
}, [readyToShow]);
|
||||
|
||||
useEffect(() => {
|
||||
if (previewError) usePlayerStore.getState().markPreviewBooted();
|
||||
if (!previewError) return;
|
||||
usePlayerStore.getState().markPreviewBooted();
|
||||
usePlayerStore.getState().markPreviewAssetsSettled();
|
||||
}, [previewError]);
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
@@ -3,6 +3,7 @@ import React, { act } from "react";
|
||||
import { createRoot, type Root } from "react-dom/client";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { thumbnailScheduler } from "../lib/thumbnailScheduler";
|
||||
import { usePlayerStore } from "../store/playerStore";
|
||||
import { decodeVideoThumbnail } from "../lib/thumbnailVideoDecoder";
|
||||
import { VideoThumbnail } from "./VideoThumbnail";
|
||||
|
||||
@@ -24,6 +25,7 @@ let root: Root | null = null;
|
||||
|
||||
beforeEach(() => {
|
||||
globalThis.ResizeObserver = MockResizeObserver as unknown as typeof ResizeObserver;
|
||||
usePlayerStore.setState({ previewAssetsSettled: true });
|
||||
host = document.createElement("div");
|
||||
document.body.append(host);
|
||||
});
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
probeMissingSourceDurations,
|
||||
resetMediaProbeRegistry,
|
||||
} from "./mediaProbe";
|
||||
import { usePlayerStore } from "../store/playerStore";
|
||||
|
||||
const dispose = vi.fn();
|
||||
const getDurationFromMetadata = vi.fn(async () => 5);
|
||||
@@ -33,6 +34,7 @@ beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
requestedSources.length = 0;
|
||||
getDurationFromMetadata.mockResolvedValue(5);
|
||||
usePlayerStore.setState({ previewAssetsSettled: true });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -89,6 +91,23 @@ describe("media probe registry", () => {
|
||||
expect(getDurationFromMetadata).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("waits to open a file until the preview's opening assets settle", async () => {
|
||||
usePlayerStore.setState({ previewAssetsSettled: false });
|
||||
const apply = vi.fn();
|
||||
const probing = probeMissingSourceDurations(
|
||||
[{ id: "clip", tag: "video", src: "/assets/clip.mp4" }],
|
||||
null,
|
||||
apply,
|
||||
);
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
expect(requestedSources).toEqual([]);
|
||||
|
||||
usePlayerStore.getState().markPreviewAssetsSettled();
|
||||
await probing;
|
||||
expect(requestedSources).toHaveLength(1);
|
||||
expect(apply).toHaveBeenCalledWith("clip", 5);
|
||||
});
|
||||
|
||||
it("probes same-origin rooted media through the active project preview", async () => {
|
||||
const apply = vi.fn();
|
||||
await probeMissingSourceDurations(
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { resolveMediaPreviewUrl } from "../components/thumbnailUtils";
|
||||
import { whenPreviewAssetsSettled } from "../store/playerStore";
|
||||
import { TIMELINE_VIEWPORT_BUDGETS } from "./timelineViewportBudgets";
|
||||
|
||||
export interface MediaProbeResult {
|
||||
@@ -148,6 +149,8 @@ export async function probeMissingSourceDurations<
|
||||
: [];
|
||||
});
|
||||
if (needs.length === 0) return;
|
||||
// A probe opens the file with an open-ended range, competing with the preview's own first frame.
|
||||
await whenPreviewAssetsSettled();
|
||||
await Promise.allSettled(
|
||||
needs.map(async ({ el, source }) => {
|
||||
const result = await probeMediaUrl(source);
|
||||
|
||||
@@ -186,6 +186,27 @@ describe("ThumbnailScheduler", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("holds video frames and waveforms until the preview's opening assets settle", async () => {
|
||||
const scheduler = new ThumbnailScheduler();
|
||||
const video = vi.fn(async () => result("frames"));
|
||||
const waveform = vi.fn(async () => result("peaks"));
|
||||
const image = vi.fn(async () => result("still"));
|
||||
|
||||
scheduler.setPreviewAssetsPending(true);
|
||||
scheduler.acquire(request("frames", video, "visible", { kind: "video" }), vi.fn());
|
||||
scheduler.acquire(request("peaks", waveform, "visible", { kind: "waveform" }), vi.fn());
|
||||
scheduler.acquire(request("still", image), vi.fn());
|
||||
await flush();
|
||||
expect(image).toHaveBeenCalledTimes(1);
|
||||
expect(video).not.toHaveBeenCalled();
|
||||
expect(waveform).not.toHaveBeenCalled();
|
||||
|
||||
scheduler.setPreviewAssetsPending(false);
|
||||
await flush();
|
||||
expect(video).toHaveBeenCalledTimes(1);
|
||||
expect(waveform).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("aborts queued and active jobs after the final release", async () => {
|
||||
const scheduler = new ThumbnailScheduler(
|
||||
resolveTimelineViewportBudgets({ concurrentVideoDecodes: 1 }),
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { usePlayerStore } from "../store/playerStore";
|
||||
import { TIMELINE_VIEWPORT_BUDGETS, type TimelineViewportBudgets } from "./timelineViewportBudgets";
|
||||
|
||||
export type ThumbnailPriority = "overscan" | "visible" | "interaction";
|
||||
@@ -83,6 +84,11 @@ function concurrencyBucket(kind: ThumbnailJobKind): "video" | "composition" | "g
|
||||
return "general";
|
||||
}
|
||||
|
||||
/** Video frames and waveforms fetch the media files the preview's first frame is waiting for. */
|
||||
function readsPreviewMedia(kind: ThumbnailJobKind): boolean {
|
||||
return kind === "video" || kind === "waveform";
|
||||
}
|
||||
|
||||
function errorFrom(reason: unknown): Error {
|
||||
return reason instanceof Error ? reason : new Error(String(reason));
|
||||
}
|
||||
@@ -115,6 +121,7 @@ export class ThumbnailScheduler {
|
||||
private nextSequence = 1;
|
||||
private scrolling = false;
|
||||
private previewReloading = false;
|
||||
private previewAssetsPending = false;
|
||||
private cacheBytes = 0;
|
||||
private waveformCacheBytes = 0;
|
||||
private readonly activeByBucket = { video: 0, composition: 0, general: 0 };
|
||||
@@ -218,6 +225,12 @@ export class ThumbnailScheduler {
|
||||
* Hold server-rendered composition thumbnails while the preview loads a new document: both
|
||||
* are served by the same Studio server, and the preview is what the person is waiting for.
|
||||
*/
|
||||
setPreviewAssetsPending(pending: boolean): void {
|
||||
if (this.previewAssetsPending === pending) return;
|
||||
this.previewAssetsPending = pending;
|
||||
if (!pending) this.pump();
|
||||
}
|
||||
|
||||
setPreviewReloading(reloading: boolean): void {
|
||||
if (this.previewReloading === reloading) return;
|
||||
this.previewReloading = reloading;
|
||||
@@ -280,6 +293,7 @@ export class ThumbnailScheduler {
|
||||
for (const entry of queued) {
|
||||
if (this.scrolling && entry.request.rich) continue;
|
||||
if (this.previewReloading && entry.request.kind === "composition") continue;
|
||||
if (this.previewAssetsPending && readsPreviewMedia(entry.request.kind)) continue;
|
||||
const bucket = concurrencyBucket(entry.request.kind);
|
||||
if (this.activeByBucket[bucket] >= this.bucketLimit(bucket)) continue;
|
||||
this.start(entry, bucket);
|
||||
@@ -515,3 +529,7 @@ export class ThumbnailScheduler {
|
||||
}
|
||||
|
||||
export const thumbnailScheduler = new ThumbnailScheduler();
|
||||
thumbnailScheduler.setPreviewAssetsPending(!usePlayerStore.getState().previewAssetsSettled);
|
||||
usePlayerStore.subscribe((state) =>
|
||||
thumbnailScheduler.setPreviewAssetsPending(!state.previewAssetsSettled),
|
||||
);
|
||||
|
||||
@@ -557,6 +557,18 @@ export function isPreviewBooted(projectId: string): boolean {
|
||||
return previewBooted && timelineProjectId === projectId;
|
||||
}
|
||||
|
||||
/** Resolves once the live preview's opening assets have settled, or its load failed. */
|
||||
export function whenPreviewAssetsSettled(): Promise<void> {
|
||||
if (usePlayerStore.getState().previewAssetsSettled) return Promise.resolve();
|
||||
return new Promise((resolve) => {
|
||||
const stop = usePlayerStore.subscribe((state) => {
|
||||
if (!state.previewAssetsSettled) return;
|
||||
stop();
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/** True once projectId's live preview has booted, false once another project replaces it.
|
||||
* Open-time work the first frame does not need (server parses, lint) waits on it. */
|
||||
export function whenPreviewBooted(projectId: string): Promise<boolean> {
|
||||
|
||||
@@ -11,8 +11,12 @@ export interface PlaybackReadinessSlice {
|
||||
/** Latched when the project's first preview shows and can play (or fails), kept through edit
|
||||
* reloads, so work that must not compete with the boot waits for it once. */
|
||||
previewBooted: boolean;
|
||||
/** Latched when the first preview's opening assets settle (the player's own capped wait) or it
|
||||
* fails. Media reads that fetch those same files wait for it; unlike previewBooted, no deadline. */
|
||||
previewAssetsSettled: boolean;
|
||||
setTimelineReady: (ready: boolean) => void;
|
||||
markPreviewBooted: () => void;
|
||||
markPreviewAssetsSettled: () => void;
|
||||
/** Sets timelineReady once doc's readiness inputs settle, or immediately
|
||||
* if doc is null. A wait a later call supersedes never wins the race. */
|
||||
requestTimelineReady: (doc: Document | null) => void;
|
||||
@@ -24,10 +28,10 @@ let timelineReadyGeneration = 0;
|
||||
* wait in flight can never resolve into what replaced it. */
|
||||
export function resetPlaybackReadinessState(): Pick<
|
||||
PlaybackReadinessSlice,
|
||||
"timelineReady" | "previewBooted"
|
||||
"timelineReady" | "previewBooted" | "previewAssetsSettled"
|
||||
> {
|
||||
timelineReadyGeneration++;
|
||||
return { timelineReady: false, previewBooted: false };
|
||||
return { timelineReady: false, previewBooted: false, previewAssetsSettled: false };
|
||||
}
|
||||
|
||||
export function createPlaybackReadinessSlice(
|
||||
@@ -36,7 +40,9 @@ export function createPlaybackReadinessSlice(
|
||||
return {
|
||||
timelineReady: false,
|
||||
previewBooted: false,
|
||||
previewAssetsSettled: false,
|
||||
markPreviewBooted: () => set({ previewBooted: true }),
|
||||
markPreviewAssetsSettled: () => set({ previewAssetsSettled: true }),
|
||||
setTimelineReady: (ready) => {
|
||||
timelineReadyGeneration++;
|
||||
set({ timelineReady: ready });
|
||||
|
||||
Reference in New Issue
Block a user