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 551-Add unwatchfile method #553

Open
wants to merge 4 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
24 changes: 23 additions & 1 deletion src/filesystem/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var providers = require('../providers/index.js');
var Shell = require('../shell/shell.js');
var Intercom = require('../../lib/intercom.js');
var FSWatcher = require('../fs-watcher.js');
//var unwatcher = require('../unwatcherFile.js');
var Errors = require('../errors.js');
var defaultGuidFn = require('../shared.js').guid;

Expand Down Expand Up @@ -159,6 +160,27 @@ function FileSystem(options, callback) {
return watcher;
};

this.unwatchFile = function(filename, listener) {
if(isNullPath(filename)) {
throw new Error('Path must be a string without null bytes.');
}
listener = listener || nop;

if(listener == nop){
this.removeAllListeners();
}
else{
this.off('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.

For what you're trying to do to work, we're going to have to alter how .watch() works above, namely, we're going to need to cache the listener for the given filename on an Object accessible by both of these functions, and keyed on the filename, such that we can access it here.

Probably on line 159 above, when you have both the filename, the watcher, and the listener, you'll need to add it to an Object that we define somewhere, either in this file on line 143, or maybe we do it in FSWatcher itself.

Probably the latter is best, since we're normalizing the filename there. Over here you need something like this:

// At the top of the file
var _watchers = {};

FSWatcher.getWatcherForFilename = function(filename) {
  return _watchers[filename];
};

Now in the code for start(), you can cache the watcher instance in _watchers, maybe here:

_watchers[filename] = self;

Now you have what you need to call .close() here when you want to remove this watcher.

Do you follow what I'm saying?

}
/*var unwatch = new unwatchFile();
if(listener == nop){
unwatch.removeListeners();
}
else{
unwatch.removeSingleListener(listener);
}*/
};

// Deal with various approaches to node ID creation
function wrappedGuidFn(context) {
return function(callback) {
Expand Down Expand Up @@ -347,7 +369,7 @@ function FileSystem(options, callback) {
callback(error);
}
};

FileSystem.prototype.promises[methodName] = promisify(FileSystem.prototype[methodName].bind(fs));
});

Expand Down
1 change: 1 addition & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require('./spec/trailing-slashes.spec');
require('./spec/times.spec');
require('./spec/time-flags.spec');
require('./spec/fs.watch.spec');
require('./spec/fs.unwatchFile.spec');
require('./spec/errors.spec');
require('./spec/fs.shell.spec');
require('./spec/fs.chmod.spec');
Expand Down
19 changes: 19 additions & 0 deletions tests/spec/fs.unwatchFile.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;

describe('fs.unwatchFile', function() {
beforeEach(util.setup);
afterEach(util.cleanup);

it('should be a function', function() {
var fs = util.fs();
expect(typeof fs.unwatchFile).to.equal('function');
});

it('should not throw an error when using a file not being watched', function() {
var fs = util.fs();
fs.unwatchFile('/myfile', function(error){
expect(error).not.to.exist;
});
});
});