@@ -79,6 +79,7 @@ class DirectoryWatcher extends EventEmitter {
79
79
: options . poll
80
80
? 5007
81
81
: false ;
82
+ this . timeout = undefined ;
82
83
this . initialScanRemoved = new Set ( ) ;
83
84
this . initialScanFinished = undefined ;
84
85
/** @type {Map<string, Set<Watcher>> } */
@@ -104,13 +105,12 @@ class DirectoryWatcher extends EventEmitter {
104
105
createWatcher ( ) {
105
106
try {
106
107
if ( this . polledWatching ) {
107
- // Poll for changes
108
- const interval = setInterval ( ( ) => {
109
- this . doScan ( false ) ;
110
- } , this . polledWatching ) ;
111
108
this . watcher = {
112
109
close : ( ) => {
113
- clearInterval ( interval ) ;
110
+ if ( this . timeout ) {
111
+ clearTimeout ( this . timeout ) ;
112
+ this . timeout = undefined ;
113
+ }
114
114
}
115
115
} ;
116
116
} else {
@@ -466,6 +466,16 @@ class DirectoryWatcher extends EventEmitter {
466
466
if ( err ) {
467
467
console . error ( "Watchpack Error (initial scan): " + err ) ;
468
468
}
469
+ this . onScanFinished ( ) ;
470
+ }
471
+
472
+ onScanFinished ( ) {
473
+ if ( this . polledWatching ) {
474
+ this . timeout = setTimeout ( ( ) => {
475
+ if ( this . closed ) return ;
476
+ this . doScan ( false ) ;
477
+ } , this . polledWatching ) ;
478
+ }
469
479
}
470
480
471
481
onDirectoryRemoved ( reason ) {
@@ -528,6 +538,10 @@ class DirectoryWatcher extends EventEmitter {
528
538
return ;
529
539
}
530
540
this . scanning = true ;
541
+ if ( this . timeout ) {
542
+ clearTimeout ( this . timeout ) ;
543
+ this . timeout = undefined ;
544
+ }
531
545
process . nextTick ( ( ) => {
532
546
if ( this . closed ) return ;
533
547
fs . readdir ( this . path , ( err , items ) => {
@@ -606,6 +620,7 @@ class DirectoryWatcher extends EventEmitter {
606
620
this . doScan ( this . scanAgainInitial ) ;
607
621
} else {
608
622
this . scanning = false ;
623
+ this . onScanFinished ( ) ;
609
624
}
610
625
} ) ;
611
626
for ( const itemPath of itemPaths ) {
0 commit comments