diff --git a/src/filesystem/FileSystem.js b/src/filesystem/FileSystem.js index 993cd50ece..3300b25ec9 100644 --- a/src/filesystem/FileSystem.js +++ b/src/filesystem/FileSystem.js @@ -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; } @@ -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); } }; @@ -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); } diff --git a/src/filesystem/impls/appshell/node/FileWatcherDomain.js b/src/filesystem/impls/appshell/node/FileWatcherDomain.js index dbbc051063..82b6c8e297 100644 --- a/src/filesystem/impls/appshell/node/FileWatcherDomain.js +++ b/src/filesystem/impls/appshell/node/FileWatcherDomain.js @@ -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) { @@ -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. @@ -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",