diff --git a/build/lib/compilation.js b/build/lib/compilation.js index 7a741a0b545e3bc889b3a56de2ee07071c75eb02..371107d401a21e82dc980675c93ca0d1ff4a0057 100644 --- a/build/lib/compilation.js +++ b/build/lib/compilation.js @@ -109,6 +109,7 @@ exports.watchTask = watchTask; const REPO_SRC_FOLDER = path.join(__dirname, '../../src'); class MonacoGenerator { constructor(isWatch) { + this._executeSoonTimer = null; this._isWatch = isWatch; this.stream = es.through(); this._inputFiles = monacodts.getIncludesInRecipe().map((moduleId) => { @@ -127,16 +128,14 @@ class MonacoGenerator { const watcher = fs.watch(filePath); watcher.addListener('change', () => { this._inputFileChanged[filePath] = true; - // Avoid hitting empty files... :/ - setTimeout(() => this.execute(), 10); + this._executeSoon(); }); this._watchers.push(watcher); }); const recipeWatcher = fs.watch(monacodts.RECIPE_PATH); recipeWatcher.addListener('change', () => { this._recipeFileChanged = true; - // Avoid hitting empty files... :/ - setTimeout(() => this.execute(), 10); + this._executeSoon(); }); this._watchers.push(recipeWatcher); } @@ -146,6 +145,16 @@ class MonacoGenerator { this._dtsFilesContents = {}; this._dtsFilesContents2 = {}; } + _executeSoon() { + if (this._executeSoonTimer !== null) { + // Already scheduled + return; + } + this._executeSoonTimer = setTimeout(() => { + this._executeSoonTimer = null; + this.execute(); + }, 20); + } dispose() { this._watchers.forEach(watcher => watcher.close()); } diff --git a/build/lib/compilation.ts b/build/lib/compilation.ts index 0ad0d4a62481533418e0de37e9f5f73c899fe810..9316c81e692f4487806abcaadc940517a8bf6bc5 100644 --- a/build/lib/compilation.ts +++ b/build/lib/compilation.ts @@ -166,8 +166,7 @@ class MonacoGenerator { const watcher = fs.watch(filePath); watcher.addListener('change', () => { this._inputFileChanged[filePath] = true; - // Avoid hitting empty files... :/ - setTimeout(() => this.execute(), 10); + this._executeSoon(); }); this._watchers.push(watcher); }); @@ -175,8 +174,7 @@ class MonacoGenerator { const recipeWatcher = fs.watch(monacodts.RECIPE_PATH); recipeWatcher.addListener('change', () => { this._recipeFileChanged = true; - // Avoid hitting empty files... :/ - setTimeout(() => this.execute(), 10); + this._executeSoon(); }); this._watchers.push(recipeWatcher); } @@ -188,6 +186,18 @@ class MonacoGenerator { this._dtsFilesContents2 = {}; } + private _executeSoonTimer: NodeJS.Timer | null = null; + private _executeSoon(): void { + if (this._executeSoonTimer !== null) { + // Already scheduled + return; + } + this._executeSoonTimer = setTimeout(() => { + this._executeSoonTimer = null; + this.execute(); + }, 20); + } + public dispose(): void { this._watchers.forEach(watcher => watcher.close()); }