Skip to content

Commit

Permalink
consolidate FSEventWatchers after reaching a threshhold of duplicate …
Browse files Browse the repository at this point in the history
…common path prefixes
  • Loading branch information
ashaffer committed May 17, 2016
1 parent 7b921e8 commit 0630964
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion lib/fsevents-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ function createFSEventsInstance(path, callback) {
function setFSEventsListener(path, realPath, listener, rawEmitter) {
var watchPath = sysPath.extname(path) ? sysPath.dirname(path) : path;
var watchContainer;
var parentPath = parentOf(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,9 +98,36 @@ function setFSEventsListener(path, realPath, listener, rawEmitter) {
};
}

var consolidateThreshhold = 10;

// Decide whether or not we should start a new higher-level
// parent watcher
function couldConsolidate(path) {
var candidates = {};
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
}

// Find the parent of a given path
function parentOf (path) {
return path.split(sysPath.sep).slice(0, -1).join(sysPath.sep)
}

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

// determines subdirectory traversal levels from root to path
Expand Down

0 comments on commit 0630964

Please sign in to comment.