Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/polite-apes-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Prevent concurrent custom builds when multiple files change simultaneously
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,13 @@ export class BundlerController extends Controller<BundlerControllerEventMap> {
void this.#runCustomBuild(config, String(pathsToWatch));
});

this.#customBuildWatcher.on(
"all",
(_event, filePath) => void this.#runCustomBuild(config, filePath)
const debouncedRunCustomBuild = debounce(
(_event: string, filePath: string) =>
void this.#runCustomBuild(config, filePath),
50
);

this.#customBuildWatcher.on("all", debouncedRunCustomBuild);
}

#bundlerCleanup?: ReturnType<typeof runBuild>;
Expand Down
10 changes: 7 additions & 3 deletions packages/wrangler/src/utils/debounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
* `delayMs` milliseconds have elapsed since the last time the debounced
* function was invoked.
*/
export function debounce(fn: () => void, delayMs = 100) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use any. Try unknown?

export function debounce<T extends (...args: any[]) => void>(
fn: T,
delayMs = 100
): (...args: Parameters<T>) => void {
let crrTimeoutId: NodeJS.Timeout | undefined;

return () => {
return (...args: Parameters<T>) => {
if (crrTimeoutId) {
clearTimeout(crrTimeoutId);
}

crrTimeoutId = setTimeout(() => {
fn();
fn(...args);
}, delayMs);
};
}
Loading