Skip to content

Commit

Permalink
Undo conflicting pull request #690
Browse files Browse the repository at this point in the history
  • Loading branch information
srguiwiz committed May 27, 2018
1 parent 5261384 commit 28a70c4
Showing 1 changed file with 9 additions and 24 deletions.
33 changes: 9 additions & 24 deletions lib/nodefs-handler.js
Expand Up @@ -4,7 +4,6 @@ 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 @@ -342,12 +341,15 @@ 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 @@ -378,11 +380,9 @@ 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 @@ -401,35 +401,20 @@ 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;
debouncedRead(dirPath, false);

read(dirPath, false);
});
} else {
callback();
}

// 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();
};
return closer;
};

// Private method: Handle added file, directory, or glob pattern.
Expand Down

0 comments on commit 28a70c4

Please sign in to comment.