Skip to content

Commit 263c5c9

Browse files
committed
close watchers correctly when closing watching
1 parent f84412c commit 263c5c9

3 files changed

Lines changed: 20 additions & 6 deletions

File tree

lib/Compiler.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function Watching(compiler, watchOptions, handler) {
1616
this.error = null;
1717
this.stats = null;
1818
this.handler = handler;
19+
this.closed = false;
1920
if(typeof watchOptions === "number") {
2021
this.watchOptions = {
2122
aggregateTimeout: watchOptions
@@ -92,12 +93,14 @@ Watching.prototype._done = function(err, compilation) {
9293
else
9394
this.compiler.applyPlugins("failed", this.error);
9495
this.handler(this.error, this.stats);
95-
if(!this.error)
96+
if(!this.error && !this.closed)
9697
this.watch(compilation.fileDependencies, compilation.contextDependencies, compilation.missingDependencies);
9798
};
9899

99100
Watching.prototype.watch = function(files, dirs, missing) {
101+
this.pausedWatcher = null;
100102
this.watcher = this.compiler.watchFileSystem.watch(files, dirs, missing, this.startTime, this.watchOptions, function(err, filesModified, contextModified, missingModified, fileTimestamps, contextTimestamps) {
103+
this.pausedWatcher = this.watcher;
101104
this.watcher = null;
102105
if(err) return this.handler(err);
103106

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

112115
Watching.prototype.invalidate = function() {
113116
if(this.watcher) {
117+
this.pausedWatcher = this.watcher;
114118
this.watcher.pause();
115119
this.watcher = null;
116120
}
@@ -125,10 +129,15 @@ Watching.prototype.invalidate = function() {
125129
Watching.prototype.close = function(callback) {
126130
if(callback === undefined) callback = function() {};
127131

132+
this.closed = true;
128133
if(this.watcher) {
129134
this.watcher.close();
130135
this.watcher = null;
131136
}
137+
if(this.pausedWatcher) {
138+
this.pausedWatcher.close();
139+
this.pausedWatcher = null;
140+
}
132141
if(this.running) {
133142
this.invalid = true;
134143
this._done = () => {

lib/node/NodeWatchFileSystem.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,15 @@ class NodeWatchFileSystem {
5555
}
5656
return {
5757
close: () => {
58-
this.watcher.close();
58+
if(this.watcher) {
59+
this.watcher.close();
60+
this.watcher = null;
61+
}
5962
},
6063
pause: () => {
61-
this.watcher.pause();
64+
if(this.watcher) {
65+
this.watcher.pause();
66+
}
6267
}
6368
};
6469
}

test/WatchDetection.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ describe("WatchDetection", () => {
102102
function step4() {
103103
onChange = null;
104104

105-
watcher.close();
106-
107-
done();
105+
watcher.close(() => {
106+
setTimeout(done, 1000);
107+
});
108108
}
109109

110110
function handleError(err) {

0 commit comments

Comments
 (0)