Skip to content

Commit ea51052

Browse files
committed
refactor: separate watcher updates from diagnostics scheduling
1 parent 97db972 commit ea51052

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

packages/svelte-check/src/index.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* This code's groundwork is taken from https://github.com/vuejs/vetur/tree/master/vti
33
*/
44

5-
import { watch } from 'chokidar';
5+
import { watch, FSWatcher } from 'chokidar';
66
import * as fs from 'fs';
77
import { fdir } from 'fdir';
88
import * as path from 'path';
@@ -148,9 +148,10 @@ const VITE_CONFIG_REGEX = /vite\.config\.(js|ts)\.timestamp-/;
148148

149149
class DiagnosticsWatcher {
150150
private updateDiagnostics: any;
151-
private watcher: any;
151+
private watcher: FSWatcher;
152152
private currentWatchedDirs = new Set<string>();
153153
private userIgnored: Array<(path: string) => boolean>;
154+
private pendingWatcherUpdate: any;
154155

155156
constructor(
156157
private workspaceUri: URI,
@@ -167,7 +168,8 @@ class DiagnosticsWatcher {
167168
if (
168169
path.includes('node_modules') ||
169170
path.includes('.git') ||
170-
(stats?.isFile() && (!FILE_ENDING_REGEX.test(path) || VITE_CONFIG_REGEX.test(path)))
171+
(stats?.isFile() &&
172+
(!FILE_ENDING_REGEX.test(path) || VITE_CONFIG_REGEX.test(path)))
171173
) {
172174
return true;
173175
}
@@ -198,11 +200,11 @@ class DiagnosticsWatcher {
198200
private async updateWatchedDirectories() {
199201
const watchDirs = await this.svelteCheck.getWatchDirectories();
200202
const dirsToWatch = watchDirs || [{ path: this.workspaceUri.fsPath, recursive: true }];
201-
const newDirs = new Set(dirsToWatch.map(d => d.path));
203+
const newDirs = new Set(dirsToWatch.map((d) => d.path));
202204

203205
// Fast diff: find directories to add and remove
204-
const toAdd = [...newDirs].filter(dir => !this.currentWatchedDirs.has(dir));
205-
const toRemove = [...this.currentWatchedDirs].filter(dir => !newDirs.has(dir));
206+
const toAdd = [...newDirs].filter((dir) => !this.currentWatchedDirs.has(dir));
207+
const toRemove = [...this.currentWatchedDirs].filter((dir) => !newDirs.has(dir));
206208

207209
// Add new directories
208210
if (toAdd.length > 0) {
@@ -242,15 +244,15 @@ class DiagnosticsWatcher {
242244
this.scheduleDiagnostics();
243245
}
244246

245-
scheduleDiagnostics(updateWatchers = false) {
247+
updateWatchers() {
248+
clearTimeout(this.pendingWatcherUpdate);
249+
this.pendingWatcherUpdate = setTimeout(() => this.updateWatchedDirectories(), 1000);
250+
}
251+
252+
scheduleDiagnostics() {
246253
clearTimeout(this.updateDiagnostics);
247254
this.updateDiagnostics = setTimeout(
248-
async () => {
249-
if (updateWatchers) {
250-
await this.updateWatchedDirectories();
251-
}
252-
getDiagnostics(this.workspaceUri, this.writer, this.svelteCheck);
253-
},
255+
() => getDiagnostics(this.workspaceUri, this.writer, this.svelteCheck),
254256
1000
255257
);
256258
}
@@ -301,7 +303,10 @@ parseOptions(async (opts) => {
301303
};
302304

303305
if (opts.watch) {
304-
svelteCheckOptions.onProjectReload = () => watcher.scheduleDiagnostics(true);
306+
svelteCheckOptions.onProjectReload = () => {
307+
watcher.updateWatchers();
308+
watcher.scheduleDiagnostics();
309+
};
305310
const watcher = new DiagnosticsWatcher(
306311
opts.workspaceUri,
307312
new SvelteCheck(opts.workspaceUri.fsPath, svelteCheckOptions),

0 commit comments

Comments
 (0)