Skip to content

Commit

Permalink
Merge pull request #6565 from adobe/jasonsanjose/issue-6554
Browse files Browse the repository at this point in the history
Change FileWatcherDomain unwatchPath to handle directories and descendants; avoid recursive visit() for unwatching
  • Loading branch information
peterflynn committed Jan 20, 2014
2 parents 734c236 + 37a29e4 commit 0e52dbd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
26 changes: 17 additions & 9 deletions src/filesystem/FileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,17 @@ define(function (require, exports, module) {
impl[commandName].call(impl, entry.fullPath, requestCb);
}.bind(this), callback);
}
} else {
} else if (shouldWatch) {
// The impl can't handle recursive watch requests, so it's up to the
// filesystem to recursively watch or unwatch all subdirectories.
// filesystem to recursively watch all subdirectories.
this._enqueueWatchRequest(function (requestCb) {
// First construct a list of entries to watch or unwatch
var entriesToWatchOrUnwatch = [],
watchOrUnwatch = impl[commandName].bind(impl);
var entriesToWatch = [];

var visitor = function (child) {
if (watchedRoot.filter(child.name, child.parentPath)) {
if (child.isDirectory || child === watchedRoot.entry) {
entriesToWatchOrUnwatch.push(child);
entriesToWatch.push(child);
}
return true;
}
Expand All @@ -273,28 +272,33 @@ define(function (require, exports, module) {

entry.visit(visitor, function (err) {
if (err) {
// Unexpected error
requestCb(err);
return;
}

// Then watch or unwatched all these entries
var count = entriesToWatchOrUnwatch.length;
var count = entriesToWatch.length;
if (count === 0) {
requestCb(null);
return;
}

var watchOrUnwatchCallback = function () {
var watchCallback = function () {
if (--count === 0) {
requestCb(null);
}
};

entriesToWatchOrUnwatch.forEach(function (entry) {
watchOrUnwatch(entry.fullPath, watchOrUnwatchCallback);
entriesToWatch.forEach(function (entry) {
impl.watchPath(entry.fullPath, watchCallback);
});
});
}, callback);
} else {
this._enqueueWatchRequest(function (requestCb) {
impl.unwatchPath(entry.fullPath, requestCb);
}, callback);
}
};

Expand Down Expand Up @@ -714,6 +718,10 @@ define(function (require, exports, module) {
}

var watchOrUnwatchCallback = function (err) {
if (err) {
console.error("FileSystem error in _handleDirectoryChange after watch/unwatch entries: " + err);
}

if (--counter === 0) {
callback(addedEntries, removedEntries);
}
Expand Down
17 changes: 15 additions & 2 deletions src/filesystem/impls/appshell/node/FileWatcherDomain.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ var _domainManager,
_watcherMap = {};

/**
* @private
* Un-watch a file or directory.
* @param {string} path File or directory to unwatch.
*/
function unwatchPath(path) {
function _unwatchPath(path) {
var watcher = _watcherMap[path];

if (watcher) {
Expand All @@ -84,6 +85,18 @@ function unwatchPath(path) {
}
}

/**
* Un-watch a file or directory. For directories, unwatch all descendants.
* @param {string} path File or directory to unwatch.
*/
function unwatchPath(path) {
Object.keys(_watcherMap).forEach(function (keyPath) {
if (keyPath.indexOf(path) === 0) {
_unwatchPath(keyPath);
}
});
}

/**
* Watch a file or directory.
* @param {string} path File or directory to watch.
Expand Down Expand Up @@ -171,7 +184,7 @@ function init(domainManager) {
"unwatchPath",
unwatchPath,
false,
"Stop watching a file or directory",
"Stop watching a single file or a directory and it's descendants",
[{
name: "path",
type: "string",
Expand Down

0 comments on commit 0e52dbd

Please sign in to comment.