Skip to content

Commit a3b2b82

Browse files
committed
provide additional method instead of changing existing one
avoid deeply collecting watchers since they are always collected during time collection add existence file time info for directories fix safeTime collection
1 parent 4c9c76f commit a3b2b82

4 files changed

Lines changed: 44 additions & 27 deletions

File tree

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,19 @@ const { changes, removals } = wp.getAggregated();
112112
// when futher changes happen
113113
// Can also be used when paused.
114114

115-
// Watchpack.prototype.getTimeInfoEntries()
116-
var fileTimes = wp.getTimeInfoEntries();
117-
// returns a Map with all known time info objects for files and directories
115+
// Watchpack.prototype.collectTimeInfoEntries(fileInfoEntries: Map<string, Entry>, directoryInfoEntries: Map<string, Entry>)
116+
wp.collectTimeInfoEntries(fileInfoEntries, directoryInfoEntries);
117+
// collects time info objects for all known files and directories
118118
// this include info from files not directly watched
119119
// key: absolute path, value: object with { safeTime, timestamp }
120120
// safeTime: a point in time at which it is safe to say all changes happened before that
121121
// timestamp: only for files, the mtime timestamp of the file
122122

123+
// Watchpack.prototype.getTimeInfoEntries()
124+
var fileTimes = wp.getTimeInfoEntries();
125+
// returns a Map with all known time info objects for files and directories
126+
// similar to collectTimeInfoEntries but returns a single map with all entries
127+
123128
// (deprecated)
124129
// Watchpack.prototype.getTimes()
125130
var fileTimes = wp.getTimes();

lib/DirectoryWatcher.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -706,29 +706,34 @@ class DirectoryWatcher extends EventEmitter {
706706
return obj;
707707
}
708708

709-
getTimeInfoEntries(fileTimestamps, directoryTimestamps, safeTime) {
710-
safeTime.value = Math.max(safeTime.value, this.lastWatchEvent);
709+
collectTimeInfoEntries(fileTimestamps, directoryTimestamps) {
710+
let safeTime = this.lastWatchEvent;
711711
for (const [file, entry] of this.files) {
712712
fixupEntryAccuracy(entry);
713-
safeTime.value = Math.max(safeTime.value, entry.safeTime);
713+
safeTime = Math.max(safeTime, entry.safeTime);
714714
fileTimestamps.set(file, entry);
715715
}
716716
if (this.nestedWatching) {
717717
for (const w of this.directories.values()) {
718-
w.directoryWatcher.getTimeInfoEntries(
719-
fileTimestamps,
720-
directoryTimestamps,
721-
safeTime
718+
safeTime = Math.max(
719+
safeTime,
720+
w.directoryWatcher.collectTimeInfoEntries(
721+
fileTimestamps,
722+
directoryTimestamps,
723+
safeTime
724+
)
722725
);
723726
}
727+
fileTimestamps.set(this.path, EXISTANCE_ONLY_TIME_ENTRY);
724728
directoryTimestamps.set(this.path, {
725-
safeTime: safeTime.value
729+
safeTime
726730
});
727731
} else {
728732
for (const dir of this.directories.keys()) {
729733
// No additional info about this directory
730734
directoryTimestamps.set(dir, EXISTANCE_ONLY_TIME_ENTRY);
731735
}
736+
fileTimestamps.set(this.path, EXISTANCE_ONLY_TIME_ENTRY);
732737
directoryTimestamps.set(this.path, EXISTANCE_ONLY_TIME_ENTRY);
733738
}
734739
if (!this.initialScan) {
@@ -741,6 +746,7 @@ class DirectoryWatcher extends EventEmitter {
741746
}
742747
}
743748
}
749+
return safeTime;
744750
}
745751

746752
close() {

lib/watchpack.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@ const EventEmitter = require("events").EventEmitter;
1010
const globToRegExp = require("glob-to-regexp");
1111
const watchEventSource = require("./watchEventSource");
1212

13-
let EXISTANCE_ONLY_TIME_ENTRY; // lazy required
14-
1513
const EMPTY_ARRAY = [];
1614
const EMPTY_OPTIONS = {};
1715

1816
function addWatchersToSet(watchers, set) {
1917
for (const w of watchers) {
20-
if (w !== true && !set.has(w.directoryWatcher)) {
18+
if (!set.has(w.directoryWatcher)) {
2119
set.add(w.directoryWatcher);
22-
addWatchersToSet(w.directoryWatcher.directories.values(), set);
2320
}
2421
}
2522
}
@@ -275,20 +272,20 @@ class Watchpack extends EventEmitter {
275272
return obj;
276273
}
277274

278-
getTimeInfoEntries(fileTimestamps, directoryTimestamps) {
275+
getTimeInfoEntries() {
276+
const map = new Map();
277+
this.collectTimeInfoEntries(map, map);
278+
return map;
279+
}
280+
281+
collectTimeInfoEntries(fileTimestamps, directoryTimestamps) {
279282
const allWatchers = new Set();
280283
addWatchersToSet(this.fileWatchers.values(), allWatchers);
281284
addWatchersToSet(this.directoryWatchers.values(), allWatchers);
282-
const map = new Map();
283-
// if timestamp maps are passed in, populate them, otherwise return a new map with both file and directory timestamps.
284-
if (!fileTimestamps && !directoryTimestamps) {
285-
fileTimestamps = directoryTimestamps = map;
286-
}
287285
const safeTime = { value: 0 };
288286
for (const w of allWatchers) {
289-
w.getTimeInfoEntries(fileTimestamps, directoryTimestamps, safeTime);
287+
w.collectTimeInfoEntries(fileTimestamps, directoryTimestamps, safeTime);
290288
}
291-
return map;
292289
}
293290

294291
getAggregated() {

test/Watchpack.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,22 +538,31 @@ describe("Watchpack", function() {
538538
changeEvents.should.be.eql([path.join(fixtures, "dir", "sub", "a")]);
539539
const files = new Map();
540540
const directories = new Map();
541-
const times = w.getTimeInfoEntries();
542-
w.getTimeInfoEntries(files, directories);
543-
// aggregated results should be the same as seperated maps
544-
Array.from(times).sort().should.be.eql([...Array.from(files), ...Array.from(directories)].sort())
541+
w.collectTimeInfoEntries(files, directories);
545542
const dir = directories.get(path.join(fixtures, "dir"));
543+
const dirAsFile = files.get(path.join(fixtures, "dir"));
546544
const sub = directories.get(path.join(fixtures, "dir", "sub"));
545+
const subAsFile = files.get(path.join(fixtures, "dir", "sub"));
547546
const a = files.get(path.join(fixtures, "dir", "sub", "a"));
548547
dir.should.be.type("object");
549548
dir.should.have.property("safeTime");
549+
dirAsFile.should.be.type("object");
550+
dirAsFile.should.not.have.property("safeTime");
551+
sub.should.be.type("object");
552+
sub.should.have.property("safeTime");
553+
subAsFile.should.be.type("object");
554+
subAsFile.should.not.have.property("safeTime");
555+
a.should.be.type("object");
556+
a.should.have.property("safeTime");
557+
a.should.have.property("timestamp");
550558
sub.safeTime.should.be.aboveOrEqual(a.safeTime);
551559
dir.safeTime.should.be.aboveOrEqual(sub.safeTime);
552560
w.close();
553561
done();
554562
});
555563
testHelper.dir("dir");
556564
testHelper.dir(path.join("dir", "sub"));
565+
testHelper.dir(path.join("dir", "sub2"));
557566
testHelper.tick(function() {
558567
w.watch([], [path.join(fixtures, "dir")]);
559568
testHelper.tick(function() {

0 commit comments

Comments
 (0)