Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): wait during file watching to impr…
Browse files Browse the repository at this point in the history
…ove 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)
  • Loading branch information
clydin authored and dgp1130 committed Oct 31, 2022
1 parent 1886f57 commit fb4ead2
Showing 1 changed file with 16 additions and 5 deletions.
Expand Up @@ -43,6 +43,7 @@ export function createWatcher(options?: {

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

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

const next = nextQueue.shift();
if (next) {
const value = currentChanges;
currentChanges = undefined;
next(value);
// Wait 250ms from next change to better capture groups of file save operations.
if (!nextWaitTimeout) {
nextWaitTimeout = setTimeout(() => {
nextWaitTimeout = undefined;
const next = nextQueue.shift();
if (next) {
const value = currentChanges;
currentChanges = undefined;
next(value);
}
}, 250);
nextWaitTimeout?.unref();
}
});

Expand Down Expand Up @@ -99,6 +107,9 @@ export function createWatcher(options?: {
async close() {
try {
await watcher.close();
if (nextWaitTimeout) {
clearTimeout(nextWaitTimeout);
}
} finally {
let next;
while ((next = nextQueue.shift()) !== undefined) {
Expand Down

0 comments on commit fb4ead2

Please sign in to comment.