rework logic and added some test scripts
This commit is contained in:
parent
3fd3fb0c6d
commit
3f746b014c
|
@ -224,30 +224,39 @@ function FileSystem(options, callback) {
|
|||
const interval = options.interval || 5007;
|
||||
listener = listener || nop;
|
||||
|
||||
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) {
|
||||
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);
|
||||
prevStat = currStat;
|
||||
|
||||
//Conditional check for first run to set initial state for prevStat
|
||||
if(prevStat === undefined) {
|
||||
prevStat = stats;
|
||||
}
|
||||
// Store the current stat
|
||||
|
||||
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
|
||||
);
|
||||
|
||||
// Stores interval return values
|
||||
statWatchers.set(filename, value);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// Deal with various approaches to node ID creation
|
||||
|
|
|
@ -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();
|
||||
|
||||
fs.watchFile('/myfile.txt', function(event, filename) {
|
||||
expect(event).to.equal('change');
|
||||
expect(filename).to.equal('/myfile.txt');
|
||||
watcher.close();
|
||||
done();
|
||||
const fn = () => fs.watchFile(undefined);
|
||||
expect(fn).to.throw();
|
||||
});
|
||||
|
||||
fs.writeFile('/myfile.txt', 'data', function(error) {
|
||||
it('prev and curr should be populated', function() {
|
||||
const fs = util.fs();
|
||||
|
||||
fs.writeFile('/myfile', 'data', function(error) {
|
||||
if(error) throw error;
|
||||
});
|
||||
|
||||
fs.watchFile('/myfile', function(prev, curr) {
|
||||
expect(prev).to.exist;
|
||||
expect(curr).to.exist;
|
||||
});
|
||||
});
|
||||
*/
|
||||
});
|
Loading…
Reference in New Issue