Issue #654 working on adding feature fs.watchFile()

This commit is contained in:
Andrew Koung 2019-03-19 21:49:10 -04:00
parent 4aae53839a
commit 58a3a30a6b
4 changed files with 89 additions and 1 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "filer",
"version": "1.1.0",
"version": "1.1.2",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -206,6 +206,43 @@ function FileSystem(options, callback) {
return watcher;
};
//Object that uses filenames as keys
const statWatchers = new Map();
this.watchFile = function(filename, options, listener) {
const 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;
options = {};
}
//default 5007ms interval, persistent is not used this project
const interval = options.interval || 5007;
listener = listener || nop;
//stores interval return values
statWatchers.set(filename, value);
var value = setInterval(function() {
fs.stat(filename, function(err, stats) {
if(err) {
//console.log(err);
}
//record file curr
//compare curr-prev
//if theres a difference file change
});
},
interval
);
return watcher;
};
// Deal with various approaches to node ID creation
function wrappedGuidFn(context) {
return function (callback) {

View File

@ -41,6 +41,7 @@ require('./spec/trailing-slashes.spec');
require('./spec/times.spec');
require('./spec/time-flags.spec');
require('./spec/fs.watch.spec');
require('./spec/fs.watchFile.spec');
require('./spec/fs.unwatchFile.spec');
require('./spec/errors.spec');
require('./spec/fs.shell.spec');

View File

@ -0,0 +1,50 @@
'use strict';
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
describe('fs.watchFile', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
var fs = util.fs();
expect(typeof fs.watchFile).to.equal('function');
});
it('should get a change event when writing a file', function(done) {
const fs = util.fs();
const watcher = fs.watchFile('/myfile.txt', function(event, filename) {
expect(event).to.equal('change');
expect(filename).to.equal('/myfile.txt');
watcher.close();
done();
});
fs.writeFile('/myfile.txt', 'data', function(error) {
if(error) throw error;
});
});
/*
it('should get a change event when renaming a file', function(done) {
const fs = util.fs();
fs.writeFile('/myfile.txt', 'data', function(error) {
if(error) throw error;
const watcher = fs.watchFile('/myfile.txt', function(event, filename) {
expect(event).to.equal('change');
expect(filename).to.equal('/myfile.txt');
watcher.close();
done();
});
fs.rename('/myfile.txt', '/mynewfile.txt', function(error) {
if(error) throw error;
});
});
});
*/
});