Skip to content

Commit 0409506

Browse files
committed
refactor(project): Make WatchHandler wait for build to finish before triggering again
1 parent 8bd2528 commit 0409506

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

packages/project/lib/build/helpers/WatchHandler.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class WatchHandler extends EventEmitter {
1515
#updateBuildResult;
1616
#abortControllers = [];
1717
#sourceChanges = new Map();
18+
#updateInProgress = false;
1819
#fileChangeHandlerTimeout;
1920

2021
constructor(buildContext, updateBuildResult) {
@@ -64,28 +65,46 @@ class WatchHandler extends EventEmitter {
6465
}
6566
}
6667

67-
async #fileChanged(project, filePath) {
68+
#fileChanged(project, filePath) {
6869
// Collect changes (grouped by project), then trigger callbacks
6970
const resourcePath = project.getVirtualPath(filePath);
7071
if (!this.#sourceChanges.has(project)) {
7172
this.#sourceChanges.set(project, new Set());
7273
}
7374
this.#sourceChanges.get(project).add(resourcePath);
7475

76+
this.#queueHandleResourceChanges();
77+
}
78+
79+
#queueHandleResourceChanges() {
80+
if (this.#updateInProgress) {
81+
// Prevent concurrent updates
82+
return;
83+
}
84+
7585
// Trigger callbacks debounced
7686
if (this.#fileChangeHandlerTimeout) {
7787
clearTimeout(this.#fileChangeHandlerTimeout);
7888
}
7989
this.#fileChangeHandlerTimeout = setTimeout(async () => {
80-
await this.#handleResourceChanges();
8190
this.#fileChangeHandlerTimeout = null;
91+
92+
const sourceChanges = this.#sourceChanges;
93+
// Reset file changes before processing
94+
this.#sourceChanges = new Map();
95+
96+
this.#updateInProgress = true;
97+
await this.#handleResourceChanges(sourceChanges);
98+
this.#updateInProgress = false;
99+
100+
if (this.#sourceChanges.size > 0) {
101+
// New changes have occurred during processing, trigger queue again
102+
this.#queueHandleResourceChanges();
103+
}
82104
}, 100);
83105
}
84106

85-
async #handleResourceChanges() {
86-
// Reset file changes before processing
87-
const sourceChanges = this.#sourceChanges;
88-
this.#sourceChanges = new Map();
107+
async #handleResourceChanges(sourceChanges) {
89108
const dependencyChanges = new Map();
90109
let someProjectTasksInvalidated = false;
91110

0 commit comments

Comments
 (0)