rework logic and added some test scripts

This commit is contained in:
Andrew Koung 2019-03-29 00:53:45 -04:00
parent 3fd3fb0c6d
commit 3f746b014c
2 changed files with 47 additions and 35 deletions

View File

@ -207,47 +207,56 @@ function FileSystem(options, callback) {
}; };
//Object that uses filenames as keys //Object that uses filenames as keys
const statWatchers = new Map(); const statWatchers = new Map();
this.watchFile = function(filename, options, listener) { this.watchFile = function (filename, options, listener) {
let prevStat, currStat; let prevStat, currStat;
if (Path.isNull(filename)) { if (Path.isNull(filename)) {
throw new Error('Path must be a string without null bytes.'); 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 // Checks to see if there were options passed in and if not, the callback function will be set here
if (typeof options === 'function') { if (typeof options === 'function') {
listener = options; listener = options;
options = {}; options = {};
} }
// default 5007ms interval, persistent is not used this project // default 5007ms interval, persistent is not used this project
const interval = options.interval || 5007; const interval = options.interval || 5007;
listener = listener || nop; listener = listener || nop;
// Stores initial prev value to compare let intervalValue = statWatchers.get(filename);
fs.stat(filename, function(err, stats) {
prevStat = stats; // Checks to see if there's a pre-existing watcher on the file
if (intervalValue === undefined) {
// Stores interval return values // Stores initial prev value to compare
statWatchers.set(filename, value); fs.stat(filename, function (err, stats) {
var value = setInterval(function () {
var value = setInterval(function() { prevStat = currStat;
fs.stat(filename, function(err, stats) {
if(err) { //Conditional check for first run to set initial state for prevStat
console.log(err); if(prevStat === undefined) {
} prevStat = stats;
// Store the current stat }
currStat = 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); listener(prevStat, currStat);
} }
// Set a new prevStat based on previous // Set a new prevStat based on previous
prevStat = currStat; prevStat = currStat;
}); },
}, interval
interval );
);
}); // Stores interval return values
statWatchers.set(filename, value);
});
}
}; };
// Deal with various approaches to node ID creation // Deal with various approaches to node ID creation

View File

@ -8,24 +8,27 @@ describe('fs.watchFile', function() {
afterEach(util.cleanup); afterEach(util.cleanup);
it('should be a function', function() { it('should be a function', function() {
var fs = util.fs(); const fs = util.fs();
expect(typeof fs.watchFile).to.equal('function'); expect(typeof fs.watchFile).to.equal('function');
}); });
/* it('should throw an error if a file path is not defined', function() {
it('should get a change event when writing a file', function(done) {
const fs = util.fs(); const fs = util.fs();
const fn = () => fs.watchFile(undefined);
expect(fn).to.throw();
});
fs.watchFile('/myfile.txt', function(event, filename) { it('prev and curr should be populated', function() {
expect(event).to.equal('change'); const fs = util.fs();
expect(filename).to.equal('/myfile.txt');
watcher.close();
done();
});
fs.writeFile('/myfile.txt', 'data', function(error) { fs.writeFile('/myfile', 'data', function(error) {
if(error) throw error; if(error) throw error;
});
fs.watchFile('/myfile', function(prev, curr) {
expect(prev).to.exist;
expect(curr).to.exist;
}); });
}); });
*/
}); });