@@ -2,12 +2,10 @@ import paths from 'path';
22
33import ParcelWatcher from '@parcel/watcher' ;
44import fs from 'fs-extra' ;
5- import debounce from 'lodash/debounce' ;
65import uniqBy from 'lodash/uniqBy' ;
76
87import { Autowired , Injectable , Optional } from '@opensumi/di' ;
98import {
10- DefaultMap ,
119 Disposable ,
1210 DisposableCollection ,
1311 FileUri ,
@@ -24,9 +22,9 @@ import {
2422 sleep ,
2523} from '@opensumi/ide-core-node' ;
2624
27- import { FileChangeType , FileSystemWatcherClient , IFileSystemWatcherServer , INsfw , WatchOptions } from '../../common' ;
28- import { WatchInsData , fileChangeEvent } from '../data-store' ;
29- import { FileChangeCollection } from '../file-change-collection' ;
25+ import { FileSystemWatcherClient , IFileSystemWatcherServer , INsfw , WatchOptions } from '../../common' ;
26+ import { WatchInsData } from '../data-store' ;
27+ import { FileChangeCollectionManager } from '../file-change-collection' ;
3028import { shouldIgnorePath } from '../shared' ;
3129
3230export interface WatcherOptions {
@@ -50,14 +48,15 @@ export class FileSystemWatcherServer extends Disposable implements IFileSystemWa
5048 private static WATCHER_SEQUENCE = 1 ;
5149 protected watcherOptions = new Map < number , WatcherOptions > ( ) ;
5250
53- protected changes = new DefaultMap < number , FileChangeCollection > ( ( ) => new FileChangeCollection ( ) ) ;
54-
5551 @Autowired ( ILogServiceManager )
5652 private readonly loggerManager : ILogServiceManager ;
5753
5854 @GDataStore ( WatchInsData , { id : 'watcherId' } )
5955 private watcherGDataStore : GDataStore < WatchInsData , 'watcherId' > ;
6056
57+ @Autowired ( FileChangeCollectionManager )
58+ private readonly fileChangeCollectionManager : FileChangeCollectionManager ;
59+
6160 private logger : ILogService ;
6261
6362 constructor ( @Optional ( ) private readonly excludes : string [ ] = [ ] ) {
@@ -201,13 +200,13 @@ export class FileSystemWatcherServer extends Disposable implements IFileSystemWa
201200 for ( const event of events ) {
202201 switch ( event . type ) {
203202 case 'create' :
204- this . pushAdded ( watcherId , event . path ) ;
203+ this . fileChangeCollectionManager . pushAdded ( watcherId , event . path ) ;
205204 break ;
206205 case 'delete' :
207- this . pushDeleted ( watcherId , event . path ) ;
206+ this . fileChangeCollectionManager . pushDeleted ( watcherId , event . path ) ;
208207 break ;
209208 case 'update' :
210- this . pushUpdated ( watcherId , event . path ) ;
209+ this . fileChangeCollectionManager . pushUpdated ( watcherId , event . path ) ;
211210 break ;
212211 }
213212 }
@@ -361,22 +360,22 @@ export class FileSystemWatcherServer extends Disposable implements IFileSystemWa
361360 return ;
362361 }
363362
364- this . pushDeleted ( watcherId , deletedPath ) ;
363+ this . fileChangeCollectionManager . pushDeleted ( watcherId , deletedPath ) ;
365364
366365 if ( event . newDirectory ) {
367366 const path = await this . resolvePath ( event . newDirectory , event . newFile ! ) ;
368367 if ( isIgnored ( watcherId , path ) ) {
369368 return ;
370369 }
371370
372- this . pushAdded ( watcherId , path ) ;
371+ this . fileChangeCollectionManager . pushAdded ( watcherId , path ) ;
373372 } else {
374373 const path = await this . resolvePath ( event . directory , event . newFile ! ) ;
375374 if ( isIgnored ( watcherId , path ) ) {
376375 return ;
377376 }
378377
379- this . pushAdded ( watcherId , path ) ;
378+ this . fileChangeCollectionManager . pushAdded ( watcherId , path ) ;
380379 }
381380 }
382381 break ;
@@ -389,13 +388,13 @@ export class FileSystemWatcherServer extends Disposable implements IFileSystemWa
389388
390389 switch ( event . action ) {
391390 case INsfw . actions . CREATED :
392- this . pushAdded ( watcherId , path ) ;
391+ this . fileChangeCollectionManager . pushAdded ( watcherId , path ) ;
393392 break ;
394393 case INsfw . actions . DELETED :
395- this . pushDeleted ( watcherId , path ) ;
394+ this . fileChangeCollectionManager . pushDeleted ( watcherId , path ) ;
396395 break ;
397396 case INsfw . actions . MODIFIED :
398- this . pushUpdated ( watcherId , path ) ;
397+ this . fileChangeCollectionManager . pushUpdated ( watcherId , path ) ;
399398 break ;
400399 }
401400 }
@@ -405,25 +404,6 @@ export class FileSystemWatcherServer extends Disposable implements IFileSystemWa
405404 ) ;
406405 }
407406
408- protected pushAdded ( watcherId : number , path : string ) : void {
409- this . pushFileChange ( watcherId , path , FileChangeType . ADDED ) ;
410- }
411-
412- protected pushUpdated ( watcherId : number , path : string ) : void {
413- this . pushFileChange ( watcherId , path , FileChangeType . UPDATED ) ;
414- }
415-
416- protected pushDeleted ( watcherId : number , path : string ) : void {
417- this . pushFileChange ( watcherId , path , FileChangeType . DELETED ) ;
418- }
419-
420- protected pushFileChange ( watcherId : number , path : string , type : FileChangeType ) : void {
421- const uri = FileUri . create ( path ) . toString ( ) ;
422- this . changes . get ( watcherId ) . push ( { uri, type } ) ;
423-
424- this . fireDidFilesChanged ( ) ;
425- }
426-
427407 protected async resolvePath ( directory : string , file : string ) : Promise < string > {
428408 const path = paths . join ( directory , file ) ;
429409 // 如果是 linux 则获取一下真实 path,以防返回的是软连路径被过滤
@@ -442,18 +422,6 @@ export class FileSystemWatcherServer extends Disposable implements IFileSystemWa
442422 }
443423 return path ;
444424 }
445-
446- /**
447- * Fires file changes to clients.
448- * It is debounced in the case if the filesystem is spamming to avoid overwhelming clients with events.
449- */
450- protected readonly fireDidFilesChanged : ( ) => void = debounce ( ( ) => this . doFireDidFilesChanged ( ) , 100 ) ;
451- protected doFireDidFilesChanged ( ) : void {
452- this . changes . forEach ( ( change , watcherId ) => {
453- const data = change . values ( ) ;
454- this . watcherGDataStore . emit ( fileChangeEvent ( watcherId ) , data ) ;
455- } ) ;
456- }
457425}
458426
459427function requireNSFWModule ( ) : typeof import ( 'nsfw' ) {
0 commit comments