Skip to content

Commit

Permalink
Even better fsevents instance reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
es128 committed Nov 12, 2014
1 parent d323768 commit bb1531a
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions index.js
Expand Up @@ -238,13 +238,33 @@ function createFSEventsInstance(path, callback) {
}

function setFSEventsListener(path, callback) {
var watchContainer = FSEventsWatchers[path];
if (watchContainer) {
watchContainer.listeners.push(callback);
var watchPath = sysPath.extname(path) ? sysPath.dirname(path) : path;
var watchContainer;

var resolvedPath = sysPath.resolve(path);
function filteredCallback(fullPath, flags) {
if (
fullPath === resolvedPath ||
!fullPath.indexOf(resolvedPath + sysPath.sep)
) callback(fullPath, flags);
}

if (
watchPath in FSEventsWatchers ||
// check if there is already a watcher on a parent path
Object.keys(FSEventsWatchers).some(function(watchedPath) {
if (!watchPath.indexOf(watchedPath)) {
watchPath = watchedPath;
return true;
}
})
) {
watchContainer = FSEventsWatchers[watchPath];
watchContainer.listeners.push(filteredCallback);
} else {
watchContainer = FSEventsWatchers[path] = {
listeners: [callback],
watcher: createFSEventsInstance(path, function(fullPath, flags) {
watchContainer = FSEventsWatchers[watchPath] = {
listeners: [filteredCallback],
watcher: createFSEventsInstance(watchPath, function(fullPath, flags) {
watchContainer.listeners.forEach(function(callback) {
callback(fullPath, flags);
});
Expand Down

1 comment on commit bb1531a

@MylesBorins
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is causing false negatives for files in the $TMPDIR on osx and for any file that is a symlink
: (

Please sign in to comment.