diff --git a/src/filesystem/interface.js b/src/filesystem/interface.js index 0c6b3317..789b0b8e 100644 --- a/src/filesystem/interface.js +++ b/src/filesystem/interface.js @@ -159,6 +159,23 @@ 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; + + var unwatcher = new FSWatcher(); + unwatcher.start(filename, false, options.recursive); + if(listener != nop){ + unwatcher.closeOneListener(filename); + } + else{ + unwatcher.close(); + } + return unwatcher; + }; + // Deal with various approaches to node ID creation function wrappedGuidFn(context) { return function(callback) { @@ -347,7 +364,7 @@ function FileSystem(options, callback) { callback(error); } }; - + FileSystem.prototype.promises[methodName] = promisify(FileSystem.prototype[methodName].bind(fs)); }); diff --git a/src/fs-watcher.js b/src/fs-watcher.js index e236bcbc..e7714e46 100644 --- a/src/fs-watcher.js +++ b/src/fs-watcher.js @@ -1,7 +1,7 @@ var EventEmitter = require('../lib/eventemitter.js'); var Path = require('./path.js'); var Intercom = require('../lib/intercom.js'); - +var _watchers = {}; /** * FSWatcher based on node.js' FSWatcher * see https://github.com/joyent/node/blob/master/lib/fs.js @@ -45,11 +45,15 @@ function FSWatcher() { if(recursive) { recursivePathPrefix = filename === '/' ? '/' : filename + '/'; } - + _watchers[filename] = self; var intercom = Intercom.getInstance(); intercom.on('change', onchange); }; + self.closeOneListener = function(filename){ + _watchers[filename].close(); + }; + self.close = function() { var intercom = Intercom.getInstance(); intercom.off('change', onchange); @@ -58,5 +62,7 @@ function FSWatcher() { } FSWatcher.prototype = new EventEmitter(); FSWatcher.prototype.constructor = FSWatcher; - +FSWatcher.getWatcherForFilename = function(filename) { + return _watchers[filename]; +}; module.exports = FSWatcher; diff --git a/tests/index.js b/tests/index.js index e49c342f..2ad5f9e6 100644 --- a/tests/index.js +++ b/tests/index.js @@ -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'); diff --git a/tests/spec/fs.unwatchFile.spec.js b/tests/spec/fs.unwatchFile.spec.js new file mode 100644 index 00000000..6972ff65 --- /dev/null +++ b/tests/spec/fs.unwatchFile.spec.js @@ -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; + }); + }); +});