Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

44 changes: 44 additions & 0 deletions src/filesystem/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,50 @@ function FileSystem(options, callback) {
return watcher;
};

//Object that uses filenames as keys
const statWatchers = new Map();
andrewkoung marked this conversation as resolved.
Show resolved Hide resolved

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;
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() {
andrewkoung marked this conversation as resolved.
Show resolved Hide resolved
fs.stat(filename, function(err, stats) {
if(err) {
console.log(err);
}
// Store the current stat
currStat = stats;
if(Object.toJSON(prevStat) !== Object.toJSON(currStat)) {
listener(prevStat, currStat);
}
// Set a new prevStat based on previous
prevStat = currStat;
});
},
interval
);
});
};

// 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
31 changes: 31 additions & 0 deletions tests/spec/fs.watchFile.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'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');
});

/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a bunch more tests here, can you expand this please?

it('should get a change event when writing a file', function(done) {
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();
});

fs.writeFile('/myfile.txt', 'data', function(error) {
if(error) throw error;
});
});
*/
});