Skip to content

Commit

Permalink
Fix multiple non-existing throttling (paulmillr#1276)
Browse files Browse the repository at this point in the history
  • Loading branch information
vilicvane committed Mar 22, 2023
1 parent 0f163b8 commit 3b7a2ea
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
24 changes: 21 additions & 3 deletions lib/nodefs-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,13 +457,31 @@ _handleRead(directory, initialAdd, wh, target, dir, depth, throttler) {
// Normalize the directory name on Windows
directory = sysPath.join(directory, EMPTY_STR);

const current = new Set();
const targets = new Set([target]);

if (!wh.hasGlob) {
throttler = this.fsw._throttle('readdir', directory, 1000);
if (!throttler) return;
if (!throttler) {
const thr = this.fsw._throttled.get('readdir').get(directory);
if (thr.current.has(target)) {
this.fsw._incrReadyCount();

// ensure relativeness of path is preserved in case of watcher reuse
const path = sysPath.join(dir, sysPath.relative(dir, target));

this._addToNodeFs(path, initialAdd, wh, depth + 1);
} else {
thr.targets.add(target);
}
return;
}

throttler.current = current;
throttler.targets = targets;
}

const previous = this.fsw._getWatchedDir(wh.path);
const current = new Set();

let stream = this.fsw._readdirp(directory, {
fileFilter: entry => wh.filterPath(entry),
Expand All @@ -489,7 +507,7 @@ _handleRead(directory, initialAdd, wh, target, dir, depth, throttler) {
// Files that present in current directory snapshot
// but absent in previous are added to watch list and
// emit `add` event.
if (item === target || !target && !previous.has(item)) {
if (targets.has(item) || !target && !previous.has(item)) {
this.fsw._incrReadyCount();

// ensure relativeness of path is preserved in case of watcher reuse
Expand Down
17 changes: 15 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,19 @@ const runTests = (baseopts) => {
spy.withArgs(EV_CHANGE, testPath).should.have.been.calledThrice;
});

it('should detect add if multiple files watched in the same directory', async () => {
const test1Path = getFixturePath('add1.txt');
const test2Path = getFixturePath('add2.txt');
const watcher = chokidar_watch([test1Path, test2Path], options);
const spy = await aspy(watcher, EV_ALL);

await delay(300);
await write(test2Path, dateNow());
await write(test1Path, dateNow());
await waitFor([[spy, 2]]);
spy.withArgs(EV_ADD, test2Path).should.have.been.calledOnce;
spy.withArgs(EV_ADD, test1Path).should.have.been.calledOnce;
});

// PR 682 is failing.
describe.skip('Skipping gh-682: should detect unlink', () => {
Expand Down Expand Up @@ -2122,7 +2135,7 @@ const runTests = (baseopts) => {
await delay(300);
await write(testSubDirFile, '');
await delay(300);

chai.assert.deepStrictEqual(events, [
`[ALL] addDir: ${sysPath.join('test-fixtures', id, 'test')}`,
`[ALL] addDir: ${sysPath.join('test-fixtures', id, 'test', 'dir')}`,
Expand Down Expand Up @@ -2163,7 +2176,7 @@ const runTests = (baseopts) => {
await fs_mkdir(testSubDir);
await write(testSubDirFile, '');
await delay(300);

chai.assert.deepStrictEqual(events, [
`[ALL] addDir: ${sysPath.join('test-fixtures', id, 'test-link')}`,
`[ALL] addDir: ${sysPath.join('test-fixtures', id, 'test-link', 'dir')}`,
Expand Down

0 comments on commit 3b7a2ea

Please sign in to comment.