Skip to content

Commit 521fa56

Browse files
watch: allow listening for grouped changes
1 parent 40ef9d5 commit 521fa56

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

lib/internal/watch_mode/files_watcher.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ const { addAbortListener } = require('internal/events/abort_listener');
1717
const { watch } = require('fs');
1818
const { fileURLToPath } = require('internal/url');
1919
const { resolve, dirname } = require('path');
20-
const { setTimeout } = require('timers');
20+
const { setTimeout, clearTimeout } = require('timers');
2121

2222
const supportsRecursiveWatching = process.platform === 'win32' ||
2323
process.platform === 'darwin';
2424

2525
class FilesWatcher extends EventEmitter {
2626
#watchers = new SafeMap();
2727
#filteredFiles = new SafeSet();
28-
#debouncing = new SafeSet();
2928
#depencencyOwners = new SafeMap();
3029
#ownerDependencies = new SafeMap();
30+
#debounceOwners = new SafeSet();
31+
#debounceTimer;
3132
#debounce;
3233
#mode;
3334
#signal;
@@ -75,17 +76,27 @@ class FilesWatcher extends EventEmitter {
7576
}
7677

7778
#onChange(trigger) {
78-
if (this.#debouncing.has(trigger)) {
79-
return;
80-
}
8179
if (this.#mode === 'filter' && !this.#filteredFiles.has(trigger)) {
8280
return;
8381
}
84-
this.#debouncing.add(trigger);
82+
8583
const owners = this.#depencencyOwners.get(trigger);
86-
setTimeout(() => {
87-
this.#debouncing.delete(trigger);
84+
if (owners) {
85+
for (const owner of owners) {
86+
this.#debounceOwners.add(owner);
87+
}
88+
}
89+
90+
clearTimeout(this.#debounceTimer);
91+
this.#debounceTimer = setTimeout(() => {
92+
this.#debounceTimer = null;
93+
// In order to allow async processing of the 'changed' event, let's
94+
// make sure the Set emitted is a new instance. We could emit using a copy
95+
// of #debounceOwners, then clear #debounceOwners, but that would be
96+
// slower than just replacing #debounceOwners with an empty Set.
97+
const owners = this.#debounceOwners;
8898
this.emit('changed', { owners });
99+
this.#debounceOwners = new SafeSet();
89100
}, this.#debounce).unref();
90101
}
91102

0 commit comments

Comments
 (0)