mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-08 08:52:12 +00:00
Open files only reloaded from file watcher events, so a change the watcher skipped stayed stale in its tab: excluded and git-ignored folders, files outside the project, and missed events. - Re-check open documents against disk when the window gains focus, off the UI thread and debounced. Clean documents reload, dirty ones keep the existing warning, and unchanged ones cost one metadata call. - Refresh the project tree and Git status on the same focus event. - Add "Synchronize external changes when switching to the IDE window" under System Settings, on by default. Fixes #563
52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
import { executeAction } from "@/platform/actions";
|
|
import { useProject } from "@/platform/project";
|
|
|
|
/** One debounced disk check per window activation, scoped to its current project. */
|
|
export function installDiskSync(sync: () => Promise<void>, canSync: () => boolean): () => void {
|
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
let running = false;
|
|
let pending = false;
|
|
let disposed = false;
|
|
const enabled = () => {
|
|
const s = useProject.getState();
|
|
return (
|
|
!disposed && canSync() && !s.opening && (s.project || s.lightEdit) && s.settings?.files?.syncOnFocus !== false
|
|
);
|
|
};
|
|
const onFocus = () => {
|
|
clearTimeout(timer);
|
|
if (!enabled()) return;
|
|
const { project, lightEdit } = useProject.getState();
|
|
timer = setTimeout(async () => {
|
|
if (!enabled() || useProject.getState().project !== project || useProject.getState().lightEdit !== lightEdit)
|
|
return;
|
|
if (running) {
|
|
pending = true;
|
|
return;
|
|
}
|
|
running = true;
|
|
try {
|
|
await sync();
|
|
if (enabled() && useProject.getState().project === project && !lightEdit) {
|
|
void executeAction("project.refresh");
|
|
void executeAction("vcs.refresh");
|
|
}
|
|
} catch (e) {
|
|
console.warn("Cannot synchronize external file changes", e);
|
|
} finally {
|
|
running = false;
|
|
if (pending) {
|
|
pending = false;
|
|
onFocus();
|
|
}
|
|
}
|
|
}, 200);
|
|
};
|
|
window.addEventListener("focus", onFocus);
|
|
return () => {
|
|
disposed = true;
|
|
clearTimeout(timer);
|
|
window.removeEventListener("focus", onFocus);
|
|
};
|
|
}
|