Skip to content

Commit

Permalink
close watchers correctly when closing watching
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Mar 15, 2017
1 parent f84412c commit 263c5c9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
11 changes: 10 additions & 1 deletion lib/Compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function Watching(compiler, watchOptions, handler) {
this.error = null;
this.stats = null;
this.handler = handler;
this.closed = false;
if(typeof watchOptions === "number") {
this.watchOptions = {
aggregateTimeout: watchOptions
Expand Down Expand Up @@ -92,12 +93,14 @@ Watching.prototype._done = function(err, compilation) {
else
this.compiler.applyPlugins("failed", this.error);
this.handler(this.error, this.stats);
if(!this.error)
if(!this.error && !this.closed)
this.watch(compilation.fileDependencies, compilation.contextDependencies, compilation.missingDependencies);
};

Watching.prototype.watch = function(files, dirs, missing) {
this.pausedWatcher = null;
this.watcher = this.compiler.watchFileSystem.watch(files, dirs, missing, this.startTime, this.watchOptions, function(err, filesModified, contextModified, missingModified, fileTimestamps, contextTimestamps) {
this.pausedWatcher = this.watcher;
this.watcher = null;
if(err) return this.handler(err);

Expand All @@ -111,6 +114,7 @@ Watching.prototype.watch = function(files, dirs, missing) {

Watching.prototype.invalidate = function() {
if(this.watcher) {
this.pausedWatcher = this.watcher;
this.watcher.pause();
this.watcher = null;
}
Expand All @@ -125,10 +129,15 @@ Watching.prototype.invalidate = function() {
Watching.prototype.close = function(callback) {
if(callback === undefined) callback = function() {};

this.closed = true;
if(this.watcher) {
this.watcher.close();
this.watcher = null;
}
if(this.pausedWatcher) {
this.pausedWatcher.close();
this.pausedWatcher = null;
}
if(this.running) {
this.invalid = true;
this._done = () => {
Expand Down
9 changes: 7 additions & 2 deletions lib/node/NodeWatchFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,15 @@ class NodeWatchFileSystem {
}
return {
close: () => {
this.watcher.close();
if(this.watcher) {
this.watcher.close();
this.watcher = null;
}
},
pause: () => {
this.watcher.pause();
if(this.watcher) {
this.watcher.pause();
}
}
};
}
Expand Down
6 changes: 3 additions & 3 deletions test/WatchDetection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ describe("WatchDetection", () => {
function step4() {
onChange = null;

watcher.close();

done();
watcher.close(() => {
setTimeout(done, 1000);
});
}

function handleError(err) {
Expand Down

0 comments on commit 263c5c9

Please sign in to comment.