Skip to content

Commit 5c3f02c

Browse files
perf(cli): tell the preview about a file edit 30 ms after it lands, not 300 (#4348)
* perf(cli): tell the preview about a file edit 30 ms after it lands, not 300 * perf(cli): keep bulk writes coalesced while lone saves go out fast * style(cli): name the watcher delay * perf(cli): throttle watcher flushes instead of switching to a long wait
1 parent 00ea884 commit 5c3f02c

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

‎packages/cli/src/server/fileWatcher.test.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ describe("createProjectWatcher", () => {
6767
mockWatcher.emit("change", "change", "scene-a.html");
6868
mockWatcher.emit("change", "change", "scene-b.html");
6969
mockWatcher.emit("change", "change", "scene-a.html");
70-
vi.advanceTimersByTime(300);
70+
vi.advanceTimersByTime(29);
71+
expect(listener).not.toHaveBeenCalled();
72+
vi.advanceTimersByTime(1);
7173

7274
expect(listener.mock.calls).toEqual([["scene-a.html"], ["scene-b.html"]]);
7375
projectWatcher.close();
@@ -97,6 +99,32 @@ describe("createProjectWatcher", () => {
9799
expect(() => projectWatcher?.close()).not.toThrow();
98100
});
99101

102+
it("flushes at most once per 300 ms while writes keep coming", () => {
103+
vi.useFakeTimers();
104+
const projectWatcher = createProjectWatcher("/fake/project/dir");
105+
const listener = vi.fn();
106+
projectWatcher.addListener(listener);
107+
108+
mockWatcher.emit("change", "change", "a.html");
109+
vi.advanceTimersByTime(30);
110+
expect(listener).toHaveBeenCalledTimes(1);
111+
112+
mockWatcher.emit("change", "change", "b.html");
113+
vi.advanceTimersByTime(100);
114+
mockWatcher.emit("change", "change", "c.html");
115+
vi.advanceTimersByTime(199);
116+
expect(listener).toHaveBeenCalledTimes(1);
117+
vi.advanceTimersByTime(1);
118+
expect(listener.mock.calls.slice(1)).toEqual([["b.html"], ["c.html"]]);
119+
120+
// A save that lands late in the window waits only for quiet, not a whole window.
121+
vi.advanceTimersByTime(290);
122+
mockWatcher.emit("change", "change", "d.html");
123+
vi.advanceTimersByTime(30);
124+
expect(listener).toHaveBeenLastCalledWith("d.html");
125+
projectWatcher.close();
126+
});
127+
100128
// Regression: fs.watch can fail asynchronously (e.g. EMFILE from exhausted
101129
// OS watch handles) via an 'error' event, not a thrown exception. An
102130
// EventEmitter 'error' with no listener crashes the whole process — this

‎packages/cli/src/server/fileWatcher.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ const WATCHER_EXCLUDED_DIRS = new Set([
2626
"outputs",
2727
"renders",
2828
]);
29-
const DEBOUNCE_MS = 300;
29+
// A save reaches the preview QUIET_MS after the writes go quiet, but at most once per BURST_MS,
30+
// so a checkout or a multi-file tool doesn't start a rebuild for every file.
31+
const QUIET_MS = 30;
32+
const BURST_MS = 300;
3033

3134
export function shouldWatchProjectFile(filename: string): boolean {
3235
if (!filename) return false;
@@ -112,6 +115,7 @@ export function createProjectWatcher(projectDir: string): ProjectWatcher {
112115
const listeners = new Set<FileChangeListener>();
113116
const pendingPaths = new Set<string>();
114117
let debounceTimer: ReturnType<typeof setTimeout> | null = null;
118+
let lastFlushAt = Number.NEGATIVE_INFINITY;
115119
let closeTree: (() => void) | null = null;
116120

117121
try {
@@ -130,16 +134,18 @@ export function createProjectWatcher(projectDir: string): ProjectWatcher {
130134

131135
pendingPaths.add(relativePath);
132136
if (debounceTimer) clearTimeout(debounceTimer);
137+
const delay = Math.max(QUIET_MS, lastFlushAt + BURST_MS - Date.now());
133138
debounceTimer = setTimeout(() => {
134139
const changedPaths = [...pendingPaths];
135140
pendingPaths.clear();
136141
debounceTimer = null;
142+
lastFlushAt = Date.now();
137143
for (const changedPath of changedPaths) {
138144
for (const fn of listeners) {
139145
fn(changedPath);
140146
}
141147
}
142-
}, DEBOUNCE_MS);
148+
}, delay);
143149
});
144150
} catch {
145151
// fs.watch may fail on some platforms — degrade gracefully (no auto-refresh)

0 commit comments

Comments
 (0)