Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions apps/server/src/wsServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -855,26 +855,35 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<

let fileTreeDebounceTimer: ReturnType<typeof setTimeout> | null = null;

const fileTreeWatcher = fs.watch(cwd, { recursive: true }, (_eventType, filename) => {
if (!filename) return;

// Ignore changes inside noisy directories
const normalized = String(filename).replaceAll("\\", "/");
const firstSegment = normalized.split("/")[0];
if (firstSegment && IGNORED_WATCHER_DIRS.has(firstSegment)) return;

// Debounce rapid consecutive changes into a single push
if (fileTreeDebounceTimer) clearTimeout(fileTreeDebounceTimer);
fileTreeDebounceTimer = setTimeout(() => {
fileTreeDebounceTimer = null;
clearWorkspaceIndexCache(cwd);
void Effect.runPromise(pushBus.publishAll(WS_CHANNELS.projectFileTreeChanged, { cwd }));
}, FILE_TREE_DEBOUNCE_MS);
// fs.watch throws ENOENT when the directory does not exist (e.g. in tests
// or when a workspace path has been removed). Guard against this so the
// server can still start up.
const fileTreeWatcher = yield* Effect.sync(() => {
try {
return fs.watch(cwd, { recursive: true }, (_eventType, filename) => {
if (!filename) return;

// Ignore changes inside noisy directories
const normalized = String(filename).replaceAll("\\", "/");
const firstSegment = normalized.split("/")[0];
if (firstSegment && IGNORED_WATCHER_DIRS.has(firstSegment)) return;

// Debounce rapid consecutive changes into a single push
if (fileTreeDebounceTimer) clearTimeout(fileTreeDebounceTimer);
fileTreeDebounceTimer = setTimeout(() => {
fileTreeDebounceTimer = null;
clearWorkspaceIndexCache(cwd);
void Effect.runPromise(pushBus.publishAll(WS_CHANNELS.projectFileTreeChanged, { cwd }));
}, FILE_TREE_DEBOUNCE_MS);
});
} catch {
return undefined;
}
});

yield* Effect.addFinalizer(() =>
Effect.sync(() => {
fileTreeWatcher.close();
fileTreeWatcher?.close();
if (fileTreeDebounceTimer) clearTimeout(fileTreeDebounceTimer);
}),
);
Expand Down
10 changes: 5 additions & 5 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading