Skip to content

Write HTML. Render video. Built for agents.

fix(studio): an edit still reaches the preview when its reload is slow (#4418)

Miguel Ángel1926903b6a57
diff --git a/packages/studio/src/player/hooks/useShadowPreviewReload.ts b/packages/studio/src/player/hooks/useShadowPreviewReload.ts
index 500c339eef..fd446a1cae 100644
--- a/packages/studio/src/player/hooks/useShadowPreviewReload.ts
+++ b/packages/studio/src/player/hooks/useShadowPreviewReload.ts
@@ -15,10 +15,12 @@ import {
import type { PlaybackAdapter } from "../lib/playbackTypes";
import { thumbnailScheduler } from "../lib/thumbnailScheduler";
-// The single wait budget for a shadow: the player's 8s asset cap plus its 0.42s loader fade
+// One wait budget for a shadow: the player's 8s asset cap plus its 0.42s loader fade
// leaves about 6.5s for the document load and runtime boot. Nothing shorter may fail the swap.
// It only runs while the tab is visible: readiness is frame-driven, and a hidden tab renders none.
export const SHADOW_READY_TIMEOUT_MS = 15_000;
+// A busy machine can need more than one budget; the shadow keeps loading for this many before it is dropped.
+export const SHADOW_READY_BUDGETS = 3;
function isDocumentHidden(): boolean {
return typeof document !== "undefined" && document.visibilityState === "hidden";
@@ -61,6 +63,7 @@ export function useShadowPreviewReload({
const onReloadFailedRef = useRef(onReloadFailed);
onReloadFailedRef.current = onReloadFailed;
const readyTimerRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
+ const budgetsSpentRef = useRef(0);
// The shadow still owed a wait budget, so a hidden tab can resume it when it becomes visible.
const budgetGenRef = useRef<number | null>(null);
const cancelPendingLoadRef = useRef<() => void>(() => {});
@@ -160,10 +163,16 @@ export function useShadowPreviewReload({
(gen: number) => {
clearTimeout(readyTimerRef.current);
if (isDocumentHidden()) return;
- readyTimerRef.current = setTimeout(
- () => failShadow(gen, "it took too long to load"),
- SHADOW_READY_TIMEOUT_MS,
- );
+ readyTimerRef.current = setTimeout(() => {
+ if (gen !== shadowGenRef.current) return;
+ budgetsSpentRef.current += 1;
+ if (budgetsSpentRef.current < SHADOW_READY_BUDGETS) {
+ logReload("shadow-slow", { budgetsSpent: budgetsSpentRef.current });
+ armReadyTimerRef.current(gen);
+ return;
+ }
+ failShadow(gen, "it took too long to load");
+ }, SHADOW_READY_TIMEOUT_MS);
},
[failShadow],
);
@@ -176,6 +185,7 @@ export function useShadowPreviewReload({
const gen = shadowGenRef.current;
stopPendingShadow();
budgetGenRef.current = gen;
+ budgetsSpentRef.current = 0;
armReadyTimer(gen);
setPreviewSlots((prev) => planShadowReload(prev, gen, url));
// Thumbnails of the edit wait for the new preview instead of competing with it.
diff --git a/packages/studio/src/player/hooks/useTimelinePlayer.shadowReload.test.ts b/packages/studio/src/player/hooks/useTimelinePlayer.shadowReload.test.ts
index 7fa37aa5d2..c020aec8a3 100644
--- a/packages/studio/src/player/hooks/useTimelinePlayer.shadowReload.test.ts
+++ b/packages/studio/src/player/hooks/useTimelinePlayer.shadowReload.test.ts
@@ -4,7 +4,7 @@
import React, { act } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, describe, expect, it, vi } from "vitest";
-import { SHADOW_READY_TIMEOUT_MS } from "./useShadowPreviewReload";
+import { SHADOW_READY_BUDGETS, SHADOW_READY_TIMEOUT_MS } from "./useShadowPreviewReload";
import { usePlayerStore } from "../store/playerStore";
import { NLEProvider, useNLEContext, type NLEContextValue } from "../../components/nle/NLEContext";
import {
@@ -16,6 +16,8 @@ import {
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
+const FULL_WAIT_MS = SHADOW_READY_TIMEOUT_MS * SHADOW_READY_BUDGETS;
+
vi.mock("../../utils/gsapSoftReload", async (importOriginal) => ({
...(await importOriginal<typeof import("../../utils/gsapSoftReload")>()),
ensureMotionPathPluginLoaded: vi.fn(),
@@ -258,7 +260,7 @@ describe("shadow reload readiness and failure", () => {
const { getApi, live, root } = beginReload({ onPreviewReloadFailed });
expect(getApi().previewSlots).toHaveLength(2);
- act(() => void vi.advanceTimersByTime(SHADOW_READY_TIMEOUT_MS));
+ act(() => void vi.advanceTimersByTime(FULL_WAIT_MS));
expect(getApi().previewSlots).toEqual([{ gen: 0, role: "live" }]);
expect(getApi().iframeRef.current).toBe(live);
@@ -268,7 +270,7 @@ describe("shadow reload readiness and failure", () => {
unmount(root);
});
- it("keeps a slow shadow pending past 5s and fails it only at the single budget", () => {
+ it("keeps a slow shadow pending past 5s and fails it only once all budgets are spent", () => {
vi.useFakeTimers();
vi.spyOn(console, "error").mockImplementation(() => {});
const onPreviewReloadFailed = vi.fn();
@@ -283,12 +285,47 @@ describe("shadow reload readiness and failure", () => {
expect(getApi().previewSlots).toHaveLength(2);
expect(onPreviewReloadFailed).not.toHaveBeenCalled();
- act(() => void vi.advanceTimersByTime(SHADOW_READY_TIMEOUT_MS));
+ act(() => void vi.advanceTimersByTime(FULL_WAIT_MS - 5001 - 1));
+ expect(onPreviewReloadFailed).not.toHaveBeenCalled();
+ act(() => void vi.advanceTimersByTime(1));
expect(getApi().previewSlots).toEqual([{ gen: 0, role: "live" }]);
expect(onPreviewReloadFailed).toHaveBeenCalledWith(expect.stringContaining("too long"));
unmount(root);
});
+ // A busy machine: the edit's shadow paints after one budget; dropping it left the stage on the pre-edit file.
+ it("promotes a shadow that paints after its first budget instead of dropping the edit", () => {
+ vi.useFakeTimers();
+ const onPreviewReloadFailed = vi.fn();
+ const { getApi, gen, root } = beginReload({ onPreviewReloadFailed });
+ const shadow = makeShadowWithSpies();
+ act(() => {
+ getApi().setShadowIframeNode(shadow.iframe);
+ getApi().onShadowIframeLoad(gen);
+ });
+
+ act(() => void vi.advanceTimersByTime(SHADOW_READY_TIMEOUT_MS + 2000));
+ expect(onPreviewReloadFailed).not.toHaveBeenCalled();
+ act(() => getApi().onShadowReadyChange(gen, true));
+ expect(getApi().iframeRef.current).toBe(shadow.iframe);
+ unmount(root);
+ });
+
+ it("gives a reload that replaces a slow one its own full set of budgets", () => {
+ vi.useFakeTimers();
+ vi.spyOn(console, "error").mockImplementation(() => {});
+ const onPreviewReloadFailed = vi.fn();
+ const { getApi, root } = beginReload({ onPreviewReloadFailed });
+ act(() => void vi.advanceTimersByTime(SHADOW_READY_TIMEOUT_MS + 1000));
+
+ act(() => getApi().refreshPlayer());
+ act(() => void vi.advanceTimersByTime(FULL_WAIT_MS - 1));
+ expect(onPreviewReloadFailed).not.toHaveBeenCalled();
+ act(() => void vi.advanceTimersByTime(1));
+ expect(onPreviewReloadFailed).toHaveBeenCalledWith(expect.stringContaining("too long"));
+ unmount(root);
+ });
+
it("does not spend the budget while the tab is hidden, and promotes once it is visible", () => {
vi.useFakeTimers();
const onPreviewReloadFailed = vi.fn();
@@ -325,7 +362,7 @@ describe("shadow reload readiness and failure", () => {
expect(onPreviewReloadFailed).not.toHaveBeenCalled();
act(() => setVisibility("visible"));
- act(() => void vi.advanceTimersByTime(SHADOW_READY_TIMEOUT_MS - 1));
+ act(() => void vi.advanceTimersByTime(FULL_WAIT_MS - 1));
expect(onPreviewReloadFailed).not.toHaveBeenCalled();
act(() => void vi.advanceTimersByTime(1));
expect(onPreviewReloadFailed).toHaveBeenCalledWith(expect.stringContaining("too long"));
@@ -431,7 +468,7 @@ describe("shadow reload store ownership and readiness", () => {
const { getApi, rerender, root } = setup({ onPreviewReloadFailed: first });
expect(getApi().previewSlots).toHaveLength(2);
rerender({ onPreviewReloadFailed: latest });
- act(() => void vi.advanceTimersByTime(SHADOW_READY_TIMEOUT_MS));
+ act(() => void vi.advanceTimersByTime(FULL_WAIT_MS));
expect(latest).toHaveBeenCalled();
expect(first).not.toHaveBeenCalled();
unmount(root);