-
-
Notifications
You must be signed in to change notification settings - Fork 610
Description
I have been rewriting a tool that uses gulp watch, so it will use Chokidar v4 (v4.0.3) with the new setup where the argument for the watch is a folder and not a glob.
Because we would like to show the users of our tool when the watch is ready, i put a message in the .on('ready', () => {}) event.
The problem
But when watching folders with lots of files (can be more than 1000 directories), it can take a long time before the 'ready' event is fired, so say for instance 15 seconds.
Whats strange is that when i change a file when it shows 'Watcher created...' (see code below), the watch already reacts and the .on('all', () => {}) event is fired. And then about 15 seconds later, the 'ready' event fires.
After some investigation I learned that there are two phases:
A) discover & attach (directories only)
B) other things Chokidar needs to do
And that the 'ready' event is fired after phase B.
Questions
So first of all, it would be nice if there was a second event, maybe called 'readyAttach' or something like that, that fires after phase A has finished.
Second, I am not sure what happens in phase B and why it takes such a long time.
Third, if the watch .on('all', () => {}) event already fires long before phase B is finished, does that mean that I (in this particular use-case) do not need the result of phase B? And therefore I should be able to disable it with a option given to the .watch() function?
Code
Here is what my code looks like:
import { performance } from 'node:perf_hooks';
import chokidar from 'chokidar';
console.log('Starting watcher...');
const startTimer = performance.now();
const watcher = chokidar.watch(
watchFolder,
{
ignored: chokidarIgnored, // a function that determines which files to ignore
persistent: true,
ignoreInitial: true, // true = ignore add/addDir events while instantiating
followSymlinks: true,
cwd: repoPath, // this makes all reported paths relative to root of repo
usePolling: process.platform === 'win32' ? false : undefined,
interval: process.platform === 'win32' ? 100 : undefined,
binaryInterval: process.platform === 'win32' ? 300 : undefined,
}
);
msg.debug('Watcher created...');
watcher
.on('ready', () => {
console.log('Watch fully initialized...');
console.log(`Timer: ${performance.now() - startTimer}`);
const watched = watcher.getWatched();
let dirCount = 0;
for(const pth of Object.keys(watched)) {
console.log(`${pth}`);
dirCount++;
}
console.log(`Watching ${dirCount} directories`);
});