From 06309646c0f3650183290637e63b12f0f5a44458 Mon Sep 17 00:00:00 2001 From: Andrew Shaffer Date: Tue, 17 May 2016 15:53:03 -0700 Subject: [PATCH] consolidate FSEventWatchers after reaching a threshhold of duplicate common path prefixes --- lib/fsevents-handler.js | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/fsevents-handler.js b/lib/fsevents-handler.js index f211346a..b16c28a2 100644 --- a/lib/fsevents-handler.js +++ b/lib/fsevents-handler.js @@ -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; @@ -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