From 8841132f30e2e35c2dcc6c724f1507aa3e1c1175 Mon Sep 17 00:00:00 2001 From: Sakthipriyan Vairamani Date: Tue, 2 Jun 2015 22:32:15 +0530 Subject: [PATCH] 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 Reviewed-By: Trevor Norris --- lib/fs.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index ecef4f224e7d3c..8d40a1268185e1 100644 --- a/lib/fs.js +++ b/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); } };