Skip to content

Commit 508e968

Browse files
MoLowmatthieusieben
authored andcommitted
watch: batch file restarts
Co-authored-by: Matthieu <[email protected]> PR-URL: #51992 Reviewed-By: Nitzan Uziely <[email protected]> Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent ae7a11c commit 508e968

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

lib/internal/main/watch_mode.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const { spawn } = require('child_process');
2626
const { inspect } = require('util');
2727
const { setTimeout, clearTimeout } = require('timers');
2828
const { resolve } = require('path');
29-
const { once, on } = require('events');
29+
const { once } = require('events');
3030

3131
prepareMainThreadExecution(false, false);
3232
markBootstrapComplete();
@@ -103,27 +103,28 @@ async function stop() {
103103
clearGraceReport();
104104
}
105105

106+
let restarting = false;
106107
async function restart() {
107-
if (!kPreserveOutput) process.stdout.write(clear);
108-
process.stdout.write(`${green}Restarting ${kCommandStr}${white}\n`);
109-
await stop();
110-
start();
111-
}
112-
113-
(async () => {
114-
emitExperimentalWarning('Watch mode');
115-
108+
if (restarting) return;
109+
restarting = true;
116110
try {
111+
if (!kPreserveOutput) process.stdout.write(clear);
112+
process.stdout.write(`${green}Restarting ${kCommandStr}${white}\n`);
113+
await stop();
117114
start();
115+
} finally {
116+
restarting = false;
117+
}
118+
}
118119

119-
// eslint-disable-next-line no-unused-vars
120-
for await (const _ of on(watcher, 'changed')) {
121-
await restart();
122-
}
123-
} catch (error) {
120+
emitExperimentalWarning('Watch mode');
121+
start();
122+
watcher
123+
.on('changed', restart)
124+
.on('error', (error) => {
125+
watcher.off('changed', restart);
124126
triggerUncaughtException(error, true /* fromPromise */);
125-
}
126-
})();
127+
});
127128

128129
// Exiting gracefully to avoid stdout/stderr getting written after
129130
// parent process is killed.

lib/internal/watch_mode/files_watcher.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class FilesWatcher extends EventEmitter {
3232
#signal;
3333

3434
constructor({ debounce = 200, mode = 'filter', signal } = kEmptyObject) {
35-
super();
35+
super({ __proto__: null, captureRejections: true });
3636

3737
validateNumber(debounce, 'options.debounce', 0, TIMEOUT_MAX);
3838
validateOneOf(mode, 'options.mode', ['filter', 'all']);

0 commit comments

Comments
 (0)