Skip to content

Commit

Permalink
Should not watch after .close() is called.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanchuan committed Dec 6, 2020
1 parent 2e42be3 commit 4cac915
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,11 @@ Watcher.prototype.close = function(fullPath) {
// Do not close the Watcher unless all child watchers are closed.
// https://github.com/yuanchuan/node-watch/issues/75
if (is.emptyObject(self.watchers)) {
this._isClosed = true;
process.nextTick(emitClose, this);
// should emit once
if (!this._isClosed) {
this._isClosed = true;
process.nextTick(emitClose, this);
}
}
}

Expand Down Expand Up @@ -385,6 +388,12 @@ Watcher.prototype.watchDirectory = function(dir, options, fn, counter = nullCoun
delete opts.recursive;
}

// check if it's closed before calling watch.
if (self._isClosed) {
done();
return self.close();
}

var watcher = fs.watch(dir, opts);

self.add(watcher, {
Expand Down
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,17 @@ describe('watcher object', function() {
});
});

it('should not watch after .close() is called', function(done) {
var dir = tree.getPath('home');
watcher = watch(dir, { delay: 0, recursive: true });
watcher.close();

watcher.getWatchedPaths(function(dirs) {
assert(dirs.length === 0);
done();
});
});

it('Do not emit after close', function(done) {
var dir = tree.getPath('home/a');
var file = 'home/a/file1';
Expand All @@ -720,6 +731,7 @@ describe('watcher object', function() {
}, 100);
});
});

});

describe('getWatchedPaths()', function() {
Expand Down

0 comments on commit 4cac915

Please sign in to comment.