GeezerVison is showing github.com — watch: use debounce instead of throttle · nodejs/node@83a6d20
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/main/watch_mode.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,7 +42,7 @@ const args = ArrayPrototypeFilter(process.execArgv, (arg, i, arr) => | 42 | 42 | arg !== '--watch' && arg !== '--watch-preserve-output'); | 43 | 43 | ArrayPrototypePushApply(args, kCommand); | 44 | 44 |
| 45 | | -const watcher = new FilesWatcher({ throttle: 500, mode: kShouldFilterModules ? 'filter' : 'all' }); | | 45 | +const watcher = new FilesWatcher({ debounce: 500, mode: kShouldFilterModules ? 'filter' : 'all' }); | 46 | 46 | ArrayPrototypeForEach(kWatchedPaths, (p) => watcher.watchPath(p)); | 47 | 47 |
| 48 | 48 | let graceTimer; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/runner.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -419,7 +419,7 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) { | 419 | 419 | function watchFiles(testFiles, root, inspectPort, signal, testNamePatterns) { | 420 | 420 | const runningProcesses = new SafeMap(); | 421 | 421 | const runningSubtests = new SafeMap(); | 422 | | - const watcher = new FilesWatcher({ throttle: 500, mode: 'filter', signal }); | | 422 | + const watcher = new FilesWatcher({ debounce: 500, mode: 'filter', signal }); | 423 | 423 | const filesWatcher = { __proto__: null, watcher, runningProcesses, runningSubtests }; | 424 | 424 |
| 425 | 425 | watcher.on('changed', ({ owners }) => { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/watch_mode/files_watcher.js +11-9Lines changed: 11 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,19 +24,19 @@ const supportsRecursiveWatching = process.platform === 'win32' || | 24 | 24 | class FilesWatcher extends EventEmitter { | 25 | 25 | #watchers = new SafeMap(); | 26 | 26 | #filteredFiles = new SafeSet(); | 27 | | - #throttling = new SafeSet(); | | 27 | + #debouncing = new SafeSet(); | 28 | 28 | #depencencyOwners = new SafeMap(); | 29 | 29 | #ownerDependencies = new SafeMap(); | 30 | | - #throttle; | | 30 | + #debounce; | 31 | 31 | #mode; | 32 | 32 | #signal; | 33 | 33 |
| 34 | | - constructor({ throttle = 500, mode = 'filter', signal } = kEmptyObject) { | | 34 | + constructor({ debounce = 500, mode = 'filter', signal } = kEmptyObject) { | 35 | 35 | super(); | 36 | 36 |
| 37 | | - validateNumber(throttle, 'options.throttle', 0, TIMEOUT_MAX); | | 37 | + validateNumber(debounce, 'options.debounce', 0, TIMEOUT_MAX); | 38 | 38 | validateOneOf(mode, 'options.mode', ['filter', 'all']); | 39 | | - this.#throttle = throttle; | | 39 | + this.#debounce = debounce; | 40 | 40 | this.#mode = mode; | 41 | 41 | this.#signal = signal; | 42 | 42 |
| @@ -74,16 +74,18 @@ class FilesWatcher extends EventEmitter { | 74 | 74 | } | 75 | 75 |
| 76 | 76 | #onChange(trigger) { | 77 | | - if (this.#throttling.has(trigger)) { | | 77 | + if (this.#debouncing.has(trigger)) { | 78 | 78 | return; | 79 | 79 | } | 80 | 80 | if (this.#mode === 'filter' && !this.#filteredFiles.has(trigger)) { | 81 | 81 | return; | 82 | 82 | } | 83 | | - this.#throttling.add(trigger); | | 83 | + this.#debouncing.add(trigger); | 84 | 84 | const owners = this.#depencencyOwners.get(trigger); | 85 | | - this.emit('changed', { owners }); | 86 | | - setTimeout(() => this.#throttling.delete(trigger), this.#throttle).unref(); | | 85 | + setTimeout(() => { | | 86 | + this.#debouncing.delete(trigger); | | 87 | + this.emit('changed', { owners }); | | 88 | + }, this.#debounce).unref(); | 87 | 89 | } | 88 | 90 |
| 89 | 91 | get watchedPaths() { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-watch-mode-files_watcher.mjs +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,7 +26,7 @@ describe('watch mode file watcher', () => { | 26 | 26 |
| 27 | 27 | beforeEach(() => { | 28 | 28 | changesCount = 0; | 29 | | - watcher = new FilesWatcher({ throttle: 100 }); | | 29 | + watcher = new FilesWatcher({ debounce: 100 }); | 30 | 30 | watcher.on('changed', () => changesCount++); | 31 | 31 | }); | 32 | 32 |
| @@ -51,7 +51,7 @@ describe('watch mode file watcher', () => { | 51 | 51 | assert.strictEqual(changesCount, 1); | 52 | 52 | }); | 53 | 53 |
| 54 | | - it('should throttle changes', async () => { | | 54 | + it('should debounce changes', async () => { | 55 | 55 | const file = path.join(tmpdir.path, 'file2'); | 56 | 56 | writeFileSync(file, 'written'); | 57 | 57 | watcher.filterFile(file); | @@ -61,7 +61,7 @@ describe('watch mode file watcher', () => { | 61 | 61 | writeFileSync(file, '2'); | 62 | 62 | writeFileSync(file, '3'); | 63 | 63 | writeFileSync(file, '4'); | 64 | | - await setTimeout(200); // throttle * 2 | | 64 | + await setTimeout(200); // debounce * 2 | 65 | 65 | writeFileSync(file, '5'); | 66 | 66 | const changed = once(watcher, 'changed'); | 67 | 67 | writeFileSync(file, 'after'); | @@ -86,8 +86,8 @@ describe('watch mode file watcher', () => { | 86 | 86 | await writeAndWaitForChanges(watcher, file); | 87 | 87 |
| 88 | 88 | writeFileSync(file, '1'); | | 89 | + assert.strictEqual(changesCount, 1); | 89 | 90 |
| 90 | | - await setTimeout(200); // avoid throttling | 91 | 91 | watcher.clearFileFilters(); | 92 | 92 | writeFileSync(file, '2'); | 93 | 93 | // Wait for this long to make sure changes are triggered only once | @@ -97,7 +97,7 @@ describe('watch mode file watcher', () => { | 97 | 97 |
| 98 | 98 | it('should watch all files in watched path when in "all" mode', | 99 | 99 | { skip: !supportsRecursiveWatching }, async () => { | 100 | | - watcher = new FilesWatcher({ throttle: 100, mode: 'all' }); | | 100 | + watcher = new FilesWatcher({ debounce: 100, mode: 'all' }); | 101 | 101 | watcher.on('changed', () => changesCount++); | 102 | 102 |
| 103 | 103 | const file = path.join(tmpdir.path, 'file5'); | |
|