Skip to content

Commit 263fbc1

Browse files
committed
debounce file change callback to avoid occasional double trigger
1 parent 764d6bd commit 263fbc1

File tree

5 files changed

+58
-40
lines changed

5 files changed

+58
-40
lines changed

package-lock.json

Lines changed: 12 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"chokidar": "^2.0.4",
3939
"commander": "^2.19.0",
4040
"date-fns": "^1.30.1",
41-
"glob": "^7.1.3"
41+
"glob": "^7.1.3",
42+
"lodash": "^4.17.11"
4243
},
4344
"optionalDependencies": {
4445
"fsevents": "^1.0.0"

src/file-watcher.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
const fileWatcher = require('chokidar');
21
const fs = require('fs');
2+
const fileWatcher = require('chokidar');
3+
const _ = require('lodash');
34
const { consolePrinter } = require('./printer');
45

56
module.exports = {
6-
createWatcher: configureCreateWatcher.bind(null, consolePrinter),
7+
createWatcher: configureCreateWatcher.bind(null, consolePrinter, _.debounce),
78
configureCreateWatcher,
89
};
910

10-
function configureCreateWatcher(printer, configuration) {
11+
function configureCreateWatcher(printer, debounce, configuration) {
1112
const { environments } = configuration;
1213
let watcher;
1314

@@ -22,7 +23,9 @@ function configureCreateWatcher(printer, configuration) {
2223
atomic: true,
2324
depth: 0,
2425
});
25-
watcher.on('change', callback);
26+
27+
// We debounce the change callback to avoid occasional double trigger
28+
watcher.on('change', debounce(callback, 100));
2629
watcher.on('ready', () => {
2730
printer.info(`Watching ${getFilesWatchedCount(environments, watcher.getWatched())} file(s)`);
2831
});

test/file-watcher.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('watcher', function watcherTest() {
2121
};
2222
process.chdir(path.join('.', 'test'));
2323
printerSpy = createPrinterSpy();
24-
watcher = configureCreateWatcher(printerSpy, configuration);
24+
watcher = configureCreateWatcher(printerSpy, debounceDummy, configuration);
2525
});
2626

2727
afterEach(() => {
@@ -136,3 +136,7 @@ describe('watcher', function watcherTest() {
136136
};
137137
}
138138
});
139+
140+
function debounceDummy(func) {
141+
return func;
142+
}

test/integration/argus.test.js

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ const { runCommands } = require('../../src/command-runner');
66
const createRunCommandsSpy = require('../helpers/run-commands-spy');
77
const { StdinMock } = require('./../helpers/mockStdio');
88

9+
const wait = miliseconds => new Promise(resolve => setTimeout(resolve, miliseconds));
10+
const waitForDebounce = () => wait(110);
11+
912
describe('argus', function argusTestSuite() {
1013
this.slow(500);
1114

@@ -42,9 +45,11 @@ describe('argus', function argusTestSuite() {
4245
fork(pathToTouchScript, [path.join('.', 'src', 'PhpClass.php')]);
4346

4447
watcher.on('change', () => {
45-
assert.equal(runCommandsSpy.getLastRunCommands()[0].command, 'echo');
46-
assert.deepEqual(runCommandsSpy.getLastRunCommands()[0].args, ['tests/src/PhpClassTest.php']);
47-
done();
48+
waitForDebounce().then(() => {
49+
assert.equal(runCommandsSpy.getLastRunCommands()[0].command, 'echo');
50+
assert.deepEqual(runCommandsSpy.getLastRunCommands()[0].args, ['tests/src/PhpClassTest.php']);
51+
done();
52+
});
4853
});
4954
});
5055

@@ -53,9 +58,11 @@ describe('argus', function argusTestSuite() {
5358
fork(pathToTouchScript, [path.join('.', 'tests', 'src', 'PhpClassTest.php')]);
5459

5560
watcher.on('change', () => {
56-
assert.equal(runCommandsSpy.getLastRunCommands()[0].command, 'echo');
57-
assert.deepEqual(runCommandsSpy.getLastRunCommands()[0].args, ['tests/src/PhpClassTest.php']);
58-
done();
61+
waitForDebounce().then(() => {
62+
assert.equal(runCommandsSpy.getLastRunCommands()[0].command, 'echo');
63+
assert.deepEqual(runCommandsSpy.getLastRunCommands()[0].args, ['tests/src/PhpClassTest.php']);
64+
done();
65+
});
5966
});
6067
});
6168

@@ -65,12 +72,14 @@ describe('argus', function argusTestSuite() {
6572

6673
return new Promise((resolve) => {
6774
watcher.on('change', () => {
68-
assert.strictEqual(runCommandsSpy.getCommandsBatchRunCount(), 1);
69-
assert.equal(runCommandsSpy.getLastRunCommands()[0].command, 'echo');
70-
assert.deepEqual(runCommandsSpy.getLastRunCommands()[0].args, ['tests/src/PhpClassTest.php']);
71-
72-
mockStdin.on('data', resolve);
73-
mockStdin.push('r');
75+
waitForDebounce().then(() => {
76+
assert.strictEqual(runCommandsSpy.getCommandsBatchRunCount(), 1);
77+
assert.equal(runCommandsSpy.getLastRunCommands()[0].command, 'echo');
78+
assert.deepEqual(runCommandsSpy.getLastRunCommands()[0].args, ['tests/src/PhpClassTest.php']);
79+
80+
mockStdin.on('data', resolve);
81+
mockStdin.push('r');
82+
});
7483
});
7584
}).then(() => {
7685
assert.strictEqual(runCommandsSpy.getCommandsBatchRunCount(), 2);
@@ -104,15 +113,17 @@ describe('argus', function argusTestSuite() {
104113
fork(pathToTouchScript, [path.join('.', 'src', 'Class.php')]);
105114

106115
watcher.on('change', () => {
107-
assert.equal(runCommandsSpy.getLastRunCommands().length, 2, 'Expected only two commands to run');
108-
assert.equal(runCommandsSpy.getLastRunCommands()[0].command, 'vendor/bin/phpunit');
109-
assert.deepEqual(runCommandsSpy.getLastRunCommands()[0].args, ['tests/unit/src/ClassTest.php']);
110-
assert.equal(runCommandsSpy.getLastRunCommands()[1].command, 'vendor/bin/phpunit');
111-
assert.deepEqual(
112-
runCommandsSpy.getLastRunCommands()[1].args,
113-
['-c', 'phpunit-integration.xml', 'tests/integration/src/ClassTest.php'],
114-
);
115-
done();
116+
waitForDebounce().then(() => {
117+
assert.equal(runCommandsSpy.getLastRunCommands().length, 2, 'Expected only two commands to run');
118+
assert.equal(runCommandsSpy.getLastRunCommands()[0].command, 'vendor/bin/phpunit');
119+
assert.deepEqual(runCommandsSpy.getLastRunCommands()[0].args, ['tests/unit/src/ClassTest.php']);
120+
assert.equal(runCommandsSpy.getLastRunCommands()[1].command, 'vendor/bin/phpunit');
121+
assert.deepEqual(
122+
runCommandsSpy.getLastRunCommands()[1].args,
123+
['-c', 'phpunit-integration.xml', 'tests/integration/src/ClassTest.php'],
124+
);
125+
done();
126+
});
116127
});
117128
});
118129
});

0 commit comments

Comments
 (0)