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
This commit is contained in:
Zoltán Gilián
2019-07-08 03:01:37 -07:00
committed by Facebook Github Bot
parent 5d99817555
commit aff51b881d

View File

@@ -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);
}
}),
);