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 = {};
|
||||
function inStatWatchers(filename) {
|
||||
return Object.prototype.hasOwnProperty.call(statWatchers, filename) &&
|
||||
statWatchers[filename];
|
||||
}
|
||||
|
||||
const statWatchers = new Map();
|
||||
|
||||
fs.watchFile = function(filename) {
|
||||
nullCheck(filename);
|
||||
@ -1333,12 +1328,14 @@ fs.watchFile = function(filename) {
|
||||
throw new Error('watchFile requires a listener function');
|
||||
}
|
||||
|
||||
if (inStatWatchers(filename)) {
|
||||
stat = statWatchers[filename];
|
||||
} else {
|
||||
stat = statWatchers[filename] = new StatWatcher();
|
||||
stat = statWatchers.get(filename);
|
||||
|
||||
if (stat === undefined) {
|
||||
stat = new StatWatcher();
|
||||
stat.start(filename, options.persistent, options.interval);
|
||||
statWatchers.set(filename, stat);
|
||||
}
|
||||
|
||||
stat.addListener('change', listener);
|
||||
return stat;
|
||||
};
|
||||
@ -1346,9 +1343,9 @@ fs.watchFile = function(filename) {
|
||||
fs.unwatchFile = function(filename, listener) {
|
||||
nullCheck(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') {
|
||||
stat.removeListener('change', listener);
|
||||
@ -1358,7 +1355,7 @@ fs.unwatchFile = function(filename, listener) {
|
||||
|
||||
if (EventEmitter.listenerCount(stat, 'change') === 0) {
|
||||
stat.stop();
|
||||
statWatchers[filename] = undefined;
|
||||
statWatchers.delete(filename);
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user