Skip to content

Commit 840ead9

Browse files
authored
fix(compiler): fix file watcher sometimes doesn't trigger rebuild (#6191)
* fix(compiler): fix file watcher sometimes doesn't trigger rebuild The existing implementation was running into the `lastTsBuilder && !timeoutId` condition with a `timeoutId` being present even though there was no running build or any other good reason not to rebuild. The change simplifies the implementation by using a recursive `setTimeout` to wait for pending builds. This allows to: - remove the extra `rebuildTimer` which was arguably confusing - remove the clearTimeout override Since we immediately call `clearTimeout(timeoutId)` in the `setTimeout` override, we don't stack up rebuild requests. After applying these changes the watcher behaves beautifully. fixes: #6190 * style(compiler): fix code formatting
1 parent aa1bc62 commit 840ead9

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/compiler/transpile/create-watch-program.ts

+11-13
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export const createTsWatchProgram = async (
2424
let isRunning = false;
2525
let lastTsBuilder: any;
2626
let timeoutId: any;
27-
let rebuildTimer: any;
2827

2928
// Get the pre-baked TS options we want to use for our builder program
3029
const optionsToExtend = getTsOptionsToExtend(config);
@@ -44,19 +43,18 @@ export const createTsWatchProgram = async (
4443
* @returns A {@link NodeJs.Timer} instance
4544
*/
4645
setTimeout(callback, time) {
47-
clearInterval(rebuildTimer);
48-
const t = (timeoutId = setInterval(() => {
46+
clearTimeout(timeoutId);
47+
const delay = config.sys.watchTimeout || time;
48+
const tick = () => {
4949
if (!isRunning) {
5050
callback();
51-
clearInterval(t);
52-
timeoutId = rebuildTimer = null;
51+
timeoutId = null;
52+
} else {
53+
timeoutId = setTimeout(tick, delay);
5354
}
54-
}, config.sys.watchTimeout || time));
55-
return t;
56-
},
57-
58-
clearTimeout(id) {
59-
return clearInterval(id);
55+
};
56+
timeoutId = setTimeout(tick, delay);
57+
return timeoutId;
6058
},
6159
};
6260

@@ -111,8 +109,8 @@ export const createTsWatchProgram = async (
111109
// This will be called via a callback on the watch build whenever a file
112110
// change is detected
113111
rebuild: () => {
114-
if (lastTsBuilder && !timeoutId) {
115-
rebuildTimer = tsWatchSys.setTimeout(() => tsWatchHost.afterProgramCreate(lastTsBuilder), 300);
112+
if (lastTsBuilder) {
113+
tsWatchSys.setTimeout(() => tsWatchHost.afterProgramCreate(lastTsBuilder), 300);
116114
}
117115
},
118116
};

0 commit comments

Comments
 (0)