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: add global debounce #51986

Closed
wants to merge 1 commit into from
Closed

Conversation

MoLow
Copy link
Member

@MoLow MoLow commented Mar 6, 2024

Fixes #51954

@MoLow MoLow requested review from benjamingr and atlowChemi March 6, 2024 13:29
@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label Mar 6, 2024
@MoLow MoLow force-pushed the watch-mode-global-debounce branch 5 times, most recently from 771ed41 to d274f34 Compare March 6, 2024 13:41
@MoLow MoLow force-pushed the watch-mode-global-debounce branch from d274f34 to 57ac709 Compare March 6, 2024 13:43
@matthieusieben
Copy link
Contributor

matthieusieben commented Mar 6, 2024

These changes change the behavior of the class and might breaks its uses (1). They also fails to address one of the issues linked to the restart in watch mode (2).

  1. This line will prevent the changed event to be triggered twice if two files are changed simultaneously.
    https://github.com/nodejs/node/pull/51986/files#diff-30ce5ad0c669a2ea6eef90f95d1f70cbf58f4b3063ff61d2498e6db30b30881bR79

  2. In watch mode, the restart function will be called as many times as the changed event is triggered by the FileWatcher. Adding a globally debounced eventcthat does not affect current behaviour can be done like so main...matthieusieben:node:patch-3
    However, having the restart function called as many times as a changed occurs is sub optimal (even the way it is done in the previous patch). Indeed, imagine that several changed occurs while the application restarts. In that case, the application will be restarted as many times as the changed occurred, while only restarting once would have been enough.
    Using an iterable as in watch: debounce restart in watch mode #51983 does account for this. Using the debounced iterator will ensure that both frequent updates as well as updates happening before the next() item is requested are all bundled together, leading to fewer restarts.

@MoLow
Copy link
Member Author

MoLow commented Mar 6, 2024

This line will prevent the changed event to be triggered twice if two files are changed simultaneously.

this is the goal. I am a bit confused do want debouncing or not?

Adding a globally debounced eventcthat does not affect current behaviour can be done like so

Not sure why you want to avoid effecting current behavior?

@matthieusieben
Copy link
Contributor

Not sure why you want to avoid effecting current behavior?

Because of other uses of this class, like here

@MoLow
Copy link
Member Author

MoLow commented Mar 6, 2024

Not sure why you want to avoid effecting current behavior?

Because of other uses of this class, like here

that is watch mode for node --test it should be fixed as well

@@ -74,16 +76,17 @@ class FilesWatcher extends EventEmitter {
}

#onChange(trigger) {
if (this.#debouncing.has(trigger)) {
if (this.#debouncing.has(trigger) || this.#debouncing.has(kGlobalDebounce)) {
Copy link
Member

Choose a reason for hiding this comment

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

We always set the 2 of these together, can't we simplify by only setting & checking for the kGlobalDebounce? (and drop the .add(trigger) as well)

Copy link
Contributor

Choose a reason for hiding this comment

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

Piggy backing on this comment to emphasis that indeed, with this implementation, the second part of the if will always be true if any other file already triggered as #onChange. This not only renders the use of the this.#debouncing set unnecessary (a simple boolean would suffice), but it also means that the owners argument of the event will no longer contain all the owners. Indeed, a change to file A would cause kGlobalDebounce to be set so that any change to file B would get ignored by this if condition.

@matthieusieben
Copy link
Contributor

matthieusieben commented Mar 6, 2024

that is watch mode for node --test it should be fixed as well

right but that implementation depends on the owners argument, which will not contain all the owners if some events are skipped.

@matthieusieben
Copy link
Contributor

matthieusieben commented Mar 6, 2024

Something like this might not break the current behaviour.

main...matthieusieben:node:patch-4 (#51988)

This would still cause too many restarts in case of --watch (as explained here) but it would better debounce events for the node --test case.

@MoLow
Copy link
Member Author

MoLow commented Mar 6, 2024

Superseded by #51992

@MoLow MoLow closed this Mar 6, 2024
@MoLow MoLow deleted the watch-mode-global-debounce branch March 6, 2024 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

watch should debounce the restart
4 participants