pathwatcher
Advanced tools
Comparing version 0.5.0 to 0.6.0
117
lib/main.js
(function() { | ||
var EventEmitter, HandleWatcher, PathWatcher, binding, fs, handleWatchers, | ||
var EventEmitter, HandleMap, HandleWatcher, PathWatcher, binding, fs, handleWatchers, path, | ||
__hasProp = {}.hasOwnProperty, | ||
@@ -8,2 +8,4 @@ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; | ||
HandleMap = binding.HandleMap; | ||
EventEmitter = require('events').EventEmitter; | ||
@@ -13,8 +15,10 @@ | ||
handleWatchers = {}; | ||
path = require('path'); | ||
binding.setCallback(function(event, handle, path) { | ||
var _ref; | ||
handleWatchers = new HandleMap; | ||
return (_ref = handleWatchers[handle]) != null ? _ref.onEvent(event, path) : void 0; | ||
binding.setCallback(function(event, handle, filePath, oldFilePath) { | ||
if (handleWatchers.has(handle)) { | ||
return handleWatchers.get(handle).onEvent(event, filePath, oldFilePath); | ||
} | ||
}); | ||
@@ -30,3 +34,3 @@ | ||
HandleWatcher.prototype.onEvent = function(event, path) { | ||
HandleWatcher.prototype.onEvent = function(event, filePath, oldFilePath) { | ||
var detectRename, | ||
@@ -41,5 +45,5 @@ _this = this; | ||
if (err) { | ||
_this.path = path; | ||
_this.path = filePath; | ||
_this.start(); | ||
return _this.emit('change', 'rename', path); | ||
return _this.emit('change', 'rename', filePath); | ||
} else { | ||
@@ -55,4 +59,7 @@ _this.start(); | ||
return this.close(); | ||
case 'unknown': | ||
throw new Error("Received unknown event for path: " + this.path); | ||
break; | ||
default: | ||
return this.emit('change', event, path); | ||
return this.emit('change', event, filePath, oldFilePath); | ||
} | ||
@@ -63,3 +70,3 @@ }; | ||
this.handle = binding.watch(this.path); | ||
return handleWatchers[this.handle] = this; | ||
return handleWatchers.add(this.handle, this); | ||
}; | ||
@@ -74,5 +81,5 @@ | ||
HandleWatcher.prototype.close = function() { | ||
if (handleWatchers[this.handle] != null) { | ||
if (handleWatchers.has(this.handle)) { | ||
binding.unwatch(this.handle); | ||
return delete handleWatchers[this.handle]; | ||
return handleWatchers.remove(this.handle); | ||
} | ||
@@ -88,11 +95,24 @@ }; | ||
PathWatcher.prototype.isWatchingParent = false; | ||
PathWatcher.prototype.path = null; | ||
PathWatcher.prototype.handleWatcher = null; | ||
function PathWatcher(path, callback) { | ||
var handle, watcher, _ref, | ||
function PathWatcher(filePath, callback) { | ||
var stats, watcher, _i, _len, _ref, _ref1, | ||
_this = this; | ||
for (handle in handleWatchers) { | ||
watcher = handleWatchers[handle]; | ||
if (watcher.path === path) { | ||
this.path = filePath; | ||
if (process.platform === 'win32') { | ||
stats = fs.statSync(filePath); | ||
this.isWatchingParent = !stats.isDirectory(); | ||
} | ||
if (this.isWatchingParent) { | ||
filePath = path.dirname(filePath); | ||
} | ||
_ref = handleWatchers.values(); | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
watcher = _ref[_i]; | ||
if (watcher.path === filePath) { | ||
this.handleWatcher = watcher; | ||
@@ -102,10 +122,45 @@ break; | ||
} | ||
if ((_ref = this.handleWatcher) == null) { | ||
this.handleWatcher = new HandleWatcher(path); | ||
if ((_ref1 = this.handleWatcher) == null) { | ||
this.handleWatcher = new HandleWatcher(filePath); | ||
} | ||
this.onChange = function(event, path) { | ||
if (typeof callback === 'function') { | ||
callback.call(_this, event, path); | ||
this.onChange = function(event, newFilePath, oldFilePath) { | ||
switch (event) { | ||
case 'rename': | ||
case 'change': | ||
case 'delete': | ||
if (event === 'rename') { | ||
_this.path = newFilePath; | ||
} | ||
if (typeof callback === 'function') { | ||
callback.call(_this, event, newFilePath); | ||
} | ||
return _this.emit('change', event, newFilePath); | ||
case 'child-rename': | ||
if (_this.isWatchingParent) { | ||
if (_this.path === oldFilePath) { | ||
return _this.onChange('rename', newFilePath); | ||
} | ||
} else { | ||
return _this.onChange('change', ''); | ||
} | ||
break; | ||
case 'child-delete': | ||
if (_this.isWatchingParent) { | ||
if (_this.path === newFilePath) { | ||
return _this.onChange('delete', null); | ||
} | ||
} else { | ||
return _this.onChange('change', ''); | ||
} | ||
break; | ||
case 'child-change': | ||
if (_this.isWatchingParent && _this.path === newFilePath) { | ||
return _this.onChange('change', ''); | ||
} | ||
break; | ||
case 'child-create': | ||
if (!_this.isWatchingParent) { | ||
return _this.onChange('change', ''); | ||
} | ||
} | ||
return _this.emit('change', event, path); | ||
}; | ||
@@ -130,17 +185,19 @@ this.handleWatcher.on('change', this.onChange); | ||
exports.closeAllWatchers = function() { | ||
var handle, watcher; | ||
var watcher, _i, _len, _ref; | ||
for (handle in handleWatchers) { | ||
watcher = handleWatchers[handle]; | ||
_ref = handleWatchers.values(); | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
watcher = _ref[_i]; | ||
watcher.close(); | ||
} | ||
return handleWatchers = {}; | ||
return handleWatchers.clear(); | ||
}; | ||
exports.getWatchedPaths = function() { | ||
var handle, paths, watcher; | ||
var paths, watcher, _i, _len, _ref; | ||
paths = []; | ||
for (handle in handleWatchers) { | ||
watcher = handleWatchers[handle]; | ||
_ref = handleWatchers.values(); | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
watcher = _ref[_i]; | ||
paths.push(watcher.path); | ||
@@ -147,0 +204,0 @@ } |
@@ -6,3 +6,3 @@ { | ||
"description": "Watch file events", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"licenses": [ | ||
@@ -23,3 +23,3 @@ { | ||
"scripts": { | ||
"prepublish": "grunt clean lint coffee", | ||
"prepublish": "grunt clean coffee lint", | ||
"test": "grunt test" | ||
@@ -33,6 +33,7 @@ }, | ||
"coffee-script": "~1.6.2", | ||
"jasmine-focused": "~0.7.0", | ||
"jasmine-focused": "~0.15.0", | ||
"rimraf": "~2.2.0", | ||
"node-cpplint": "~0.1.4", | ||
"grunt-coffeelint": "0.0.6" | ||
"grunt-coffeelint": "0.0.6", | ||
"temp": "0.6.0" | ||
}, | ||
@@ -39,0 +40,0 @@ "dependencies": { |
@@ -27,8 +27,13 @@ # Path Watcher Node Module | ||
The listener callback gets two arguments `(event, path)`. `event` is either | ||
'rename' or 'change', and `path` is the path of the file which triggered the | ||
The listener callback gets two arguments `(event, path)`. `event` is 'rename', | ||
'delete' or 'change', and `path` is the path of the file which triggered the | ||
event. | ||
For directories, the 'change' event is emitted when a file or directory under | ||
the watched directory got created or deleted. And the `PathWatcher.watch` is | ||
not recursive, so changes of subdirectories under the watched directory would | ||
not be detected. | ||
### PathWatcher.close() | ||
Stop watching for changes on the given `PathWatcher`. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
30540
12
167
39
10