Skip to content

Commit

Permalink
watch: add global debounce
Browse files Browse the repository at this point in the history
Co-authored-by: Matthieu <matthieusieben@users.noreply.github.com>
  • Loading branch information
MoLow and matthieusieben committed Mar 6, 2024
1 parent 384fd17 commit 57ac709
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/internal/watch_mode/files_watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
SafeMap,
SafeSet,
StringPrototypeStartsWith,
Symbol,
} = primordials;

const { validateNumber, validateOneOf } = require('internal/validators');
Expand All @@ -21,6 +22,7 @@ const { setTimeout } = require('timers');
const supportsRecursiveWatching = process.platform === 'win32' ||
process.platform === 'darwin';

const kGlobalDebounce = Symbol('kGlobalDebounce');
class FilesWatcher extends EventEmitter {
#watchers = new SafeMap();
#filteredFiles = new SafeSet();
Expand Down Expand Up @@ -74,16 +76,17 @@ class FilesWatcher extends EventEmitter {
}

#onChange(trigger) {
if (this.#debouncing.has(trigger)) {
if (this.#debouncing.has(trigger) || this.#debouncing.has(kGlobalDebounce)) {
return;
}
if (this.#mode === 'filter' && !this.#filteredFiles.has(trigger)) {
return;
}
this.#debouncing.add(trigger);
this.#debouncing.add(trigger).add(kGlobalDebounce);
const owners = this.#depencencyOwners.get(trigger);
setTimeout(() => {
this.#debouncing.delete(trigger);
this.#debouncing.delete(kGlobalDebounce);
this.emit('changed', { owners });
}, this.#debounce).unref();
}
Expand Down

0 comments on commit 57ac709

Please sign in to comment.