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

Add debouncing to readdirp call #690

Merged
merged 1 commit into from
Apr 27, 2018
Merged
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
33 changes: 24 additions & 9 deletions lib/nodefs-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var fs = require('fs');
var sysPath = require('path');
var readdirp = require('readdirp');
var isBinaryPath = require('is-binary-path');
var debounce = require('lodash.debounce');

// fs.watch helpers

Expand Down Expand Up @@ -340,15 +341,12 @@ function(dir, stats, initialAdd, depth, target, wh, callback) {
parentDir.add(sysPath.basename(dir));
this._getWatchedDir(dir);

var debouncedRead;

var read = function(directory, initialAdd, done) {
// Normalize the directory name on Windows
directory = sysPath.join(directory, '');

if (!wh.hasGlob) {
var throttler = this._throttle('readdir', directory, 1000);
if (!throttler) return;
}

var previous = this._getWatchedDir(wh.path);
var current = [];

Expand Down Expand Up @@ -379,9 +377,11 @@ function(dir, stats, initialAdd, depth, target, wh, callback) {
this._addToNodeFs(path, initialAdd, wh, depth + 1);
}
}.bind(this)).on('end', function() {
if (throttler) throttler.clear();
if (done) done();

// Run any pending reads that may be queued
debouncedRead.flush();

// Files that absent in current directory snapshot
// but present in previous emit `remove` event
// and are removed from @watched[directory].
Expand All @@ -400,20 +400,35 @@ function(dir, stats, initialAdd, depth, target, wh, callback) {
}.bind(this)).on('error', this._handleError.bind(this));
}.bind(this);

// Create a debounced version of read
debouncedRead = debounce(read, 1000, {
leading: true,
trailing: true,
maxWait: 1000
});

var closer;

if (this.options.depth == null || depth <= this.options.depth) {
if (!target) read(dir, initialAdd, callback);
closer = this._watchWithNodeFs(dir, function(dirPath, stats) {
// if current directory is removed, do nothing
if (stats && stats.mtime.getTime() === 0) return;

read(dirPath, false);
debouncedRead(dirPath, false);
});
} else {
callback();
}
return closer;

// Close function that calls fs closer and cancels any pending debounced reads
return function () {
if (closer) {
closer();
}

// Cancel any pending reads that may be queued
debouncedRead.cancel();
};
};

// Private method: Handle added file, directory, or glob pattern.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"inherits": "^2.0.1",
"is-binary-path": "^1.0.0",
"is-glob": "^4.0.0",
"lodash.debounce": "^4.0.8",
"normalize-path": "^2.1.1",
"path-is-absolute": "^1.0.0",
"readdirp": "^2.0.0",
Expand Down