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 3 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.

42 changes: 42 additions & 0 deletions src/filesystem/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,48 @@ 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) {
const prevStat, currStat;
andrewkoung marked this conversation as resolved.
Show resolved Hide resolved

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
andrewkoung marked this conversation as resolved.
Show resolved Hide resolved
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});
andrewkoung marked this conversation as resolved.
Show resolved Hide resolved

//stores interval return values
statWatchers.set(filename, value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: what do we do if there is already a watcher in here for this filename? Probably we need an array of intervals for each filename vs. assuming it will only be one? That will complicate removing it. Need to ponder this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If there's a watcher already associated with the filename, maybe it shouldn't it be ignored. There would be a setInterval() that's associated with that filename won't there be?


var value = setInterval(function() {
fs.stat(filename, function(err, stats) {
if(err) {
console.log(err);
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't good enough. We'll have to deal with it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any errors handling in nodejs seems to be happening in the highlighted area
image
I tried to analyze the start method, but it seems to be too complex for me with the terminology being used here, but I believe the highlighted portion below is what's important.
image

Copy link
Contributor

Choose a reason for hiding this comment

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

Node is able to throw the error here because they are doing the initial stat synchronously, so the ultimate recipient of the throw would be the caller of fs.watchFile. We can only do asynchronous calls in Filer and the Browser (i.e., we can't block on access to IndexedDB), so throwing doesn't make as much sense for us.

Another problem I see is that node is returning the stat object it gets back from that synchronous call. We also can't do this.

I'm not 100% sure what the right approach is here. Perhaps we need to kill the interval and have the watcher just become a no-op if there is an error? Maybe logging an error to the console in addition does make sense. Perhaps you could do:

console.warn([Filer Error] fs.watchFile encountered an error with ${path}: ${err.message})

}
//Store the current stat
currStat = stats;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is happening too early. Do you not need:

  1. Move currStat to prevStat
  2. Move stats to currStat
  3. Diff the inner values of the objects (not reference equality)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought the order makes sense in this case because initial prevStat and currStat are undefined, so I set an initial stat value to prevStat. In which I then set the stats coming from fs.stat to currStat since it's undefined. In which does a compare and then sets current to prev.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could be. Let's circle back to this when you get deeper into it.

if(prevStat != currStat) {
andrewkoung marked this conversation as resolved.
Show resolved Hide resolved
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
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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

watchFile doesn't return a value, so you can drop const watcher =

Copy link
Contributor

Choose a reason for hiding this comment

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

Question: what does node.js do when you watchFile a file that doesn't exist yet? Your code above will break when you do the initial stat, and it gets an ENOENT error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Node attempts to receive the watcher using this line here stat = statWatchers.get(filename);. If there is no value associated with the key, it'll return undefined. It does a conditional check to see if the stat variable is undefined and create a new watcher and then assign a value to the key. Whether the stat variable was undefined or defined, it lastly attaches a listener stat.addListener('change', listener);.

Copy link
Contributor

Choose a reason for hiding this comment

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

So we have a choice to make in this case: we either duplicate node's use of stat on an internval, or we alter the way watchFile works internally to just call into watch, which uses event based monitoring vs. polling. The upside to the latter is that it already works.

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;
});
});
});
*/
});