Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watch fix #5

Merged
merged 5 commits into from
Jun 7, 2012
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function FSWatcher(options) {
FSWatcher.prototype = Object.create(EventEmitter.prototype);

FSWatcher.prototype._getWatchedDir = function(directory) {
directory = directory.replace(/\/$/,"");
Copy link
Owner

Choose a reason for hiding this comment

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

What about win32?

Copy link
Owner

Choose a reason for hiding this comment

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

also, a space after comma and ''

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Little clueless when it comes to windows. Is it just a matter of replacing both forward and back slashes? That's what you are referring to ya?

Copy link
Owner

Choose a reason for hiding this comment

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

yes

if (this.watched[directory] == null) this.watched[directory] = [];
return this.watched[directory];
};
Expand Down Expand Up @@ -74,11 +75,14 @@ FSWatcher.prototype._watch = function(item, callback) {
var _this = this;
var options, watcher;
if (callback == null) callback = function() {};
var parent = this._getWatchedDir(sysPath.dirname(item));
var directory = sysPath.dirname(item)
var basename = sysPath.basename(item);
var parent = this._getWatchedDir(directory);
// Prevent memory leaks.
if (parent.indexOf(basename) >= 0) return;
parent.push(basename);

_this._addToWatchedDir(directory, basename);

if (process.platform === 'win32') {
watcher = fs.watch(item, {
persistent: this.options.persistent
Expand Down Expand Up @@ -136,8 +140,7 @@ FSWatcher.prototype._handleDir = function(directory) {
return current.indexOf(file) < 0;
})
.forEach(function(file) {
_this._removeFromWatchedDir(directory, file);
_this.emit('unlink', sysPath.join(directory, file));
_this._remove(directory,file);
});

// Files that present in current directory snapshot
Expand All @@ -148,7 +151,6 @@ FSWatcher.prototype._handleDir = function(directory) {
return previous.indexOf(file) < 0;
})
.forEach(function(file) {
_this._addToWatchedDir(directory, file);
_this._handle(sysPath.join(directory, file));
});
});
Expand All @@ -157,6 +159,33 @@ FSWatcher.prototype._handleDir = function(directory) {
this._watch(directory, read);
};

// Private: Handles emitting unlink events for
// files and directories, and via recursion, for
// files and directories within directories that are unlinked
//
// directory - string, directory within which the following item is located
// item - string, base path of item/directory
//
// Returns nothing.
FSWatcher.prototype._remove = function(directory, item) {
var _this = this;

// if what is being deleted is a directory, get that directory's paths
// for recursive deleting and cleaning of watched object
// if it is not a directory, nestedDirectoryChildren will be empty array
var fullPath = sysPath.join(directory,item);
Copy link
Owner

Choose a reason for hiding this comment

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

a space

var nestedDirectoryChildren = _this._getWatchedDir(fullPath).slice(); // need clone

// remove this file/directory from watched list and emit unlink
_this._removeFromWatchedDir(directory, item);
_this.emit('unlink', fullPath);

// recurse
nestedDirectoryChildren.forEach(function(nestedItem) {
_this._remove(fullPath, nestedItem);
});
};

// Private: Handle added file or directory.
// Delegates call to _handleFile / _handleDir after checks.
//
Expand Down