Skip to content

Commit

Permalink
Refactor windows EPERM fix
Browse files Browse the repository at this point in the history
Resolves gh-88
  • Loading branch information
es128 committed Oct 10, 2014
1 parent 3226281 commit 2913fae
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,25 +274,24 @@ FSWatcher.prototype._watch = function(item, callback) {
callback(item);
}, 0);
});
watcher.on('error', this._emitError);
var _emitError = this._emitError;
watcher.on('error', function(error) {
// Workaround for the "Windows rough edge" regarding the deletion of directories
// (https://github.com/joyent/node/issues/4337)
if (isWindows && error.code === 'EPERM') {
fs.exists(item, function(exists) {
if (exists) _emitError(error);
});
} else {
_emitError(error);
}
});
this.watchers.push(watcher);
}
};

// Workaround for the "Windows rough edge" regarding the deletion of directories
// (https://github.com/joyent/node/issues/4337)
FSWatcher.prototype._emitError = function(error) {
var emit = (function() {
this.emit('error', error);
}).bind(this);

if (isWindows && error.code === 'EPERM') {
fs.exists(item, function(exists) {
if (exists) emit();
});
} else {
emit();
}
this.emit('error', error);
};

// Private: Emit `change` event once and watch file to emit it in the future
Expand Down

0 comments on commit 2913fae

Please sign in to comment.