Execute just once after N changes

This commit is contained in:
Alex Dima
2018-10-24 12:19:52 +02:00
parent c88064a3ad
commit 7e8384ba86
2 changed files with 27 additions and 8 deletions

View File

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