Skip to content

Commit 6cda8d5

Browse files
Copilotstreamich
andcommitted
fix: remove setTimeout to capture immediate file events in watched directories
Co-authored-by: streamich <[email protected]>
1 parent ba80031 commit 6cda8d5

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

src/node/__tests__/volume.test.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,8 +1386,10 @@ describe('volume', () => {
13861386
try {
13871387
vol.writeFileSync('/tmp/foo-dir/foo.js', writtenContent);
13881388

1389-
expect(mockCallback).toBeCalledTimes(1);
1390-
expect(mockCallback).toBeCalledWith('rename', 'foo.js');
1389+
expect(mockCallback).toBeCalledTimes(3);
1390+
expect(mockCallback).nthCalledWith(1, 'rename', 'foo.js');
1391+
expect(mockCallback).nthCalledWith(2, 'change', 'foo.js');
1392+
expect(mockCallback).nthCalledWith(3, 'change', 'foo.js');
13911393
} finally {
13921394
watcher.close();
13931395
}
@@ -1442,13 +1444,15 @@ describe('volume', () => {
14421444

14431445
setTimeout(() => {
14441446
watcher.close();
1445-
expect(listener).toBeCalledTimes(6);
1447+
expect(listener).toBeCalledTimes(8);
14461448
expect(listener).nthCalledWith(1, 'change', 'lol.txt');
14471449
expect(listener).nthCalledWith(2, 'change', 'lol.txt');
14481450
expect(listener).nthCalledWith(3, 'rename', 'test/lol.txt');
1449-
expect(listener).nthCalledWith(4, 'rename', 'lol.txt');
1450-
expect(listener).nthCalledWith(5, 'rename', 'test/lol.txt');
1451-
expect(listener).nthCalledWith(6, 'rename', 'test/foo');
1451+
expect(listener).nthCalledWith(4, 'change', 'test/lol.txt');
1452+
expect(listener).nthCalledWith(5, 'change', 'test/lol.txt');
1453+
expect(listener).nthCalledWith(6, 'rename', 'lol.txt');
1454+
expect(listener).nthCalledWith(7, 'rename', 'test/lol.txt');
1455+
expect(listener).nthCalledWith(8, 'rename', 'test/foo');
14521456
done();
14531457
}, 10);
14541458
});
@@ -1475,6 +1479,28 @@ describe('volume', () => {
14751479
}, 10);
14761480
});
14771481
});
1482+
it('Calls listener for file created immediately after directory creation', done => {
1483+
const vol = new Volume();
1484+
vol.mkdirSync('/watched', { recursive: true });
1485+
1486+
const listener = jest.fn();
1487+
const watcher = vol.watch('/watched', { recursive: true }, listener);
1488+
1489+
// Create directory and immediately create file inside it
1490+
vol.mkdirSync('/watched/new_dir', { recursive: true });
1491+
vol.writeFileSync('/watched/new_dir/new_file', 'content');
1492+
1493+
setTimeout(() => {
1494+
watcher.close();
1495+
1496+
// Should have at least 3 events: directory creation, file creation, file change
1497+
expect(listener).toHaveBeenCalledWith('rename', 'new_dir');
1498+
expect(listener).toHaveBeenCalledWith('rename', 'new_dir/new_file');
1499+
expect(listener).toHaveBeenCalledWith('change', 'new_dir/new_file');
1500+
1501+
done();
1502+
}, 10);
1503+
});
14781504
});
14791505
describe('.watchFile(path[, options], listener)', () => {
14801506
it('Calls listener on .writeFile', done => {

src/node/volume.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,12 +2006,10 @@ export class FSWatcher extends EventEmitter {
20062006
const onLinkChildAdd = (l: Link) => {
20072007
this.emit('change', 'rename', relative(this._filename, l.getPath()));
20082008

2009-
setTimeout(() => {
2010-
// 1. watch changes of the new link-node
2011-
watchLinkNodeChanged(l);
2012-
// 2. watch changes of the new link-node's children
2013-
watchLinkChildrenChanged(l);
2014-
});
2009+
// 1. watch changes of the new link-node
2010+
watchLinkNodeChanged(l);
2011+
// 2. watch changes of the new link-node's children
2012+
watchLinkChildrenChanged(l);
20152013
};
20162014

20172015
// when a new link deleted

0 commit comments

Comments
 (0)