Skip to content

Commit

Permalink
fix watching for not-existent directories
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Feb 28, 2018
1 parent 6a4b0af commit 1baaa86
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/DirectoryWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function Watcher(directoryWatcher, filePath, startTime) {
this.directoryWatcher = directoryWatcher;
this.path = filePath;
this.startTime = startTime && +startTime;
// TODO this.data seem to be only read, weird
this.data = 0;
}

Expand Down Expand Up @@ -72,6 +73,7 @@ function DirectoryWatcher(directoryPath, options) {
this.initialScanRemoved = [];
this.doInitialScan();
this.watchers = Object.create(null);
this.parentWatcher = null;
this.refs = 0;
}
module.exports = DirectoryWatcher;
Expand Down Expand Up @@ -142,6 +144,11 @@ DirectoryWatcher.prototype.setDirectory = function setDirectory(directoryPath, e
w.emit("change", directoryPath, w.data, initial ? "initial" : type);
});
}
if(this.watchers[withoutCase(directoryPath) + "#directory"]) {
this.watchers[withoutCase(directoryPath) + "#directory"].forEach(function(w) {
w.emit("change", w.data, initial ? "initial" : type);
});
}
}
} else {
if(!exist) {
Expand All @@ -153,6 +160,11 @@ DirectoryWatcher.prototype.setDirectory = function setDirectory(directoryPath, e
w.emit("change", directoryPath, w.data, initial ? "initial" : type);
});
}
if(this.watchers[withoutCase(directoryPath) + "#directory"]) {
this.watchers[withoutCase(directoryPath) + "#directory"].forEach(function(w) {
w.emit("change", directoryPath, w.data, initial ? "initial" : type);
});
}
}
}
}
Expand Down Expand Up @@ -274,6 +286,14 @@ DirectoryWatcher.prototype.onWatcherError = function onWatcherError(/* err */) {
DirectoryWatcher.prototype.doInitialScan = function doInitialScan() {
fs.readdir(this.path, function(err, items) {
if(err) {
this.parentWatcher = watcherManager.watchFile(this.path + "#directory", this.options, 1);
this.parentWatcher.on("change", function(mtime, type) {
if(this.watchers[withoutCase(this.path)]) {
this.watchers[withoutCase(this.path)].forEach(function(w) {
w.emit("change", this.path, mtime, type);
});
}
}.bind(this));
this.initialScan = false;
return;
}
Expand Down Expand Up @@ -340,6 +360,7 @@ DirectoryWatcher.prototype.close = function() {
this.directories[dir].close();
}, this);
}
if(this.parentWatcher) this.parentWatcher.close();
this.emit("closed");
};

Expand Down

0 comments on commit 1baaa86

Please sign in to comment.