fs: remove inStatWatchers and use Map for lookup
Remove `inStatWatchers` function and make `statWatchers` a `Map`. PR-URL: https://github.com/nodejs/io.js/pull/1870 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
This commit is contained in:
parent
67a11b9bcc
commit
8841132f30
23
lib/fs.js
23
lib/fs.js
@ -1301,12 +1301,7 @@ StatWatcher.prototype.stop = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
var statWatchers = {};
|
const statWatchers = new Map();
|
||||||
function inStatWatchers(filename) {
|
|
||||||
return Object.prototype.hasOwnProperty.call(statWatchers, filename) &&
|
|
||||||
statWatchers[filename];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fs.watchFile = function(filename) {
|
fs.watchFile = function(filename) {
|
||||||
nullCheck(filename);
|
nullCheck(filename);
|
||||||
@ -1333,12 +1328,14 @@ fs.watchFile = function(filename) {
|
|||||||
throw new Error('watchFile requires a listener function');
|
throw new Error('watchFile requires a listener function');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inStatWatchers(filename)) {
|
stat = statWatchers.get(filename);
|
||||||
stat = statWatchers[filename];
|
|
||||||
} else {
|
if (stat === undefined) {
|
||||||
stat = statWatchers[filename] = new StatWatcher();
|
stat = new StatWatcher();
|
||||||
stat.start(filename, options.persistent, options.interval);
|
stat.start(filename, options.persistent, options.interval);
|
||||||
|
statWatchers.set(filename, stat);
|
||||||
}
|
}
|
||||||
|
|
||||||
stat.addListener('change', listener);
|
stat.addListener('change', listener);
|
||||||
return stat;
|
return stat;
|
||||||
};
|
};
|
||||||
@ -1346,9 +1343,9 @@ fs.watchFile = function(filename) {
|
|||||||
fs.unwatchFile = function(filename, listener) {
|
fs.unwatchFile = function(filename, listener) {
|
||||||
nullCheck(filename);
|
nullCheck(filename);
|
||||||
filename = pathModule.resolve(filename);
|
filename = pathModule.resolve(filename);
|
||||||
if (!inStatWatchers(filename)) return;
|
var stat = statWatchers.get(filename);
|
||||||
|
|
||||||
var stat = statWatchers[filename];
|
if (stat === undefined) return;
|
||||||
|
|
||||||
if (typeof listener === 'function') {
|
if (typeof listener === 'function') {
|
||||||
stat.removeListener('change', listener);
|
stat.removeListener('change', listener);
|
||||||
@ -1358,7 +1355,7 @@ fs.unwatchFile = function(filename, listener) {
|
|||||||
|
|
||||||
if (EventEmitter.listenerCount(stat, 'change') === 0) {
|
if (EventEmitter.listenerCount(stat, 'change') === 0) {
|
||||||
stat.stop();
|
stat.stop();
|
||||||
statWatchers[filename] = undefined;
|
statWatchers.delete(filename);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user