Skip to content

Commit

Permalink
Merge d5c1455 into 8b37d6a
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jul 15, 2017
2 parents 8b37d6a + d5c1455 commit e254670
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 16 deletions.
45 changes: 29 additions & 16 deletions lib/DirectoryWatcher.js
Expand Up @@ -120,24 +120,37 @@ DirectoryWatcher.prototype.setFileTime = function setFileTime(filePath, mtime, i
};

DirectoryWatcher.prototype.setDirectory = function setDirectory(directoryPath, exist, initial, type) {
var old = this.directories[directoryPath];
if(!old) {
if(exist) {
if(this.nestedWatching) {
this.createNestedWatcher(directoryPath);
} else {
this.directories[directoryPath] = true;
}
if(directoryPath === this.path) {
if(!initial && this.watchers[withoutCase(this.path)]) {
this.watchers[withoutCase(this.path)].forEach(function(w) {
w.emit("change", directoryPath, w.data, initial ? "initial" : type);
});
}
} else {
if(!exist) {
if(this.nestedWatching)
this.directories[directoryPath].close();
delete this.directories[directoryPath];
if(!initial && this.watchers[withoutCase(this.path)]) {
this.watchers[withoutCase(this.path)].forEach(function(w) {
w.emit("change", directoryPath, w.data, initial ? "initial" : type);
});
var old = this.directories[directoryPath];
if(!old) {
if(exist) {
if(this.nestedWatching) {
this.createNestedWatcher(directoryPath);
} else {
this.directories[directoryPath] = true;
}
if(!initial && this.watchers[withoutCase(this.path)]) {
this.watchers[withoutCase(this.path)].forEach(function(w) {
w.emit("change", directoryPath, w.data, initial ? "initial" : type);
});
}
}
} else {
if(!exist) {
if(this.nestedWatching)
this.directories[directoryPath].close();
delete this.directories[directoryPath];
if(!initial && this.watchers[withoutCase(this.path)]) {
this.watchers[withoutCase(this.path)].forEach(function(w) {
w.emit("change", directoryPath, w.data, initial ? "initial" : type);
});
}
}
}
}
Expand Down
50 changes: 50 additions & 0 deletions test/Watchpack.js
Expand Up @@ -194,6 +194,56 @@ describe("Watchpack", function() {
});
});

it("should watch a missing directory", function(done) {
var w = new Watchpack({
aggregateTimeout: 1000
});
var changeEvents = [];
w.on("change", function(file) {
if(changeEvents[changeEvents.length - 1] === file)
return;
changeEvents.push(file);
});
w.on("aggregated", function(changes) {
changes.should.be.eql([path.join(fixtures, "dir", "sub")]);
changeEvents.should.be.eql([path.join(fixtures, "dir", "sub")]);
w.close();
done();
});
testHelper.dir("dir");
testHelper.tick(function() {
w.watch([], [path.join(fixtures, "dir", "sub")]);
testHelper.tick(function() {
testHelper.dir(path.join("dir", "sub"));
});
});
});

it("should watch a directory (add directory)", function(done) {
var w = new Watchpack({
aggregateTimeout: 1000
});
var changeEvents = [];
w.on("change", function(file) {
if(changeEvents[changeEvents.length - 1] === file)
return;
changeEvents.push(file);
});
w.on("aggregated", function(changes) {
changes.should.be.eql([path.join(fixtures, "dir")]);
changeEvents.should.be.eql([path.join(fixtures, "dir", "sub")]);
w.close();
done();
});
testHelper.dir("dir");
testHelper.tick(function() {
w.watch([], [path.join(fixtures, "dir")]);
testHelper.tick(function() {
testHelper.dir(path.join("dir", "sub"));
});
});
});

it("should watch a directory (delete directory)", function(done) {
var w = new Watchpack({
aggregateTimeout: 1000
Expand Down

0 comments on commit e254670

Please sign in to comment.