Skip to content

Commit

Permalink
watch: allow listening for grouped changes
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieusieben committed Apr 27, 2024
1 parent 40ef9d5 commit 521fa56
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions lib/internal/watch_mode/files_watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ const { addAbortListener } = require('internal/events/abort_listener');
const { watch } = require('fs');
const { fileURLToPath } = require('internal/url');
const { resolve, dirname } = require('path');
const { setTimeout } = require('timers');
const { setTimeout, clearTimeout } = require('timers');

const supportsRecursiveWatching = process.platform === 'win32' ||
process.platform === 'darwin';

class FilesWatcher extends EventEmitter {
#watchers = new SafeMap();
#filteredFiles = new SafeSet();
#debouncing = new SafeSet();
#depencencyOwners = new SafeMap();
#ownerDependencies = new SafeMap();
#debounceOwners = new SafeSet();
#debounceTimer;
#debounce;
#mode;
#signal;
Expand Down Expand Up @@ -75,17 +76,27 @@ class FilesWatcher extends EventEmitter {
}

#onChange(trigger) {
if (this.#debouncing.has(trigger)) {
return;
}
if (this.#mode === 'filter' && !this.#filteredFiles.has(trigger)) {
return;
}
this.#debouncing.add(trigger);

const owners = this.#depencencyOwners.get(trigger);
setTimeout(() => {
this.#debouncing.delete(trigger);
if (owners) {
for (const owner of owners) {
this.#debounceOwners.add(owner);
}
}

clearTimeout(this.#debounceTimer);
this.#debounceTimer = setTimeout(() => {
this.#debounceTimer = null;
// In order to allow async processing of the 'changed' event, let's
// make sure the Set emitted is a new instance. We could emit using a copy
// of #debounceOwners, then clear #debounceOwners, but that would be
// slower than just replacing #debounceOwners with an empty Set.
const owners = this.#debounceOwners;
this.emit('changed', { owners });
this.#debounceOwners = new SafeSet();
}, this.#debounce).unref();
}

Expand Down

0 comments on commit 521fa56

Please sign in to comment.