Comparing version 0.10.5 to 0.10.6
69
index.js
@@ -234,12 +234,57 @@ 'use strict'; | ||
// FS Events helper. | ||
var FSEventsInstanceCount = 0; | ||
// FS Events helpers. | ||
var FSEventsWatchers = Object.create(null); | ||
function createFSEventsInstance(path, callback) { | ||
FSEventsInstanceCount++; | ||
return (new fsevents(path)).on('fsevent', callback).start(); | ||
} | ||
function setFSEventsListener(path, callback) { | ||
var watchPath = sysPath.extname(path) ? sysPath.dirname(path) : path; | ||
var watchContainer; | ||
var resolvedPath = sysPath.resolve(path); | ||
function filteredCallback(fullPath, flags) { | ||
if ( | ||
fullPath === resolvedPath || | ||
!fullPath.indexOf(resolvedPath + sysPath.sep) | ||
) callback(fullPath, flags); | ||
} | ||
if ( | ||
watchPath in FSEventsWatchers || | ||
// check if there is already a watcher on a parent path | ||
Object.keys(FSEventsWatchers).some(function(watchedPath) { | ||
if (!watchPath.indexOf(watchedPath)) { | ||
watchPath = watchedPath; | ||
return true; | ||
} | ||
}) | ||
) { | ||
watchContainer = FSEventsWatchers[watchPath]; | ||
watchContainer.listeners.push(filteredCallback); | ||
} else { | ||
watchContainer = FSEventsWatchers[watchPath] = { | ||
listeners: [filteredCallback], | ||
watcher: createFSEventsInstance(watchPath, function(fullPath, flags) { | ||
watchContainer.listeners.forEach(function(callback) { | ||
callback(fullPath, flags); | ||
}); | ||
}) | ||
}; | ||
} | ||
var listenerIndex = watchContainer.listeners.length - 1; | ||
return { | ||
stop: function() { | ||
delete watchContainer.listeners[listenerIndex]; | ||
if (!Object.keys(watchContainer.listeners).length) { | ||
watchContainer.watcher.stop(); | ||
delete FSEventsWatchers[watchPath]; | ||
} | ||
} | ||
}; | ||
} | ||
FSWatcher.prototype._watchWithFsEvents = function(watchPath) { | ||
if (this._isIgnored(watchPath)) return; | ||
var watcher = createFSEventsInstance(watchPath, function(fullPath, flags) { | ||
var watcher = setFSEventsListener(watchPath, function(fullPath, flags) { | ||
var info = fsevents.getInfo(fullPath, flags); | ||
@@ -349,6 +394,10 @@ var path = sysPath.join(watchPath, sysPath.relative(watchPath, fullPath)); | ||
} else { | ||
var watcher = fs.watch(item, options, function(event, path) { | ||
callback(item); | ||
}); | ||
var _handleError = this._handleError; | ||
var _handleEvent = function() {callback(item);}; | ||
var _handleError = this._handleError.bind(this); | ||
var watcher; | ||
try { | ||
watcher = fs.watch(item, options, _handleEvent); | ||
} catch (error) { | ||
return _handleError(error); | ||
} | ||
watcher.on('error', function(error) { | ||
@@ -533,3 +582,3 @@ // Workaround for https://github.com/joyent/node/issues/4337 | ||
if (this.options.useFsEvents && FSEventsInstanceCount < 400) { | ||
if (this.options.useFsEvents && Object.keys(FSEventsWatchers).length < 128) { | ||
files.forEach(this._addToFsEvents, this); | ||
@@ -559,3 +608,2 @@ } else if (!this.closed) { | ||
var useFsEvents = this.options.useFsEvents; | ||
var method = useFsEvents ? 'stop' : 'close'; | ||
@@ -566,3 +614,2 @@ this.closed = true; | ||
watcher.stop(); | ||
FSEventsInstanceCount--; | ||
} else { | ||
@@ -569,0 +616,0 @@ watcher.close(); |
{ | ||
"name": "chokidar", | ||
"description": "A neat wrapper around node.js fs.watch / fs.watchFile / fsevents.", | ||
"version": "0.10.5", | ||
"version": "0.10.6", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "fs", |
@@ -11,14 +11,16 @@ # Chokidar | ||
* Doesn't report events at all when using editors like Sublime on OS X. | ||
* Doesn't use OS X internals for **fast low-CPU watching on OS X** (no other fs watch module does this!). | ||
* Sometimes reports events twice. | ||
* Has only one non-useful event: `rename`. | ||
* Often reports events twice. | ||
* Emits most changes as `rename`. | ||
* Has [a lot of other issues](https://github.com/joyent/node/search?q=fs.watch&type=Issues) | ||
* Does not provide an easy way to recursively watch file trees. | ||
Node.js `fs.watchFile`: | ||
* Almost as shitty in event tracking. | ||
* Almost as bad at event handling. | ||
* Also does not provide any recursive watching. | ||
* Results in high CPU utilization. | ||
Other node.js watching libraries: | ||
* Are not using ultra-fast non-polling watcher implementation on OS X | ||
* Are not using ultra-fast non-polling fsevents watcher implementation on OS X | ||
@@ -89,2 +91,4 @@ Chokidar resolves these problems. | ||
polling for binary files (see extensions in src/is-binary). | ||
* `options.useFsEvents` (default: `true` on OS X). Whether to use the `fsevents` watching interface if | ||
available. When `true` and `fsevents` is available, it supercedes the `usePolling` setting. | ||
* `options.usePolling` (default: `false` on Windows, `true` on Linux and OS X). Whether to use fs.watchFile | ||
@@ -91,0 +95,0 @@ (backed by polling), or fs.watch. If polling leads to high CPU utilization, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27736
564
127