Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a global change debounce #302

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 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 @@ -58,26 +59,29 @@ module.exports = function (
// Run ./dedupe.js as preload script
if (dedupe) process.env.NODE_DEV_PRELOAD = localPath('dedupe');

const watcher = filewatcher({ debounce, forcePolling, interval });
const watcher = filewatcher({ forcePolling, interval });
let isPaused = false;

// 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
25 changes: 25 additions & 0 deletions test/spawn/debounce-multi-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const tap = require('tap');

const { spawn, touchFile } = require('../utils');

tap.test('should reset debounce timer if a different file changes', t => {
spawn('--debounce=1000 --interval=50 server.js', out => {
if (out.match(/touch message.js/)) {
const ts = Date.now();
touchFile('server.js');
for (let i = 0; i <= 2000; i += 500) {
setTimeout(() => {
touchFile('message.js');
}, i);
}
return out2 => {
if (out2.match(/Restarting/)) {
if (Date.now() - ts < 2500) {
t.fail('Debounce logic did not wait 200 ms after the second change.');
}
return { exit: t.end.bind(t) };
}
};
}
});
});
Loading