Skip to content

Commit

Permalink
watch: batch file restarts
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed Mar 6, 2024
1 parent 2246cd9 commit 8095f10
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
26 changes: 12 additions & 14 deletions lib/internal/main/watch_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const { spawn } = require('child_process');
const { inspect } = require('util');
const { setTimeout, clearTimeout } = require('timers');
const { resolve } = require('path');
const { once, on } = require('events');
const { once } = require('events');

prepareMainThreadExecution(false, false);
markBootstrapComplete();
Expand Down Expand Up @@ -103,27 +103,25 @@ async function stop() {
clearGraceReport();
}

let restarting = false;
async function restart() {
if (restarting) return;
restarting = true;
if (!kPreserveOutput) process.stdout.write(clear);
process.stdout.write(`${green}Restarting ${kCommandStr}${white}\n`);
await stop();
start();
restarting = false;
}

(async () => {
emitExperimentalWarning('Watch mode');

try {
start();

// eslint-disable-next-line no-unused-vars
for await (const _ of on(watcher, 'changed')) {
await restart();
}
} catch (error) {
emitExperimentalWarning('Watch mode');
start();
watcher
.on('changed', restart)
.on('error', (error) => {
watcher.off('changed', restart);
triggerUncaughtException(error, true /* fromPromise */);
}
})();
});

// Exiting gracefully to avoid stdout/stderr getting written after
// parent process is killed.
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/watch_mode/files_watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FilesWatcher extends EventEmitter {
#signal;

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

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

0 comments on commit 8095f10

Please sign in to comment.