Skip to content

Commit

Permalink
Implement a global change debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
dobesv committed Sep 6, 2023
1 parent 9ab4eb6 commit 923cb9d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
32 changes: 18 additions & 14 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const filewatcher = require('filewatcher');
const { join } = require('path');
const semver = require('semver');
const { pathToFileURL } = require('url');
const debounceFn = require('debounce');

const { clearFactory } = require('./clear');
const { configureDeps, configureIgnore } = require('./ignore');
Expand Down Expand Up @@ -64,20 +65,23 @@ module.exports = function (
// The child_process
let child;

watcher.on('change', file => {
clearOutput();
notify('Restarting', `${file} has been modified`);
watcher.removeAll();
isPaused = true;
if (child) {
// Child is still running, restart upon exit
child.on('exit', start);
stop();
} else {
// Child is already stopped, probably due to a previous error
start();
}
});
watcher.on(
'change',
debounceFn(file => {
clearOutput();
notify('Restarting', `${file} has been modified`);
watcher.removeAll();
isPaused = true;
if (child) {
// Child is still running, restart upon exit
child.on('exit', start);
stop();
} else {
// Child is already stopped, probably due to a previous error
start();
}
}, debounce)
);

watcher.on('fallback', limit => {
log.warn('node-dev ran out of file handles after watching %s files.', limit);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"dependencies": {
"dateformat": "^3.0.3",
"debounce": "^1.2.1",
"dynamic-dedupe": "^0.3.0",
"filewatcher": "~3.0.0",
"get-package-type": "^0.1.0",
Expand Down

0 comments on commit 923cb9d

Please sign in to comment.