made syntax changes, updated readme, and added more tests

This commit is contained in:
Andrew Koung 2019-03-29 16:30:55 -04:00
parent 3f746b014c
commit babe89c069
3 changed files with 65 additions and 10 deletions

View File

@ -388,6 +388,7 @@ const fs = new Filer.FileSystem(options, callback);
* [fs.removexattr(path, name, callback)](#removexattr) * [fs.removexattr(path, name, callback)](#removexattr)
* [fs.fremovexattr(fd, name, callback)](#fremovexattr) * [fs.fremovexattr(fd, name, callback)](#fremovexattr)
* [fs.watch(filename, [options], [listener])](#watch) * [fs.watch(filename, [options], [listener])](#watch)
* [fs.watchFile(filename, [options], [listener])](#watchFile)
#### fs.rename(oldPath, newPath, callback)<a name="rename"></a> #### fs.rename(oldPath, newPath, callback)<a name="rename"></a>
@ -1323,6 +1324,24 @@ var watcher = fs.watch('/data', { recursive: true }, function(event, filename) {
fs.writeFile('/data/subdir/file', 'data'); fs.writeFile('/data/subdir/file', 'data');
``` ```
### fs.watchFile(filename, [options], [listener])<a name="watch"></a>
Watch for changes on a file at `filename`. The callback `listener` will be called each time the file is accessed.
The `options` argument only supports the change in interval between checks measured in milliseconds and does not support perstistence like node.
The `listener` receives two arguments that are the current stat object and previous stat object that are instances of `fs.Stat`. Reference to `fs.Stat` can be found
here: [`fs.Stat`](https://nodejs.org/api/fs.html#fs_class_fs_stats)
Example:
```javascript
fs.watchFile('/myfile.txt', (curr, prev) => {
console.log(`the current mtime is: ${curr.mtime}`);
console.log(`the previous mtime was: ${prev.mtime}`);
});
```
### FileSystemShell<a name="FileSystemShell"></a> ### FileSystemShell<a name="FileSystemShell"></a>
Many common file system shell operations are available by using a `FileSystemShell` object. Many common file system shell operations are available by using a `FileSystemShell` object.

View File

@ -226,15 +226,16 @@ function FileSystem(options, callback) {
let intervalValue = statWatchers.get(filename); let intervalValue = statWatchers.get(filename);
// Checks to see if there's a pre-existing watcher on the file if(intervalValue) {
if (intervalValue === undefined) { return;
// Stores initial prev value to compare }
else {
fs.stat(filename, function (err, stats) { fs.stat(filename, function (err, stats) {
var value = setInterval(function () { var value = setInterval(function () {
prevStat = currStat; prevStat = currStat;
//Conditional check for first run to set initial state for prevStat //Conditional check for first run to set initial state for prevStat
if(prevStat === undefined) { if(!prevStat) {
prevStat = stats; prevStat = stats;
} }
@ -242,7 +243,7 @@ function FileSystem(options, callback) {
if (err) { if (err) {
clearInterval(value); clearInterval(value);
console.warn('[Filer Error] fs.watchFile encountered an error: ' + err); console.warn('[Filer Error] fs.watchFile encountered an error' + err.message);
} }
if (JSON.stringify(prevStat) !== JSON.stringify(currStat)) { if (JSON.stringify(prevStat) !== JSON.stringify(currStat)) {
listener(prevStat, currStat); listener(prevStat, currStat);

View File

@ -19,7 +19,7 @@ describe('fs.watchFile', function() {
expect(fn).to.throw(); expect(fn).to.throw();
}); });
it('prev and curr should be populated', function() { it('should throw an error if a file is deleted', function() {
const fs = util.fs(); const fs = util.fs();
fs.writeFile('/myfile', 'data', function(error) { fs.writeFile('/myfile', 'data', function(error) {
@ -30,5 +30,40 @@ describe('fs.watchFile', function() {
expect(prev).to.exist; expect(prev).to.exist;
expect(curr).to.exist; expect(curr).to.exist;
}); });
fs.unlink('/myfile');
const fn = () => fs.watchFile('/myfile');
expect(fn).to.throw();
});
it('prev and curr should be equal if nothing has been changed in the file', function() {
const fs = util.fs();
fs.writeFile('/myfile', 'data', function(error) {
if(error) throw error;
});
fs.watchFile('/myfile', function(prev, curr) {
expect(prev).to.equal(curr);
});
});
it('prev and curr should not be equal if something has been changed in the file', function() {
const fs = util.fs();
fs.writeFile('/myfile', 'data', function(error) {
if(error) throw error;
});
fs.watchFile('/myfile', function(prev, curr) {
expect(prev).to.equal(curr);
fs.writeFile('/myfile', 'data2', function(error) {
if(error) throw error;
});
expect(prev).to.not.equal(curr);
});
}); });
}); });