Skip to content

Commit

Permalink
Issue filerjs#654 working on adding feature fs.watchFile()
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkoung committed Mar 20, 2019
1 parent 4aae538 commit 58a3a30
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions src/filesystem/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
50 changes: 50 additions & 0 deletions tests/spec/fs.watchFile.spec.js
Original file line number Diff line number Diff line change
@@ -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;
});
});
});
*/
});

0 comments on commit 58a3a30

Please sign in to comment.