Skip to content

Commit

Permalink
fix incorrectly reported changes when attaching to an already watched…
Browse files Browse the repository at this point in the history
… directory
  • Loading branch information
sokra committed Oct 30, 2020
1 parent c42f625 commit 128bcd4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 48 deletions.
17 changes: 16 additions & 1 deletion lib/DirectoryWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,22 @@ class DirectoryWatcher extends EventEmitter {
if (safeTime >= startTime) {
process.nextTick(() => {
if (this.closed) return;
watcher.emit("change", safeTime);
if (filePath === this.path) {
watcher.emit(
"change",
filePath,
safeTime,
"watch (outdated on attach)",
true
);
} else {
watcher.emit(
"change",
safeTime,
"watch (outdated on attach)",
true
);
}
});
}
} else if (this.initialScan) {
Expand Down
40 changes: 0 additions & 40 deletions lib/watchpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,46 +302,6 @@ class Watchpack extends EventEmitter {
return map;
}

_missingWatcher(file, watcher) {
if (watcher) {
watcher.on("change", (mtime, type) => {
this._onChange(file, mtime, file, type);
});
watcher.on("remove", type => {
this._onRemove(file, file, type);
});
}
return watcher;
}

_fileWatcher(file, watcher) {
if (watcher) {
watcher.on("initial-missing", type => {
this._onRemove(file, file, type);
});
watcher.on("change", (mtime, type) => {
this._onChange(file, mtime, file, type);
});
watcher.on("remove", type => {
this._onRemove(file, file, type);
});
}
return watcher;
}

_dirWatcher(item, watcher) {
watcher.on("initial-missing", type => {
this._onRemove(item, item, type);
});
watcher.on("change", (file, mtime, type) => {
this._onChange(item, mtime, file, type);
});
watcher.on("remove", type => {
this._onRemove(item, item, type);
});
return watcher;
}

_onChange(item, mtime, file, type) {
file = file || item;
if (this.paused) return;
Expand Down
65 changes: 58 additions & 7 deletions test/Watchpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ describe("Watchpack", function() {

it("should not report changes in initial scan when no start time is provided", function(done) {
var w = new Watchpack({
aggregateTimeout: 1000
aggregateTimeout: 100
});
w.on("aggregated", () => {
done(new Error("should not fire"));
Expand All @@ -818,18 +818,69 @@ describe("Watchpack", function() {
testHelper.dir("dir/b");
testHelper.dir("dir/b/sub");
testHelper.file("dir/b/sub/file");
testHelper.dir("dir/b/sub/sub");
testHelper.file("dir/b/sub/sub/file");
testHelper.file("dir/b/file");
testHelper.file("dir/a");
testHelper.tick(() => {
testHelper.tick(1000, () => {
w.watch({
files: [path.join(fixtures, "dir", "a")],
directories: [path.join(fixtures, "dir", "b")],
missing: [path.join(fixtures, "dir", "c")]
directories: [path.join(fixtures, "dir", "b", "sub")]
});
testHelper.tick(2000, () => {
// no event fired
w.close();
done();
w.watch({
files: [path.join(fixtures, "dir", "a")],
directories: [
path.join(fixtures, "dir", "b", "sub"),
path.join(fixtures, "dir", "b")
],
missing: [path.join(fixtures, "dir", "c")]
});
testHelper.tick(2000, () => {
// no event fired
w.close();
done();
});
});
});
});

it("should not report changes in initial scan when start time is provided", function(done) {
var w = new Watchpack({
aggregateTimeout: 100
});
w.on("aggregated", () => {
done(new Error("should not fire"));
});
testHelper.dir("dir");
testHelper.dir("dir/b");
testHelper.dir("dir/b/sub");
testHelper.file("dir/b/sub/file");
testHelper.dir("dir/b/sub/sub");
testHelper.file("dir/b/sub/sub/file");
testHelper.file("dir/b/file");
testHelper.file("dir/a");
testHelper.tick(1000, () => {
w.watch({
directories: [path.join(fixtures, "dir", "b", "sub")],
startTime: Date.now()
});
testHelper.tick(2000, () => {
// no event fired
w.watch({
files: [path.join(fixtures, "dir", "a")],
directories: [
path.join(fixtures, "dir", "b", "sub"),
path.join(fixtures, "dir", "b")
],
missing: [path.join(fixtures, "dir", "c")],
startTime: Date.now()
});
testHelper.tick(2000, () => {
// no event fired
w.close();
done();
});
});
});
});
Expand Down

0 comments on commit 128bcd4

Please sign in to comment.