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 61eb26c
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 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,22 @@ 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);
this.emit('changed', { owners });
if (owners) {
for (const owner of owners) {
this.#debounceOwners.add(owner);
}
}

clearTimeout(this.#debounceTimer);
this.#debounceTimer = setTimeout(() => {
this.#debounceTimer = null;
this.emit('changed', { owners: this.#debounceOwners });
this.#debounceOwners.clear();
}, this.#debounce).unref();
}

Expand Down

0 comments on commit 61eb26c

Please sign in to comment.