Skip to content

Commit

Permalink
fix: Improved debounce to prevent multiple extension reloads on conse…
Browse files Browse the repository at this point in the history
…cutive file changes (#2385)

Co-authored-by: Federico Brigante <me@fregante.com>
  • Loading branch information
rpl and fregante authored Mar 24, 2022
1 parent 81083df commit 6c53a01
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function onSourceChange(
artifactsDir,
onChange,
shouldWatchFile,
debounceTime = 1000,
debounceTime = 500,
}: OnSourceChangeParams
): Watchpack {
// When running on Windows, transform the ignored paths and globs
Expand All @@ -51,7 +51,8 @@ export default function onSourceChange(
new Watchpack({ignored}) :
new Watchpack();

const executeImmediately = true;
// Allow multiple files to be changed before reloading the extension
const executeImmediately = false;
onChange = debounce(onChange, debounceTime, executeImmediately);

watcher.on('change', (filePath) => {
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/test.watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ describe('watcher', () => {
);

it('watches for changes in the sourceDir', async () => {
const defaultDebounce = 500;
const {
onChange,
watchedFilePath,
Expand All @@ -102,6 +103,8 @@ describe('watcher', () => {
touchedFile: 'foo.txt',
});

await new Promise((resolve) => setTimeout(resolve, defaultDebounce + 50));

sinon.assert.calledOnce(onChange);
assert.equal(watchedDirPath, tmpDirPath);
assert.isUndefined(watchedFilePath);
Expand Down

0 comments on commit 6c53a01

Please sign in to comment.