Skip to content

Commit

Permalink
Merge pull request #483 from ashaffer/master
Browse files Browse the repository at this point in the history
Consolidate common folders to avoid hitting the FSEvent limit
  • Loading branch information
paulmillr committed May 18, 2016
2 parents 3b6f870 + 7c92a19 commit 2c7fed9
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/fsevents-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ try { fsevents = require('fsevents'); } catch (error) {}
// (may be shared across chokidar FSWatcher instances)
var FSEventsWatchers = Object.create(null);

// Threshold of duplicate path prefixes at which to start
// consolidating going forward
var consolidateThreshhold = 10;

// Private function: Instantiates the fsevents interface

// * path - string, path to be watched
Expand All @@ -34,6 +38,15 @@ function createFSEventsInstance(path, callback) {
function setFSEventsListener(path, realPath, listener, rawEmitter) {
var watchPath = sysPath.extname(path) ? sysPath.dirname(path) : path;
var watchContainer;
var parentPath = sysPath.dirname(watchPath);

// If we've accumulated a substantial number of paths that
// could have been consolidated by watching one directory
// above the current one, create a watcher on the parent
// path instead, so that we do consolidate going forward.
if (couldConsolidate(parentPath)) {
watchPath = parentPath;
}

var resolvedPath = sysPath.resolve(path);
var hasSymlink = resolvedPath !== realPath;
Expand Down Expand Up @@ -89,6 +102,25 @@ function setFSEventsListener(path, realPath, listener, rawEmitter) {
};
}

// Decide whether or not we should start a new higher-level
// parent watcher
function couldConsolidate(path) {
var keys = Object.keys(FSEventsWatchers);
var count = 0;

for (var i = 0, len = keys.length; i < len; ++i) {
var watchPath = keys[i];
if (watchPath.indexOf(path) === 0) {
count++;
if (count >= consolidateThreshhold) {
return true;
}
}
}

return false;
}

// returns boolean indicating whether fsevents can be used
function canUse() {
return fsevents && Object.keys(FSEventsWatchers).length < 128;
Expand Down

0 comments on commit 2c7fed9

Please sign in to comment.