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 all 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
19 changes: 18 additions & 1 deletion src/filesystem/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -347,7 +364,7 @@ function FileSystem(options, callback) {
callback(error);
}
};

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

Expand Down
12 changes: 9 additions & 3 deletions src/fs-watcher.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -58,5 +62,7 @@ function FSWatcher() {
}
FSWatcher.prototype = new EventEmitter();
FSWatcher.prototype.constructor = FSWatcher;

FSWatcher.getWatcherForFilename = function(filename) {
return _watchers[filename];
};
module.exports = FSWatcher;
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;
});
});
});