fix: repair broken IPC wiring and cut the cost of repository polling
Three handlers referenced a dependency they were never given, which made them throw a ReferenceError as soon as they ran: - deployment:preflight for Gitea Actions profiles (`preflight` was passed to registerOperationsIpc but not to registerDeploymentIpc) - Unraid write-access repair (`safeRelativeRemoteFile` was missing from createUnraidAccessMethods) - a dead reference of the same name in unraid-state-methods no-undef and no-unused-vars were disabled for every file, which is why none of these were caught. Both are now enabled for src/main and src/shared, where the dependency graph is explicit. The renderer keeps them off because its functions are deliberately cross-script globals. Performance: - git.status() spawned three processes (rev-parse, status, remote get-url) per call. A directory holding its own .git is by definition the work tree root, so rev-parse is unnecessary, and the remote URL is cached against the mtime of .git/config, including the failure for a repository without that remote. - git status runs with --no-optional-locks so a read no longer rewrites the index. That stops it fighting a concurrent Git command for the index lock, and is what makes filesystem watching viable at all. - One commit issued four `git status` reads; callers that already hold the status now pass it on, leaving two. - The repository monitor is event driven. A watched repository is read on filesystem activity, with a 30s safety net for watchers that stop delivering and a 1s floor so a busy tree cannot drive a read per event. Repositories that cannot be watched keep using the interval. Idle cost for one repository over 35s: 24 git processes before, 3 after. - Resolving one repository by name no longer refreshes the whole workspace. - Concurrent configuration saves share a single write of the latest state. - Repository discovery follows directory junctions again. The filter that skipped them made the realpath cycle guard dead code, and hid any project folder reached through a junction. Renderer: - render() replaced the whole shell on every poll, discarding focus, caret and scroll position while the user was typing. Those are preserved now, and an unchanged render leaves the DOM alone entirely. - The four sections that enhanceRenderedUi() injected after render moved into the views, so the rendered markup is the single source of truth. - The monitor no longer keeps a repository paused forever when it is unlinked mid-mutation, scheduleAutoRefresh honours its delay argument, the demo bridges no longer block startup, and #app is no longer an aria-live region announcing the entire UI on every render. IPC channel plumbing moved to src/main/ipc/channel.cjs, replacing a module-level mutable diagnostics singleton with an argument. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
cf1da8a2fa
commit
9260d35957
@@ -1,5 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('node:fs');
|
||||
|
||||
// A watched repository is only re-read when the filesystem reports activity. The
|
||||
// interval below stays as a safety net for watchers that silently stop
|
||||
// delivering, which happens on network shares and removed folders.
|
||||
const SAFETY_CHECK_INTERVAL_MS = 30_000;
|
||||
const WATCH_DEBOUNCE_MS = 250;
|
||||
// Busy trees (a build, an install, a fetch) produce a continuous event stream.
|
||||
// This bounds how often that can turn into a Git read.
|
||||
const MIN_WATCH_CHECK_INTERVAL_MS = 1_000;
|
||||
|
||||
class RepositoryMonitor {
|
||||
constructor({ store, git, onChange, diagnostics = null }) {
|
||||
this.store = store;
|
||||
@@ -11,13 +22,87 @@ class RepositoryMonitor {
|
||||
this.timer = null;
|
||||
this.running = false;
|
||||
this.paused = new Set();
|
||||
this.active = false;
|
||||
this.watchers = new Map();
|
||||
this.changed = new Set();
|
||||
this.lastCheckedAt = new Map();
|
||||
this.watchTimer = null;
|
||||
}
|
||||
|
||||
setPaths(paths) {
|
||||
this.paths = [...new Set((paths || []).filter(Boolean))];
|
||||
const watched = new Set(this.paths);
|
||||
for (const existing of [...this.fingerprints.keys()]) {
|
||||
if (!this.paths.includes(existing)) this.fingerprints.delete(existing);
|
||||
if (!watched.has(existing)) this.fingerprints.delete(existing);
|
||||
}
|
||||
// A repository that is unlinked while a mutation holds it paused would keep
|
||||
// that pause forever, silently freezing its status once it is watched again.
|
||||
for (const existing of [...this.paused]) {
|
||||
if (!watched.has(existing)) this.paused.delete(existing);
|
||||
}
|
||||
for (const existing of [...this.changed]) {
|
||||
if (!watched.has(existing)) this.changed.delete(existing);
|
||||
}
|
||||
for (const existing of [...this.lastCheckedAt.keys()]) {
|
||||
if (!watched.has(existing)) this.lastCheckedAt.delete(existing);
|
||||
}
|
||||
this.syncWatchers();
|
||||
}
|
||||
|
||||
syncWatchers() {
|
||||
for (const [localPath, watcher] of [...this.watchers]) {
|
||||
if (this.active && this.paths.includes(localPath)) continue;
|
||||
this.closeWatcher(localPath, watcher);
|
||||
}
|
||||
if (!this.active) return;
|
||||
for (const localPath of this.paths) {
|
||||
if (this.watchers.has(localPath)) continue;
|
||||
try {
|
||||
const watcher = fs.watch(
|
||||
localPath,
|
||||
{ recursive: true, persistent: false },
|
||||
() => this.noteFilesystemChange(localPath)
|
||||
);
|
||||
watcher.on('error', () => this.dropWatcher(localPath));
|
||||
this.watchers.set(localPath, watcher);
|
||||
} catch {
|
||||
// Watching is unavailable for this folder. Leaving it unwatched makes
|
||||
// shouldCheck() fall back to the interval for that repository only.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
closeWatcher(localPath, watcher = this.watchers.get(localPath)) {
|
||||
if (!watcher) return;
|
||||
try { watcher.close(); } catch { /* already closed */ }
|
||||
this.watchers.delete(localPath);
|
||||
}
|
||||
|
||||
dropWatcher(localPath) {
|
||||
this.closeWatcher(localPath);
|
||||
this.changed.add(localPath);
|
||||
}
|
||||
|
||||
noteFilesystemChange(localPath) {
|
||||
this.changed.add(localPath);
|
||||
this.scheduleWatchTick();
|
||||
}
|
||||
|
||||
scheduleWatchTick() {
|
||||
if (this.watchTimer) return;
|
||||
this.watchTimer = setTimeout(() => {
|
||||
this.watchTimer = null;
|
||||
this.tick().catch((error) => this.diagnostics?.warning('repository-monitor.tick.failed', error));
|
||||
}, WATCH_DEBOUNCE_MS);
|
||||
this.watchTimer.unref?.();
|
||||
}
|
||||
|
||||
shouldCheck(localPath, now) {
|
||||
if (this.paused.has(localPath)) return false;
|
||||
if (!this.watchers.has(localPath)) return true;
|
||||
const sinceLastCheck = now - (this.lastCheckedAt.get(localPath) || 0);
|
||||
if (this.changed.has(localPath)) return sinceLastCheck >= MIN_WATCH_CHECK_INTERVAL_MS;
|
||||
return sinceLastCheck >= SAFETY_CHECK_INTERVAL_MS;
|
||||
}
|
||||
|
||||
pause(localPath) { if (localPath) this.paused.add(localPath); }
|
||||
@@ -26,6 +111,8 @@ class RepositoryMonitor {
|
||||
restart() {
|
||||
this.stop();
|
||||
if (!this.store.data.preferences.autoRefresh) return;
|
||||
this.active = true;
|
||||
this.syncWatchers();
|
||||
const seconds = Math.min(Math.max(Number(this.store.data.preferences.repositoryPollSeconds) || 4, 2), 60);
|
||||
this.timer = setInterval(() => this.tick().catch((error) => this.diagnostics?.warning('repository-monitor.tick.failed', error)), seconds * 1000);
|
||||
this.timer.unref?.();
|
||||
@@ -34,17 +121,23 @@ class RepositoryMonitor {
|
||||
stop() {
|
||||
if (this.timer) clearInterval(this.timer);
|
||||
this.timer = null;
|
||||
if (this.watchTimer) clearTimeout(this.watchTimer);
|
||||
this.watchTimer = null;
|
||||
this.active = false;
|
||||
this.syncWatchers();
|
||||
}
|
||||
|
||||
async tick() {
|
||||
if (this.running || !this.paths.length) return;
|
||||
this.running = true;
|
||||
try {
|
||||
const queue = [...this.paths];
|
||||
const now = Date.now();
|
||||
const queue = this.paths.filter((localPath) => this.shouldCheck(localPath, now));
|
||||
const workers = Array.from({ length: Math.min(4, queue.length) }, async () => {
|
||||
while (queue.length) {
|
||||
const localPath = queue.shift();
|
||||
if (this.paused.has(localPath)) continue;
|
||||
this.changed.delete(localPath);
|
||||
this.lastCheckedAt.set(localPath, Date.now());
|
||||
try {
|
||||
const status = await this.git.status(localPath);
|
||||
const next = this.git.statusFingerprint(status);
|
||||
@@ -68,8 +161,11 @@ class RepositoryMonitor {
|
||||
await Promise.all(workers);
|
||||
} finally {
|
||||
this.running = false;
|
||||
// Activity that arrived while the check was running keeps its flag set, so
|
||||
// it must not wait for the safety interval.
|
||||
if (this.active && this.changed.size) this.scheduleWatchTick();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { RepositoryMonitor };
|
||||
module.exports = { RepositoryMonitor, SAFETY_CHECK_INTERVAL_MS, WATCH_DEBOUNCE_MS, MIN_WATCH_CHECK_INTERVAL_MS };
|
||||
|
||||
Reference in New Issue
Block a user