chokidar
chokidar copied to clipboard
[Feature] Add timeout option
It would be very useful to have an option called timeout
(maybe by default set to 0
to allow retrocompatibility) that would cause the watcher to throw
an Exception if the execution time exceeded N ms.
For now i implemented like this (the Promise
part with resolve
and reject
can be omitted; i added it only for the sake of completeness):
return new Promise((resolve, reject) => {
// Watch event on download folder or file
const watcher = chokidar.watch(target, {
interval: 500,
awaitWriteFinish: true,
});
// Stop watching the file if the timeout is reached
let watcherTimeout = setTimeout(async () => {
console.log("Timeout reached!");
if (watcher && Object.keys(watcher.getWatched()).length !== 0) {
console.log("Closing watcher!");
await watcher.close();
reject(`Timeout reached while waiting for file '${target}' to be created!`);
}
}, 5000);
// Start watching until the file has been created with the expected FileName (temporary has '.crdownload' extension)
watcher
.on('ready', () => console.log('Initial scan complete. Ready for changes'))
.on('add', (path, stats) => {
console.log(`File '${path}' has been added (${stats?.size} bytes)`);
watcher.unwatch(target).close().then(() => {
clearTimeout(watcherTimeout);
resolve(target);
});
});
});