Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(preview): key the reload budget on the media, not just the asset id

EtienneLescotd35ca823e76a
diff --git a/src/components/ai-edition/VirtualPreview.mediaError.test.tsx b/src/components/ai-edition/VirtualPreview.mediaError.test.tsx
index 58975ad08..1329e0223 100644
--- a/src/components/ai-edition/VirtualPreview.mediaError.test.tsx
+++ b/src/components/ai-edition/VirtualPreview.mediaError.test.tsx
@@ -132,17 +132,18 @@ function driveVideo(element: HTMLVideoElement) {
const SOURCES: VideoSource[] = [{ id: "a1", src: "file:///tmp/a1.mp4", label: "a1" }];
-function mount(clips: AxcutClip[] = [clip("clip_1", "a1", 0)]) {
+function mount(clips: AxcutClip[] = [clip("clip_1", "a1", 0)], sources: VideoSource[] = SOURCES) {
const onVideoError = vi.fn<(assetId: string, detail: string) => void>();
const onVideoRecovered = vi.fn<(assetId: string) => void>();
const onTimeChange = vi.fn<(timeSec: number) => void>();
let seekRequestId = 0;
+ let activeSources = sources;
const tree = (
retryToken: number,
seekTarget: { timeSec: number; requestId: number } | null = null,
) => (
<VirtualPreview
- videoSources={SOURCES}
+ videoSources={activeSources}
clips={clips}
onTimeChange={onTimeChange}
onVideoError={onVideoError}
@@ -167,6 +168,12 @@ function mount(clips: AxcutClip[] = [clip("clip_1", "a1", 0)]) {
onVideoRecovered,
onTimeChange,
bumpRetryToken: (token: number) => view.rerender(tree(token)),
+ /** Re-point the mounted asset at a different file, keeping its id. */
+ repointSourceTo: (src: string) =>
+ act(() => {
+ activeSources = activeSources.map((source) => ({ ...source, src }));
+ view.rerender(tree(0));
+ }),
/** Move the playhead the way the shell does — a new seekTarget requestId. */
scrubTo: (timeSec: number) =>
act(() => {
@@ -367,6 +374,31 @@ describe("VirtualPreview media-error recovery (issue #395)", () => {
expect(video.loadCalls).toBe(loadsBefore + 1);
});
+ // The element is keyed on the asset id alone, so pointing an asset at a new
+ // file reloads the SAME element — and a budget spent on the old file must not
+ // be charged to the new one. Nothing in the app re-points an asset today, but
+ // it is the shape a "relink the moved file" flow would take, and this is the
+ // component's own contract rather than the app's current use of it.
+ it("gives a re-pointed source a fresh budget", () => {
+ const { video, onVideoError, repointSourceTo } = mount();
+
+ for (const delay of [...RETRY_DELAYS_MS, 0]) {
+ video.fail(3);
+ advance(delay);
+ }
+ expect(onVideoError).toHaveBeenCalledTimes(1);
+
+ repointSourceTo("file:///tmp/a1-restored.mp4");
+ onVideoError.mockClear();
+ const loadsBefore = video.loadCalls;
+
+ video.fail(3);
+ advance(RETRY_DELAYS_MS[0]);
+
+ expect(onVideoError).not.toHaveBeenCalled();
+ expect(video.loadCalls).toBe(loadsBefore + 1);
+ });
+
// A reload scheduled for an element that is going away must never fire: the
// <video> is keyed on the asset id, so the timer can outlive its element.
it("does not reload an unmounted element", () => {
diff --git a/src/components/ai-edition/VirtualPreview.tsx b/src/components/ai-edition/VirtualPreview.tsx
index 884718e3d..42df3bb95 100644
--- a/src/components/ai-edition/VirtualPreview.tsx
+++ b/src/components/ai-edition/VirtualPreview.tsx
@@ -635,11 +635,17 @@ export function VirtualPreview({
if (id) onVideoRecovered?.(id);
}, [onVideoRecovered]);
- // Reload bookkeeping belongs to the MOUNTED element: the <video> is keyed on
- // the asset id, so a source swap replaces it, and a reload scheduled for the
- // old one must never fire against its replacement. React runs this cleanup
+ // Reload bookkeeping belongs to the MOUNTED MEDIA, so it is keyed on the URL
+ // as well as the asset id — the element is keyed on the id alone (see the
+ // <video> below), so re-pointing an asset at a different file re-runs the
+ // load algorithm on the SAME element, and a budget spent on the old file
+ // must not be charged to the new one. Nothing re-points an asset today; the
+ // relink this card's copy invites would be the first, and it is the exact
+ // case where a fresh budget is the whole point.
+ // The cleanup matters for the swap that IS reachable: a reload scheduled for
+ // the old element must never fire against its replacement. React runs it
// before any queued macrotask can.
- // biome-ignore lint/correctness/useExhaustiveDependencies: re-run on source swap
+ // biome-ignore lint/correctness/useExhaustiveDependencies: re-run when the mounted media changes
useEffect(() => {
retryRef.current = { attempts: 0, timer: null };
recoveringRef.current = false;
@@ -650,7 +656,7 @@ export function VirtualPreview({
retryRef.current.timer = null;
}
};
- }, [activeSourceKey]);
+ }, [activeSourceKey, activeSource?.src]);
// The caller's Retry button. Same path as an automatic reload so the two
// cannot drift, but with a fresh budget and no delay: the user clicked