Skip to content

Commit fb4ead2

Browse files
clydindgp1130
authored andcommitted
fix(@angular-devkit/build-angular): wait during file watching to improve multi-save rebuilds for esbuild builder
When using the experimental esbuild-based browser application builder in watch mode, the file watcher will now wait 250ms from a reported file event before triggering a rebuild. The change allows the rebuild to better capture groups of file changes. This can happen when using an IDE while editing multiple files and would otherwise result in multiple rebuilds where a single rebuild would be ideal. (cherry picked from commit 7c2b846)
1 parent 1886f57 commit fb4ead2

File tree

1 file changed

+16
-5
lines changed
  • packages/angular_devkit/build_angular/src/builders/browser-esbuild

1 file changed

+16
-5
lines changed

Diff for: packages/angular_devkit/build_angular/src/builders/browser-esbuild/watcher.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export function createWatcher(options?: {
4343

4444
const nextQueue: ((value?: ChangedFiles) => void)[] = [];
4545
let currentChanges: ChangedFiles | undefined;
46+
let nextWaitTimeout: NodeJS.Timeout | undefined;
4647

4748
watcher.on('all', (event, path) => {
4849
switch (event) {
@@ -62,11 +63,18 @@ export function createWatcher(options?: {
6263
return;
6364
}
6465

65-
const next = nextQueue.shift();
66-
if (next) {
67-
const value = currentChanges;
68-
currentChanges = undefined;
69-
next(value);
66+
// Wait 250ms from next change to better capture groups of file save operations.
67+
if (!nextWaitTimeout) {
68+
nextWaitTimeout = setTimeout(() => {
69+
nextWaitTimeout = undefined;
70+
const next = nextQueue.shift();
71+
if (next) {
72+
const value = currentChanges;
73+
currentChanges = undefined;
74+
next(value);
75+
}
76+
}, 250);
77+
nextWaitTimeout?.unref();
7078
}
7179
});
7280

@@ -99,6 +107,9 @@ export function createWatcher(options?: {
99107
async close() {
100108
try {
101109
await watcher.close();
110+
if (nextWaitTimeout) {
111+
clearTimeout(nextWaitTimeout);
112+
}
102113
} finally {
103114
let next;
104115
while ((next = nextQueue.shift()) !== undefined) {

0 commit comments

Comments
 (0)