Comparing version 0.10.6 to 0.10.7
148
index.js
@@ -276,3 +276,3 @@ 'use strict'; | ||
return { | ||
stop: function() { | ||
close: function() { | ||
delete watchContainer.listeners[listenerIndex]; | ||
@@ -367,2 +367,111 @@ if (!Object.keys(watchContainer.listeners).length) { | ||
// Node.js native watcher helpers | ||
var FsWatchInstances = Object.create(null); | ||
function createFsWatchInstance(item, options, callback, errHandler) { | ||
var handleEvent = function() {callback(item);}; | ||
try { | ||
return fs.watch(item, options, handleEvent); | ||
} catch (error) { | ||
errHandler(error); | ||
} | ||
} | ||
function fsWatchBroadcast(item, type, value) { | ||
FsWatchInstances[item][type].forEach(function(callback) { | ||
callback(value); | ||
}); | ||
} | ||
function setFsWatchListener(item, options, callback, errHandler) { | ||
var container = container; | ||
if (!options.persistent) { | ||
return createFsWatchInstance(item, options, callback, errHandler); | ||
} else if (!container) { | ||
var watcher = createFsWatchInstance( | ||
item, | ||
options, | ||
fsWatchBroadcast.bind(null, item, 'listeners'), | ||
errHandler // no need to use broadcast here | ||
); | ||
var broadcastErr = fsWatchBroadcast.bind(null, item, 'errHandlers'); | ||
watcher.on('error', function(error) { | ||
// Workaround for https://github.com/joyent/node/issues/4337 | ||
if (isWin32 && error.code === 'EPERM') { | ||
fs.exists(item, function(exists) { | ||
if (exists) broadcastErr(error); | ||
}); | ||
} else { | ||
broadcastErr(error); | ||
} | ||
}); | ||
container = FsWatchInstances[item] = { | ||
listeners: [callback], | ||
errHandlers: [errHandler], | ||
watcher: watcher | ||
}; | ||
} else { | ||
container.listeners.push(callback); | ||
container.errHandlers.push(errHandler); | ||
} | ||
var listenerIndex = container.listeners.length - 1; | ||
return { | ||
close: function() { | ||
delete container.listeners[listenerIndex]; | ||
delete container.errHandlers[listenerIndex]; | ||
if (!Object.keys(container.listeners).length) { | ||
container.watcher.close(); | ||
delete FsWatchInstances[item]; | ||
} | ||
} | ||
}; | ||
} | ||
var FsWatchFileInstances = Object.create(null); | ||
function setFsWatchFileListener(item, absPath, options, callback) { | ||
var container = FsWatchFileInstances[absPath]; | ||
var listeners = []; | ||
if ( | ||
container && ( | ||
container.options.persistent < options.persistent || | ||
container.options.interval > options.interval | ||
) | ||
) { | ||
// "Upgrade" the watcher to persistence or a quicker interval. | ||
// This creates some unlikely edge case issues if the user mixes | ||
// settings in a very weird way, but solving for those cases | ||
// doesn't seem worthwhile for the added complexity. | ||
listeners = container.listeners; | ||
fs.unwatchFile(absPath); | ||
container = false; | ||
} | ||
if (!container) { | ||
listeners.push(callback); | ||
container = FsWatchFileInstances[absPath] = { | ||
listeners: listeners, | ||
options: options, | ||
watcher: fs.watchFile(absPath, options, function(curr, prev) { | ||
var currmtime = curr.mtime.getTime(); | ||
if (currmtime > prev.mtime.getTime() || currmtime === 0) { | ||
container.listeners.forEach(function(callback) { | ||
callback(item, curr); | ||
}); | ||
} | ||
}) | ||
}; | ||
} else { | ||
container.listeners.push(callback); | ||
console.log(absPath, container) | ||
} | ||
var listenerIndex = container.listeners.length - 1; | ||
return { | ||
close: function() { | ||
delete container.listeners[listenerIndex]; | ||
if (!Object.keys(container.listeners).length) { | ||
fs.unwatchFile(absPath); | ||
delete FsWatchFileInstances[absPath]; | ||
} | ||
} | ||
}; | ||
} | ||
// Private: Watch file for changes with fs.watchFile or fs.watch. | ||
@@ -384,33 +493,12 @@ | ||
var watcher; | ||
if (this.options.usePolling) { | ||
options.interval = this.enableBinaryInterval && isBinaryPath(basename) ? | ||
this.options.binaryInterval : this.options.interval; | ||
var listener = this.listeners[absolutePath] = function(curr, prev) { | ||
var currmtime = curr.mtime.getTime(); | ||
if (currmtime > prev.mtime.getTime() || currmtime === 0) { | ||
callback(item, curr); | ||
} | ||
}; | ||
fs.watchFile(absolutePath, options, listener); | ||
watcher = setFsWatchFileListener(item, absolutePath, options, callback); | ||
} else { | ||
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) { | ||
// Workaround for https://github.com/joyent/node/issues/4337 | ||
if (isWin32 && error.code === 'EPERM') { | ||
fs.exists(item, function(exists) { | ||
if (exists) _handleError(error); | ||
}); | ||
} else { | ||
_handleError(error); | ||
} | ||
}); | ||
this.watchers.push(watcher); | ||
var errHandler = this._handleError.bind(this); | ||
watcher = createFsWatchInstance(item, options, callback, errHandler); | ||
} | ||
if (watcher) this.watchers.push(watcher); | ||
}; | ||
@@ -611,7 +699,3 @@ | ||
this.watchers.forEach(function(watcher) { | ||
if (watcher.stop) { | ||
watcher.stop(); | ||
} else { | ||
watcher.close(); | ||
} | ||
watcher.close(); | ||
}); | ||
@@ -618,0 +702,0 @@ Object.keys(watched).forEach(function(directory) { |
{ | ||
"name": "chokidar", | ||
"description": "A neat wrapper around node.js fs.watch / fs.watchFile / fsevents.", | ||
"version": "0.10.6", | ||
"version": "0.10.7", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "fs", |
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
30398
644