Skip to content

Commit

Permalink
fs: Removing inStatWatchers function
Browse files Browse the repository at this point in the history
We can get rid of `inStatWatchers` function by creating `statWatchers`
 with `null` as prototype and use `in` operator to check if `filename`
 is in it or not.
  • Loading branch information
thefourtheye committed Jun 2, 2015
1 parent 0016239 commit 2aabff9
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1297,12 +1297,7 @@ StatWatcher.prototype.stop = function() {
};


var statWatchers = {};
function inStatWatchers(filename) {
return Object.prototype.hasOwnProperty.call(statWatchers, filename) &&
statWatchers[filename];
}

const statWatchers = Object.create(null);

fs.watchFile = function(filename) {
nullCheck(filename);
Expand All @@ -1329,7 +1324,7 @@ fs.watchFile = function(filename) {
throw new Error('watchFile requires a listener function');
}

if (inStatWatchers(filename)) {
if (filename in statWatchers) {
stat = statWatchers[filename];
} else {
stat = statWatchers[filename] = new StatWatcher();
Expand All @@ -1342,7 +1337,7 @@ fs.watchFile = function(filename) {
fs.unwatchFile = function(filename, listener) {
nullCheck(filename);
filename = pathModule.resolve(filename);
if (!inStatWatchers(filename)) return;
if (!(filename in statWatchers)) return;

var stat = statWatchers[filename];

Expand All @@ -1354,7 +1349,7 @@ fs.unwatchFile = function(filename, listener) {

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

Expand Down

0 comments on commit 2aabff9

Please sign in to comment.