Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
correctly transfer stats from watcher domain
Browse files Browse the repository at this point in the history
  • Loading branch information
zaggino committed Aug 5, 2016
1 parent 77121c8 commit 78ddc82
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/filesystem/FileSystemStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ define(function (require, exports, module) {

this._isFile = isFile;
this._isDirectory = !isFile;
this._mtime = options.mtime;
this._mtime = options.mtime instanceof Date ? options.mtime : new Date(options.mtime);
this._size = options.size;
this._hash = options.hash;

Expand Down
41 changes: 21 additions & 20 deletions src/filesystem/impls/appshell/AppshellFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,33 +101,34 @@ define(function (require, exports, module) {

/**
* Event handler for the Node fileWatcher domain's change event.
* event values from chokidar: "change", "ready", "add", "addDir", "unlink", "unlinkDir"
*
* @param {jQuery.Event} The underlying change event
* @param {string} path The path that is reported to have changed
* @param {string} parentDirPath The path to the directory holding entry that has changed
* @param {string} event The type of the event: either "change" or "rename"
* @param {string=} filename The name of the file that changed.
* @param {string=} entryName The name of the file/directory that has changed
* @private
*/
function _fileWatcherChange(evt, path, event, filename, stats) {
console.log('_fileWatcherChange', evt, path, event, filename, stats);
function _fileWatcherChange(evt, event, parentDirPath, entryName, statsObj) {
// console.log('_fileWatcherChange', event, parentDirPath, entryName, statsObj);

var change;

if (stats) {
stats.mtime = new Date(stats.mtime);
}

if (event === "change") {
// Only register change events if filename is passed
if (filename) {
// an existing file was modified; stats are passed
change = path + filename;
_enqueueChange(change, stats);
}
} else if (event === "rename") {
// a new file was created; no stats are passed
change = path;
_enqueueChange(change, null);
switch (event) {
case "change":
case "ready":
// an existing file/directory was modified; stats are passed if available
var fsStats = statsObj ? new FileSystemStats(statsObj) : null;
_enqueueChange(parentDirPath + entryName, fsStats);
break;
case "add":
case "addDir":
case "unlink":
case "unlinkDir":
// file/directory was created/deleted; fire change on parent to reload contents
_enqueueChange(parentDirPath, null);
break;
default:
console.error("Unexpected 'change' event:", event);
}
}

Expand Down
35 changes: 28 additions & 7 deletions src/filesystem/impls/appshell/node/FileWatcherDomain.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ function unwatchPath(path) {
});
}

/**
* Transform Node's native fs.stats to a format that can be sent through domain
* @param {stats} Node's fs.stats result
* @return {object} Can be consumed by new FileSystemStats(object); in Brackets
*/
function normalizeStats(nodeFsStats) {
// from shell: If "filename" is a symlink,
// realPath should be the actual path to the linked object
// not implemented in shell yet
return {
isFile: nodeFsStats.isFile(),
isDirectory: nodeFsStats.isDirectory(),
mtime: nodeFsStats.mtime,
size: nodeFsStats.size,
realPath: null,
hash: nodeFsStats.mtime.getTime()
};
}

/**
* Watch a file or directory.
* @param {string} path File or directory to watch.
Expand All @@ -84,15 +103,17 @@ function watchPath(path, ignored) {
ignored: ignored
});

watcher.on("all", function (event, filename, stats) {
watcher.on("all", function (event, filename, nodeFsStats) {
if (event === "raw" || event === "error" || !filename) {
return;
}
// make sure stats are normalized for domain transfer
var statsObj = nodeFsStats ? normalizeStats(nodeFsStats) : null;
// make sure it's normalized
filename = filename.replace(/\\/g, "/");
var parent = fspath.dirname(filename) + "/";
var name = fspath.basename(filename);
_domainManager.emitEvent("fileWatcher", "change", [parent, event, name, stats]);
var parentDirPath = fspath.dirname(filename) + "/";
var entryName = fspath.basename(filename);
_domainManager.emitEvent("fileWatcher", "change", [event, parentDirPath, entryName, statsObj]);
});

_watcherMap[path] = watcher;
Expand Down Expand Up @@ -167,10 +188,10 @@ function init(domainManager) {
"fileWatcher",
"change",
[
{name: "path", type: "string"},
{name: "event", type: "string"},
{name: "filename", type: "string"},
{name: "stats", type: "object"}
{name: "parentDirPath", type: "string"},
{name: "entryName", type: "string"},
{name: "statsObj", type: "object"}
]
);

Expand Down

0 comments on commit 78ddc82

Please sign in to comment.