-
-
Notifications
You must be signed in to change notification settings - Fork 592
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
Watch fix #5
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2d7a46a
re-ordering where watch directory is added, fixing trailing slash issue
dbashford 783ee18
only add to list once, and only via _addToWatchedDir method
dbashford e7f625a
recursively handle unlinks for nested directories/files
dbashford 9595bd0
spacing
dbashford 977567e
removing trailing back and forward slashes
dbashford File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ function FSWatcher(options) { | |
FSWatcher.prototype = Object.create(EventEmitter.prototype); | ||
|
||
FSWatcher.prototype._getWatchedDir = function(directory) { | ||
directory = directory.replace(/\/$/,""); | ||
if (this.watched[directory] == null) this.watched[directory] = []; | ||
return this.watched[directory]; | ||
}; | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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)); | ||
}); | ||
}); | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
// | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about win32?
There was a problem hiding this comment.
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
''
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes