Skip to content

Commit 8841132

Browse files
thefourtheyetrevnorris
authored andcommitted
fs: remove inStatWatchers and use Map for lookup
Remove `inStatWatchers` function and make `statWatchers` a `Map`. PR-URL: nodejs#1870 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
1 parent 67a11b9 commit 8841132

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

lib/fs.js

+10-13
Original file line numberDiff line numberDiff line change
@@ -1301,12 +1301,7 @@ StatWatcher.prototype.stop = function() {
13011301
};
13021302

13031303

1304-
var statWatchers = {};
1305-
function inStatWatchers(filename) {
1306-
return Object.prototype.hasOwnProperty.call(statWatchers, filename) &&
1307-
statWatchers[filename];
1308-
}
1309-
1304+
const statWatchers = new Map();
13101305

13111306
fs.watchFile = function(filename) {
13121307
nullCheck(filename);
@@ -1333,22 +1328,24 @@ fs.watchFile = function(filename) {
13331328
throw new Error('watchFile requires a listener function');
13341329
}
13351330

1336-
if (inStatWatchers(filename)) {
1337-
stat = statWatchers[filename];
1338-
} else {
1339-
stat = statWatchers[filename] = new StatWatcher();
1331+
stat = statWatchers.get(filename);
1332+
1333+
if (stat === undefined) {
1334+
stat = new StatWatcher();
13401335
stat.start(filename, options.persistent, options.interval);
1336+
statWatchers.set(filename, stat);
13411337
}
1338+
13421339
stat.addListener('change', listener);
13431340
return stat;
13441341
};
13451342

13461343
fs.unwatchFile = function(filename, listener) {
13471344
nullCheck(filename);
13481345
filename = pathModule.resolve(filename);
1349-
if (!inStatWatchers(filename)) return;
1346+
var stat = statWatchers.get(filename);
13501347

1351-
var stat = statWatchers[filename];
1348+
if (stat === undefined) return;
13521349

13531350
if (typeof listener === 'function') {
13541351
stat.removeListener('change', listener);
@@ -1358,7 +1355,7 @@ fs.unwatchFile = function(filename, listener) {
13581355

13591356
if (EventEmitter.listenerCount(stat, 'change') === 0) {
13601357
stat.stop();
1361-
statWatchers[filename] = undefined;
1358+
statWatchers.delete(filename);
13621359
}
13631360
};
13641361

0 commit comments

Comments
 (0)