@@ -157,7 +157,8 @@ export class ParcelWatcher extends BaseWatcher implements IRecursiveWatcherWithS
157
157
private readonly _onDidError = this . _register ( new Emitter < IWatcherErrorEvent > ( ) ) ;
158
158
readonly onDidError = this . _onDidError . event ;
159
159
160
- readonly watchers = new Set < ParcelWatcherInstance > ( ) ;
160
+ private readonly _watchers = new Map < string /* path */ | number /* correlation ID */ , ParcelWatcherInstance > ( ) ;
161
+ get watchers ( ) { return this . _watchers . values ( ) ; }
161
162
162
163
// A delay for collecting file changes from Parcel
163
164
// before collecting them for coalescing and emitting.
@@ -206,7 +207,7 @@ export class ParcelWatcher extends BaseWatcher implements IRecursiveWatcherWithS
206
207
const requestsToStart : IRecursiveWatchRequest [ ] = [ ] ;
207
208
const watchersToStop = new Set ( Array . from ( this . watchers ) ) ;
208
209
for ( const request of requests ) {
209
- const watcher = this . findWatcher ( request ) ;
210
+ const watcher = this . _watchers . get ( this . requestToWatcherKey ( request ) ) ;
210
211
if ( watcher && patternsEquals ( watcher . request . excludes , request . excludes ) && patternsEquals ( watcher . request . includes , request . includes ) && watcher . request . pollingInterval === request . pollingInterval ) {
211
212
watchersToStop . delete ( watcher ) ; // keep watcher
212
213
} else {
@@ -238,25 +239,12 @@ export class ParcelWatcher extends BaseWatcher implements IRecursiveWatcherWithS
238
239
}
239
240
}
240
241
241
- private findWatcher ( request : IRecursiveWatchRequest ) : ParcelWatcherInstance | undefined {
242
- for ( const watcher of this . watchers ) {
243
-
244
- // Requests or watchers with correlation always match on that
245
- if ( this . isCorrelated ( request ) || this . isCorrelated ( watcher . request ) ) {
246
- if ( watcher . request . correlationId === request . correlationId ) {
247
- return watcher ;
248
- }
249
- }
250
-
251
- // Non-correlated requests or watchers match on path
252
- else {
253
- if ( isEqual ( watcher . request . path , request . path , ! isLinux /* ignorecase */ ) ) {
254
- return watcher ;
255
- }
256
- }
257
- }
242
+ private requestToWatcherKey ( request : IRecursiveWatchRequest ) : string | number {
243
+ return typeof request . correlationId === 'number' ? request . correlationId : this . pathToWatcherKey ( request . path ) ;
244
+ }
258
245
259
- return undefined ;
246
+ private pathToWatcherKey ( path : string ) : string {
247
+ return isLinux ? path : path . toLowerCase ( ) /* ignore path casing */ ;
260
248
}
261
249
262
250
private startPolling ( request : IRecursiveWatchRequest , pollingInterval : number , restarts = 0 ) : void {
@@ -283,7 +271,7 @@ export class ParcelWatcher extends BaseWatcher implements IRecursiveWatcherWithS
283
271
unlinkSync ( snapshotFile ) ;
284
272
}
285
273
) ;
286
- this . watchers . add ( watcher ) ;
274
+ this . _watchers . set ( this . requestToWatcherKey ( request ) , watcher ) ;
287
275
288
276
// Path checks for symbolic links / wrong casing
289
277
const { realPath, realPathDiffers, realPathLength } = this . normalizePath ( request ) ;
@@ -352,7 +340,7 @@ export class ParcelWatcher extends BaseWatcher implements IRecursiveWatcherWithS
352
340
await watcherInstance ?. unsubscribe ( ) ;
353
341
}
354
342
) ;
355
- this . watchers . add ( watcher ) ;
343
+ this . _watchers . set ( this . requestToWatcherKey ( request ) , watcher ) ;
356
344
357
345
// Path checks for symbolic links / wrong casing
358
346
const { realPath, realPathDiffers, realPathLength } = this . normalizePath ( request ) ;
@@ -643,7 +631,7 @@ export class ParcelWatcher extends BaseWatcher implements IRecursiveWatcherWithS
643
631
private async stopWatching ( watcher : ParcelWatcherInstance , joinRestart ?: Promise < void > ) : Promise < void > {
644
632
this . trace ( `stopping file watcher` , watcher ) ;
645
633
646
- this . watchers . delete ( watcher ) ;
634
+ this . _watchers . delete ( this . requestToWatcherKey ( watcher . request ) ) ;
647
635
648
636
try {
649
637
await watcher . stop ( joinRestart ) ;
@@ -666,14 +654,14 @@ export class ParcelWatcher extends BaseWatcher implements IRecursiveWatcherWithS
666
654
continue ; // path is ignored entirely (via `**` glob exclude)
667
655
}
668
656
669
- const path = isLinux ? request . path : request . path . toLowerCase ( ) ; // adjust for case sensitivity
670
657
671
658
let requestsForCorrelation = mapCorrelationtoRequests . get ( request . correlationId ) ;
672
659
if ( ! requestsForCorrelation ) {
673
660
requestsForCorrelation = new Map < string , IRecursiveWatchRequest > ( ) ;
674
661
mapCorrelationtoRequests . set ( request . correlationId , requestsForCorrelation ) ;
675
662
}
676
663
664
+ const path = this . pathToWatcherKey ( request . path ) ;
677
665
if ( requestsForCorrelation . has ( path ) ) {
678
666
this . trace ( `ignoring a request for watching who's path is already watched: ${ this . requestToString ( request ) } ` ) ;
679
667
}
0 commit comments