Skip to content

Commit

Permalink
fs: remove inStatWatchers and use Map for lookup
Browse files Browse the repository at this point in the history
Remove `inStatWatchers` function and make `statWatchers` a `Map`.

PR-URL: nodejs#1870
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Trevor Norris <[email protected]>
  • Loading branch information
thefourtheye authored and trevnorris committed Jun 10, 2015
1 parent 67a11b9 commit 8841132
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -1333,22 +1328,24 @@ 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;
};

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);
Expand All @@ -1358,7 +1355,7 @@ fs.unwatchFile = function(filename, listener) {

if (EventEmitter.listenerCount(stat, 'change') === 0) {
stat.stop();
statWatchers[filename] = undefined;
statWatchers.delete(filename);
}
};

Expand Down

0 comments on commit 8841132

Please sign in to comment.