From f9f6e5b834016809ddc3a81843a9a8f99ef0218d Mon Sep 17 00:00:00 2001 From: Vince Date: Sun, 23 Sep 2018 19:51:21 -0400 Subject: [PATCH 1/4] Fix #445 test cases for fs.unwatchFile --- tests/spec/fs.unwatchFile.spec.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tests/spec/fs.unwatchFile.spec.js 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; + }); + }); +}); From 7c16490e255ee7f9e270d4356583cd00948e9a93 Mon Sep 17 00:00:00 2001 From: Vince Date: Wed, 10 Oct 2018 21:44:05 -0400 Subject: [PATCH 2/4] Included unwatchFile in test index.js --- tests/index.js | 1 + 1 file changed, 1 insertion(+) 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'); From db315eae134c1e0d9d9b47531615b13c4abb9026 Mon Sep 17 00:00:00 2001 From: Vince Date: Tue, 16 Oct 2018 20:50:10 -0400 Subject: [PATCH 3/4] Added unwatchFile method for issue-551 --- src/filesystem/interface.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/filesystem/interface.js b/src/filesystem/interface.js index 0c6b3317..c66dd718 100644 --- a/src/filesystem/interface.js +++ b/src/filesystem/interface.js @@ -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; @@ -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); + } + /*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) { @@ -347,7 +369,7 @@ function FileSystem(options, callback) { callback(error); } }; - + FileSystem.prototype.promises[methodName] = promisify(FileSystem.prototype[methodName].bind(fs)); }); From d68361a99b672f400263942ae4852112455c9d16 Mon Sep 17 00:00:00 2001 From: Vince Date: Mon, 22 Oct 2018 15:36:02 -0400 Subject: [PATCH 4/4] Fixed unwatchfile method for issue551 --- src/filesystem/interface.js | 17 ++++++----------- src/fs-watcher.js | 12 +++++++++--- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/filesystem/interface.js b/src/filesystem/interface.js index c66dd718..789b0b8e 100644 --- a/src/filesystem/interface.js +++ b/src/filesystem/interface.js @@ -17,7 +17,6 @@ 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; @@ -166,19 +165,15 @@ function FileSystem(options, callback) { } listener = listener || nop; - if(listener == nop){ - this.removeAllListeners(); + var unwatcher = new FSWatcher(); + unwatcher.start(filename, false, options.recursive); + if(listener != nop){ + unwatcher.closeOneListener(filename); } else{ - this.off('change', listener); + unwatcher.close(); } - /*var unwatch = new unwatchFile(); - if(listener == nop){ - unwatch.removeListeners(); - } - else{ - unwatch.removeSingleListener(listener); - }*/ + return unwatcher; }; // Deal with various approaches to node ID creation 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;