Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
fix(studio): keep an edit's preview waiting while the window draws no frames (#5926)
Miguel Ángelf5033879f763
diff --git a/packages/studio/src/player/hooks/useShadowPreviewReload.ts b/packages/studio/src/player/hooks/useShadowPreviewReload.tsindex 39e114649d..91cd9c7c67 100644--- a/packages/studio/src/player/hooks/useShadowPreviewReload.ts+++ b/packages/studio/src/player/hooks/useShadowPreviewReload.ts@@ -25,7 +25,7 @@ import { usePlayerStore } from "../store/playerStore";// 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.+// Spent only while frames are drawn (readiness needs them); a hidden tab or a covered Linux window draws 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;@@ -276,8 +276,19 @@ export function useShadowPreviewReload({(gen: number) => {clearTimeout(readyTimerRef.current);if (isDocumentHidden()) return;+ let drew = false;+ let starved = false;+ requestAnimationFrame(() => {+ if (!starved) drew = true;+ else if (budgetGenRef.current === gen) armReadyTimerRef.current(gen);+ });readyTimerRef.current = setTimeout(() => {if (gen !== shadowGenRef.current) return;+ if (!drew) {+ starved = true;+ logReload("shadow-starved", { gen });+ return;+ }budgetsSpentRef.current += 1;if (budgetsSpentRef.current < SHADOW_READY_BUDGETS) {logReload("shadow-slow", { budgetsSpent: budgetsSpentRef.current });diff --git a/packages/studio/src/player/hooks/useTimelinePlayer.shadowReload.test.ts b/packages/studio/src/player/hooks/useTimelinePlayer.shadowReload.test.tsindex 6770367ddc..873ead27ea 100644--- a/packages/studio/src/player/hooks/useTimelinePlayer.shadowReload.test.ts+++ b/packages/studio/src/player/hooks/useTimelinePlayer.shadowReload.test.ts@@ -545,6 +545,60 @@ describe("shadow reload readiness and failure", () => {unmount(root);});+ // A window behind others on some Linux desktops draws no frames yet reports visible; readiness needs frames.+ it("does not spend the budget while the window draws no frames, and promotes once they return", () => {+ vi.useFakeTimers();+ const frames = holdFrames();+ 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(FULL_WAIT_MS * 2));+ expect(onPreviewReloadFailed).not.toHaveBeenCalled();+ expect(getApi().previewSlots).toHaveLength(2);++ act(() => frames.resume());+ act(() => getApi().onShadowReadyChange(gen, true));+ expect(getApi().iframeRef.current).toBe(shadow.iframe);+ unmount(root);+ });++ it("gives a shadow its full budget once frames return, then fails one that stays silent", () => {+ vi.useFakeTimers();+ vi.spyOn(console, "error").mockImplementation(() => {});+ const frames = holdFrames();+ const onPreviewReloadFailed = vi.fn();+ const { getApi, live, root } = beginReload({ onPreviewReloadFailed });++ act(() => void vi.advanceTimersByTime(FULL_WAIT_MS));+ act(() => frames.resume());+ act(() => void vi.advanceTimersByTime(FULL_WAIT_MS - 1));+ expect(onPreviewReloadFailed).not.toHaveBeenCalled();+ act(() => void vi.advanceTimersByTime(1));+ expect(onPreviewReloadFailed).toHaveBeenCalledWith(expect.stringContaining("too long"));+ expect(getApi().iframeRef.current).toBe(live);+ unmount(root);+ });++ it("keeps a reload's own budget when the frames a replaced one waited on return", () => {+ vi.useFakeTimers();+ vi.spyOn(console, "error").mockImplementation(() => {});+ const frames = holdFrames();+ const onPreviewReloadFailed = vi.fn();+ const { getApi, root } = beginReload({ onPreviewReloadFailed });+ act(() => void vi.advanceTimersByTime(SHADOW_READY_TIMEOUT_MS + 1000));++ act(() => getApi().refreshPlayer());+ act(() => frames.resume());+ act(() => void vi.advanceTimersByTime(FULL_WAIT_MS));+ expect(onPreviewReloadFailed).toHaveBeenCalledWith(expect.stringContaining("too long"));+ unmount(root);+ });+it("still drops and reports a shadow whose window throws on access", () => {vi.spyOn(console, "error").mockImplementation(() => {});const onPreviewReloadFailed = vi.fn();@@ -806,6 +860,25 @@ function unmount(root: ReturnType<typeof createRoot>) {});}+/** A window that draws no frames although it reports visible: frames are held until `resume`, then one per 16 ms. */+function holdFrames() {+ let held: FrameRequestCallback[] = [];+ let flowing = false;+ vi.spyOn(window, "requestAnimationFrame").mockImplementation((callback) => {+ if (flowing) return Number(setTimeout(() => callback(performance.now()), 16));+ held.push(callback);+ return held.length;+ });+ return {+ resume: () => {+ flowing = true;+ const waiting = held;+ held = [];+ for (const callback of waiting) callback(performance.now());+ },+ };+}+function stubVisibility(initial: DocumentVisibilityState) {let state = initial;vi.spyOn(document, "visibilityState", "get").mockImplementation(() => state);