Skip to content

Commit ff2cdb4

Browse files
committedAug 6, 2020
improve how polling is scheduled to reduce load
1 parent eb608fa commit ff2cdb4

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed
 

‎lib/DirectoryWatcher.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class DirectoryWatcher extends EventEmitter {
7979
: options.poll
8080
? 5007
8181
: false;
82+
this.timeout = undefined;
8283
this.initialScanRemoved = new Set();
8384
this.initialScanFinished = undefined;
8485
/** @type {Map<string, Set<Watcher>>} */
@@ -104,13 +105,12 @@ class DirectoryWatcher extends EventEmitter {
104105
createWatcher() {
105106
try {
106107
if (this.polledWatching) {
107-
// Poll for changes
108-
const interval = setInterval(() => {
109-
this.doScan(false);
110-
}, this.polledWatching);
111108
this.watcher = {
112109
close: () => {
113-
clearInterval(interval);
110+
if (this.timeout) {
111+
clearTimeout(this.timeout);
112+
this.timeout = undefined;
113+
}
114114
}
115115
};
116116
} else {
@@ -466,6 +466,16 @@ class DirectoryWatcher extends EventEmitter {
466466
if (err) {
467467
console.error("Watchpack Error (initial scan): " + err);
468468
}
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+
}
469479
}
470480

471481
onDirectoryRemoved(reason) {
@@ -528,6 +538,10 @@ class DirectoryWatcher extends EventEmitter {
528538
return;
529539
}
530540
this.scanning = true;
541+
if (this.timeout) {
542+
clearTimeout(this.timeout);
543+
this.timeout = undefined;
544+
}
531545
process.nextTick(() => {
532546
if (this.closed) return;
533547
fs.readdir(this.path, (err, items) => {
@@ -606,6 +620,7 @@ class DirectoryWatcher extends EventEmitter {
606620
this.doScan(this.scanAgainInitial);
607621
} else {
608622
this.scanning = false;
623+
this.onScanFinished();
609624
}
610625
});
611626
for (const itemPath of itemPaths) {

0 commit comments

Comments
 (0)
Please sign in to comment.