From a25f000505528fca55c0662ab39677b927461122 Mon Sep 17 00:00:00 2001 From: Robert Gay Date: Sat, 17 Dec 2016 11:15:57 -0800 Subject: [PATCH] Fixes webpack/webpack#762 FS accuracy at startup is 10s, but it's fixed when files change. This commit allows fs accuracy to be fixed at startup by examining file mtimes. --- lib/DirectoryWatcher.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/DirectoryWatcher.js b/lib/DirectoryWatcher.js index 035b251..f135bf9 100644 --- a/lib/DirectoryWatcher.js +++ b/lib/DirectoryWatcher.js @@ -82,6 +82,8 @@ DirectoryWatcher.prototype.setFileTime = function setFileTime(filePath, mtime, i this.files[filePath] = [initial ? Math.min(now, mtime) : now, mtime]; + ensureFsAccuracy(mtime); + // we add the fs accurency to reach the maximum possible mtime if(mtime) mtime = mtime + FS_ACCURENCY; @@ -231,16 +233,7 @@ DirectoryWatcher.prototype.onChange = function onChange(filePath, stat) { if(filePath.indexOf(this.path) !== 0) return; if(/[\\\/]/.test(filePath.substr(this.path.length + 1))) return; var mtime = +stat.mtime; - if(FS_ACCURENCY > 1 && mtime % 1 !== 0) - FS_ACCURENCY = 1; - else if(FS_ACCURENCY > 10 && mtime % 10 !== 0) - FS_ACCURENCY = 10; - else if(FS_ACCURENCY > 100 && mtime % 100 !== 0) - FS_ACCURENCY = 100; - else if(FS_ACCURENCY > 1000 && mtime % 1000 !== 0) - FS_ACCURENCY = 1000; - else if(FS_ACCURENCY > 2000 && mtime % 2000 !== 0) - FS_ACCURENCY = 2000; + ensureFsAccuracy(mtime); this.setFileTime(filePath, mtime, false, "change"); }; @@ -333,3 +326,16 @@ DirectoryWatcher.prototype.close = function() { } this.emit("closed"); }; + +function ensureFsAccuracy(mtime) { + if(FS_ACCURENCY > 1 && mtime % 1 !== 0) + FS_ACCURENCY = 1; + else if(FS_ACCURENCY > 10 && mtime % 10 !== 0) + FS_ACCURENCY = 10; + else if(FS_ACCURENCY > 100 && mtime % 100 !== 0) + FS_ACCURENCY = 100; + else if(FS_ACCURENCY > 1000 && mtime % 1000 !== 0) + FS_ACCURENCY = 1000; + else if(FS_ACCURENCY > 2000 && mtime % 2000 !== 0) + FS_ACCURENCY = 2000; +}