From 3f746b014c06e33396aa7a78ee18fabc094e70f7 Mon Sep 17 00:00:00 2001 From: Andrew Koung Date: Fri, 29 Mar 2019 00:53:45 -0400 Subject: [PATCH] rework logic and added some test scripts --- src/filesystem/interface.js | 57 +++++++++++++++++++-------------- tests/spec/fs.watchFile.spec.js | 25 ++++++++------- 2 files changed, 47 insertions(+), 35 deletions(-) diff --git a/src/filesystem/interface.js b/src/filesystem/interface.js index de5b7f1..1a99963 100644 --- a/src/filesystem/interface.js +++ b/src/filesystem/interface.js @@ -207,47 +207,56 @@ function FileSystem(options, callback) { }; //Object that uses filenames as keys - const statWatchers = new Map(); + const statWatchers = new Map(); - this.watchFile = function(filename, options, listener) { - let prevStat, currStat; + this.watchFile = function (filename, options, listener) { + let prevStat, currStat; if (Path.isNull(filename)) { throw new Error('Path must be a string without null bytes.'); } // Checks to see if there were options passed in and if not, the callback function will be set here if (typeof options === 'function') { - listener = options; + listener = options; options = {}; } // default 5007ms interval, persistent is not used this project const interval = options.interval || 5007; listener = listener || nop; - - // Stores initial prev value to compare - fs.stat(filename, function(err, stats) { - prevStat = stats; - - // Stores interval return values - statWatchers.set(filename, value); - - var value = setInterval(function() { - fs.stat(filename, function(err, stats) { - if(err) { - console.log(err); - } - // Store the current stat + + let intervalValue = statWatchers.get(filename); + + // Checks to see if there's a pre-existing watcher on the file + if (intervalValue === undefined) { + // Stores initial prev value to compare + fs.stat(filename, function (err, stats) { + var value = setInterval(function () { + prevStat = currStat; + + //Conditional check for first run to set initial state for prevStat + if(prevStat === undefined) { + prevStat = stats; + } + currStat = stats; - if(Object.toJSON(prevStat) !== Object.toJSON(currStat)) { + + if (err) { + clearInterval(value); + console.warn('[Filer Error] fs.watchFile encountered an error: ' + err); + } + if (JSON.stringify(prevStat) !== JSON.stringify(currStat)) { listener(prevStat, currStat); } // Set a new prevStat based on previous prevStat = currStat; - }); - }, - interval - ); - }); + }, + interval + ); + + // Stores interval return values + statWatchers.set(filename, value); + }); + } }; // Deal with various approaches to node ID creation diff --git a/tests/spec/fs.watchFile.spec.js b/tests/spec/fs.watchFile.spec.js index 0620b7a..d54c55f 100644 --- a/tests/spec/fs.watchFile.spec.js +++ b/tests/spec/fs.watchFile.spec.js @@ -8,24 +8,27 @@ describe('fs.watchFile', function() { afterEach(util.cleanup); it('should be a function', function() { - var fs = util.fs(); + const fs = util.fs(); expect(typeof fs.watchFile).to.equal('function'); }); - /* - it('should get a change event when writing a file', function(done) { + it('should throw an error if a file path is not defined', function() { const fs = util.fs(); + + const fn = () => fs.watchFile(undefined); + expect(fn).to.throw(); + }); - fs.watchFile('/myfile.txt', function(event, filename) { - expect(event).to.equal('change'); - expect(filename).to.equal('/myfile.txt'); - watcher.close(); - done(); - }); + it('prev and curr should be populated', function() { + const fs = util.fs(); - fs.writeFile('/myfile.txt', 'data', function(error) { + fs.writeFile('/myfile', 'data', function(error) { if(error) throw error; + }); + + fs.watchFile('/myfile', function(prev, curr) { + expect(prev).to.exist; + expect(curr).to.exist; }); }); - */ }); \ No newline at end of file