2
2
* This code's groundwork is taken from https://github.com/vuejs/vetur/tree/master/vti
3
3
*/
4
4
5
- import { watch } from 'chokidar' ;
5
+ import { watch , FSWatcher } from 'chokidar' ;
6
6
import * as fs from 'fs' ;
7
7
import { fdir } from 'fdir' ;
8
8
import * as path from 'path' ;
@@ -148,9 +148,10 @@ const VITE_CONFIG_REGEX = /vite\.config\.(js|ts)\.timestamp-/;
148
148
149
149
class DiagnosticsWatcher {
150
150
private updateDiagnostics : any ;
151
- private watcher : any ;
151
+ private watcher : FSWatcher ;
152
152
private currentWatchedDirs = new Set < string > ( ) ;
153
153
private userIgnored : Array < ( path : string ) => boolean > ;
154
+ private pendingWatcherUpdate : any ;
154
155
155
156
constructor (
156
157
private workspaceUri : URI ,
@@ -167,7 +168,8 @@ class DiagnosticsWatcher {
167
168
if (
168
169
path . includes ( 'node_modules' ) ||
169
170
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 ) ) )
171
173
) {
172
174
return true ;
173
175
}
@@ -198,11 +200,11 @@ class DiagnosticsWatcher {
198
200
private async updateWatchedDirectories ( ) {
199
201
const watchDirs = await this . svelteCheck . getWatchDirectories ( ) ;
200
202
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 ) ) ;
202
204
203
205
// 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 ) ) ;
206
208
207
209
// Add new directories
208
210
if ( toAdd . length > 0 ) {
@@ -242,15 +244,15 @@ class DiagnosticsWatcher {
242
244
this . scheduleDiagnostics ( ) ;
243
245
}
244
246
245
- scheduleDiagnostics ( updateWatchers = false ) {
247
+ updateWatchers ( ) {
248
+ clearTimeout ( this . pendingWatcherUpdate ) ;
249
+ this . pendingWatcherUpdate = setTimeout ( ( ) => this . updateWatchedDirectories ( ) , 1000 ) ;
250
+ }
251
+
252
+ scheduleDiagnostics ( ) {
246
253
clearTimeout ( this . updateDiagnostics ) ;
247
254
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 ) ,
254
256
1000
255
257
) ;
256
258
}
@@ -301,7 +303,10 @@ parseOptions(async (opts) => {
301
303
} ;
302
304
303
305
if ( opts . watch ) {
304
- svelteCheckOptions . onProjectReload = ( ) => watcher . scheduleDiagnostics ( true ) ;
306
+ svelteCheckOptions . onProjectReload = ( ) => {
307
+ watcher . updateWatchers ( ) ;
308
+ watcher . scheduleDiagnostics ( ) ;
309
+ } ;
305
310
const watcher = new DiagnosticsWatcher (
306
311
opts . workspaceUri ,
307
312
new SvelteCheck ( opts . workspaceUri . fsPath , svelteCheckOptions ) ,
0 commit comments