From aff51b881d3c85f720cbd48080fe1adbf902aa4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Gili=C3=A1n?= Date: Mon, 8 Jul 2019 03:01:37 -0700 Subject: [PATCH] Fix uneccesary compilation when fs.watch rapid-fires Summary: On Windows fs.watch fires multiple times after a single edit causing multiple instances of plugin compilation threads to start bogging down system resources. This diff delays compilation by 1 second, soaking additional watch events fired for that plugin in that time window. Reviewed By: danielbuechele Differential Revision: D16109076 fbshipit-source-id: bc4f89d83f04487a3d32ee5cc2105fe0a8b7366c --- static/compilePlugins.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/static/compilePlugins.js b/static/compilePlugins.js index cfc0f66ab..4ed004779 100644 --- a/static/compilePlugins.js +++ b/static/compilePlugins.js @@ -63,15 +63,21 @@ function watchChanges( // eslint-disable-next-line no-console console.log('🕵️‍ Watching for plugin changes'); + let delayedCompilation = {}; + const kCompilationDelayMillis = 1000; + Object.values(plugins).map(plugin => fs.watch(plugin.rootDir, {recursive: true}, (eventType, filename) => { // only recompile for changes in not hidden files. Watchman might create // a file called .watchman-cookie - if (!filename.startsWith('.')) { - // eslint-disable-next-line no-console - console.log(`🕵️‍ Detected changes in ${plugin.name}`); - const watchOptions = Object.assign(options, {force: true}); - compilePlugin(plugin, pluginCache, watchOptions).then(reloadCallback); + if (!filename.startsWith('.') && !delayedCompilation[plugin.name]) { + delayedCompilation[plugin.name] = setTimeout(() => { + delayedCompilation[plugin.name] = null; + // eslint-disable-next-line no-console + console.log(`🕵️‍ Detected changes in ${plugin.name}`); + const watchOptions = Object.assign(options, {force: true}); + compilePlugin(plugin, pluginCache, watchOptions).then(reloadCallback); + }, kCompilationDelayMillis); } }), );