fix(aws): never run two workspace-status polls at once
A read in flight blocks the visibilitychange restart, so hiding and showing the window mid-read no longer starts a second poll chain. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3bc2b41b70
commit
5388db487e
2 files changed
+42
-1
No files matched your search
@@ -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(<AwsWorkspaceStatusCard project={project()} />);
|
||||
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();
|
||||
});
|
||||
});
|
||||
@@ -29,20 +29,26 @@ export default function AwsWorkspaceStatusCard({ project }: { project: Project }
|
||||
if (!running) return;
|
||||
let cancelled = false;
|
||||
let timer: ReturnType<typeof setTimeout> | 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);
|
||||
|
||||
Reference in new issue
Block a user