diff --git a/app/src/components/projects/home/AwsWorkspaceStatusCard.test.tsx b/app/src/components/projects/home/AwsWorkspaceStatusCard.test.tsx index e1028c0..50642b6 100644 --- a/app/src/components/projects/home/AwsWorkspaceStatusCard.test.tsx +++ b/app/src/components/projects/home/AwsWorkspaceStatusCard.test.tsx @@ -113,4 +113,39 @@ describe("AwsWorkspaceStatusCard", () => { expect(awsWorkspaceStatus).toHaveBeenCalledTimes(2); state.mockRestore(); }); + + it("never runs two polls at once when the window is hidden and shown mid-read", async () => { + let resolve: (s: WorkspaceStatus) => void = () => {}; + awsWorkspaceStatus.mockImplementation(() => new Promise((r) => (resolve = r))); + render(); + await flush(); + expect(awsWorkspaceStatus).toHaveBeenCalledTimes(1); + // Hidden and shown again while the first read is still out. + const state = vi.spyOn(document, "visibilityState", "get").mockReturnValue("hidden"); + await act(async () => { + document.dispatchEvent(new Event("visibilitychange")); + }); + state.mockReturnValue("visible"); + await act(async () => { + document.dispatchEvent(new Event("visibilitychange")); + }); + await flush(); + expect(awsWorkspaceStatus).toHaveBeenCalledTimes(1); + // The read ends: one chain goes on, one read per interval. + await act(async () => resolve(CLONING)); + await flush(); + await act(async () => { + vi.advanceTimersByTime(STATUS_POLL_MS); + }); + await flush(); + expect(awsWorkspaceStatus).toHaveBeenCalledTimes(2); + await act(async () => resolve(CLONING)); + await flush(); + await act(async () => { + vi.advanceTimersByTime(STATUS_POLL_MS); + }); + await flush(); + expect(awsWorkspaceStatus).toHaveBeenCalledTimes(3); + state.mockRestore(); + }); }); diff --git a/app/src/components/projects/home/AwsWorkspaceStatusCard.tsx b/app/src/components/projects/home/AwsWorkspaceStatusCard.tsx index 06fe5ad..5b55a43 100644 --- a/app/src/components/projects/home/AwsWorkspaceStatusCard.tsx +++ b/app/src/components/projects/home/AwsWorkspaceStatusCard.tsx @@ -29,20 +29,26 @@ export default function AwsWorkspaceStatusCard({ project }: { project: Project } if (!running) return; let cancelled = false; let timer: ReturnType | null = null; + // A read in progress: hiding and showing the window during it must not + // start a second poll chain beside this one. + let inFlight = false; const visible = () => document.visibilityState !== "hidden"; const poll = async () => { timer = null; if (!visible()) return; // resumed by visibilitychange + inFlight = true; try { const s = await awsWorkspaceStatus(project.id); if (!cancelled) setStatus(s); } catch { // A failed read keeps the last status; the next poll tries again. + } finally { + inFlight = false; } if (!cancelled) timer = setTimeout(poll, STATUS_POLL_MS); }; const onVisibility = () => { - if (visible() && timer === null && !cancelled) void poll(); + if (visible() && timer === null && !inFlight && !cancelled) void poll(); }; void poll(); document.addEventListener("visibilitychange", onVisibility);