From 71dd960fecaccef0830ef1179e8c2f9a23386c34 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Thu, 24 Mar 2022 16:38:03 -0800 Subject: [PATCH] fix(watch): throttle / batch watch processing (#13054) --- utils/build/build.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/utils/build/build.js b/utils/build/build.js index cfc99e4c3b..f71101f468 100644 --- a/utils/build/build.js +++ b/utils/build/build.js @@ -80,14 +80,22 @@ function quotePath(path) { async function runWatch() { function runOnChanges(paths, mustExist = [], nodeFile) { nodeFile = filePath(nodeFile); - function callback() { + let timeout; + const callback = () => { + timeout = undefined; for (const fileMustExist of mustExist) { if (!fs.existsSync(filePath(fileMustExist))) return; } child_process.spawnSync('node', [nodeFile], { stdio: 'inherit' }); - } - chokidar.watch([...paths, ...mustExist, nodeFile].map(filePath)).on('all', callback); + }; + // chokidar will report all files as added in a sync loop, throttle those. + const reschedule = () => { + if (timeout) + clearTimeout(timeout); + timeout = setTimeout(callback, 500); + }; + chokidar.watch([...paths, ...mustExist, nodeFile].map(filePath)).on('all', reschedule); callback(); }