Debounce git status refreshes on window focus
- Avoid duplicate refreshes when focus and visibility events fire together - Add coverage for the 250ms debounce
This commit is contained in:
1 parent
f5bec5e585
commit
5326507ff5
2 files changed
+55
-5
No files matched your search
@@ -235,4 +235,42 @@ describe("GitActionsControl thread-scoped progress toast", () => {
|
||||
host.remove();
|
||||
}
|
||||
});
|
||||
|
||||
it("debounces focus-driven git status refreshes", async () => {
|
||||
vi.useFakeTimers();
|
||||
|
||||
const originalVisibilityState = Object.getOwnPropertyDescriptor(document, "visibilityState");
|
||||
let visibilityState: DocumentVisibilityState = "hidden";
|
||||
Object.defineProperty(document, "visibilityState", {
|
||||
configurable: true,
|
||||
get: () => visibilityState,
|
||||
});
|
||||
|
||||
const host = document.createElement("div");
|
||||
document.body.append(host);
|
||||
const screen = await render(<GitActionsControl gitCwd={GIT_CWD} activeThreadId={THREAD_A} />, {
|
||||
container: host,
|
||||
});
|
||||
|
||||
try {
|
||||
window.dispatchEvent(new Event("focus"));
|
||||
visibilityState = "visible";
|
||||
document.dispatchEvent(new Event("visibilitychange"));
|
||||
|
||||
expect(refreshGitStatusSpy).not.toHaveBeenCalled();
|
||||
|
||||
await vi.advanceTimersByTimeAsync(249);
|
||||
expect(refreshGitStatusSpy).not.toHaveBeenCalled();
|
||||
|
||||
await vi.advanceTimersByTimeAsync(1);
|
||||
expect(refreshGitStatusSpy).toHaveBeenCalledTimes(1);
|
||||
expect(refreshGitStatusSpy).toHaveBeenCalledWith(GIT_CWD);
|
||||
} finally {
|
||||
if (originalVisibilityState) {
|
||||
Object.defineProperty(document, "visibilityState", originalVisibilityState);
|
||||
}
|
||||
await screen.unmount();
|
||||
host.remove();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -91,6 +91,8 @@ interface RunGitActionWithToastInput {
|
||||
filePaths?: string[];
|
||||
}
|
||||
|
||||
const GIT_STATUS_WINDOW_REFRESH_DEBOUNCE_MS = 250;
|
||||
|
||||
function formatElapsedDescription(startedAtMs: number | null): string | undefined {
|
||||
if (startedAtMs === null) {
|
||||
return undefined;
|
||||
@@ -363,20 +365,30 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions
|
||||
return;
|
||||
}
|
||||
|
||||
const refreshCurrentGitStatus = () => {
|
||||
void refreshGitStatus(gitCwd).catch(() => undefined);
|
||||
let refreshTimeout: number | null = null;
|
||||
const scheduleRefreshCurrentGitStatus = () => {
|
||||
if (refreshTimeout !== null) {
|
||||
window.clearTimeout(refreshTimeout);
|
||||
}
|
||||
refreshTimeout = window.setTimeout(() => {
|
||||
refreshTimeout = null;
|
||||
void refreshGitStatus(gitCwd).catch(() => undefined);
|
||||
}, GIT_STATUS_WINDOW_REFRESH_DEBOUNCE_MS);
|
||||
};
|
||||
const handleVisibilityChange = () => {
|
||||
if (document.visibilityState === "visible") {
|
||||
refreshCurrentGitStatus();
|
||||
scheduleRefreshCurrentGitStatus();
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener("focus", refreshCurrentGitStatus);
|
||||
window.addEventListener("focus", scheduleRefreshCurrentGitStatus);
|
||||
document.addEventListener("visibilitychange", handleVisibilityChange);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener("focus", refreshCurrentGitStatus);
|
||||
if (refreshTimeout !== null) {
|
||||
window.clearTimeout(refreshTimeout);
|
||||
}
|
||||
window.removeEventListener("focus", scheduleRefreshCurrentGitStatus);
|
||||
document.removeEventListener("visibilitychange", handleVisibilityChange);
|
||||
};
|
||||
}, [gitCwd]);
|
||||
|
||||
Reference in new issue
Block a user