Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

watch: Debounce all files updates into a single event. #51988

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.#debounceOwners = new SafeSet();
this.#debounceOwners.clear();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I did that is to allow async processing of the owners by the listeners. I figured it would be more efficient to swap the Set rather than doing:

      const owners = new Set(this.#debounceOwners);
      this.emit('changed', { owners });
      this.#debounceOwners.clear();

What do you think ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you think that async processing of the event is not something we should care about here, then I can just implement your change.

}, this.#debounce).unref();
}

Expand Down
Loading