Skip to content

Commit 732f8b7

Browse files
committedAug 6, 2020
create less watchers when no options are passed
fix type bug improve test case
1 parent 7c197b4 commit 732f8b7

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed
 

‎lib/watchEventSource.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ const execute = () => {
254254
} else {
255255
for (const filePath of filePaths) {
256256
const w = createDirectWatcher(filePath);
257-
for (const watcher of entry) {
257+
for (const watcher of entry.keys()) {
258258
const old = underlyingWatcher.get(watcher);
259259
if (old === w) continue;
260260
w.add(watcher);

‎lib/watchpack.js

+14-18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const watchEventSource = require("./watchEventSource");
1313
let EXISTANCE_ONLY_TIME_ENTRY; // lazy required
1414

1515
const EMPTY_ARRAY = [];
16+
const EMPTY_OPTIONS = {};
1617

1718
function addWatchersToSet(watchers, set) {
1819
for (const w of watchers) {
@@ -64,11 +65,12 @@ const cachedNormalizeOptions = options => {
6465
class Watchpack extends EventEmitter {
6566
constructor(options) {
6667
super();
67-
if (!options) options = {};
68-
if (typeof options.aggregateTimeout !== "number") {
69-
options.aggregateTimeout = 200;
70-
}
68+
if (!options) options = EMPTY_OPTIONS;
7169
this.options = options;
70+
this.aggregateTimeout =
71+
typeof options.aggregateTimeout === "number"
72+
? options.aggregateTimeout
73+
: 200;
7274
this.watcherOptions = cachedNormalizeOptions(options);
7375
this.watcherManager = getWatcherManager(this.watcherOptions);
7476
this.fileWatchers = new Map();
@@ -77,7 +79,7 @@ class Watchpack extends EventEmitter {
7779
this.paused = false;
7880
this.aggregatedChanges = new Set();
7981
this.aggregatedRemovals = new Set();
80-
this.aggregateTimeout = 0;
82+
this.aggregateTimer = undefined;
8183
this._onTimeout = this._onTimeout.bind(this);
8284
}
8385

@@ -249,7 +251,7 @@ class Watchpack extends EventEmitter {
249251

250252
close() {
251253
this.paused = true;
252-
if (this.aggregateTimeout) clearTimeout(this.aggregateTimeout);
254+
if (this.aggregateTimer) clearTimeout(this.aggregateTimer);
253255
for (const w of this.fileWatchers.values()) w.close();
254256
for (const w of this.directoryWatchers.values()) w.close();
255257
this.fileWatchers.clear();
@@ -258,7 +260,7 @@ class Watchpack extends EventEmitter {
258260

259261
pause() {
260262
this.paused = true;
261-
if (this.aggregateTimeout) clearTimeout(this.aggregateTimeout);
263+
if (this.aggregateTimer) clearTimeout(this.aggregateTimer);
262264
}
263265

264266
getTimes() {
@@ -344,30 +346,24 @@ class Watchpack extends EventEmitter {
344346
file = file || item;
345347
if (this.paused) return;
346348
this.emit("change", file, mtime, type);
347-
if (this.aggregateTimeout) clearTimeout(this.aggregateTimeout);
349+
if (this.aggregateTimer) clearTimeout(this.aggregateTimer);
348350
this.aggregatedRemovals.delete(item);
349351
this.aggregatedChanges.add(item);
350-
this.aggregateTimeout = setTimeout(
351-
this._onTimeout,
352-
this.options.aggregateTimeout
353-
);
352+
this.aggregateTimer = setTimeout(this._onTimeout, this.aggregateTimeout);
354353
}
355354

356355
_onRemove(item, file, type) {
357356
file = file || item;
358357
if (this.paused) return;
359358
this.emit("remove", file, type);
360-
if (this.aggregateTimeout) clearTimeout(this.aggregateTimeout);
359+
if (this.aggregateTimer) clearTimeout(this.aggregateTimer);
361360
this.aggregatedChanges.delete(item);
362361
this.aggregatedRemovals.add(item);
363-
this.aggregateTimeout = setTimeout(
364-
this._onTimeout,
365-
this.options.aggregateTimeout
366-
);
362+
this.aggregateTimer = setTimeout(this._onTimeout, this.aggregateTimeout);
367363
}
368364

369365
_onTimeout() {
370-
this.aggregateTimeout = 0;
366+
this.aggregateTimer = undefined;
371367
const changes = this.aggregatedChanges;
372368
const removals = this.aggregatedRemovals;
373369
this.aggregatedChanges = new Set();

‎test/Watchpack.js

+32-7
Original file line numberDiff line numberDiff line change
@@ -590,12 +590,11 @@ describe("Watchpack", function() {
590590
});
591591

592592
it("should detect a single change to future timestamps", function(done) {
593-
var w = new Watchpack({
594-
aggregateTimeout: 1000
595-
});
596-
var w2 = new Watchpack({
593+
const options = {
597594
aggregateTimeout: 1000
598-
});
595+
};
596+
var w = new Watchpack(options);
597+
var w2 = new Watchpack(options);
599598
w.on("change", function() {
600599
throw new Error("should not report change event");
601600
});
@@ -604,15 +603,41 @@ describe("Watchpack", function() {
604603
});
605604
testHelper.file("a");
606605
testHelper.tick(400, function() {
607-
w2.watch([path.join(fixtures, "a")], []);
606+
w2.watch([path.join(fixtures, "a")], [], Date.now());
608607
testHelper.tick(1000, function() {
609608
// wait for initial scan
610609
testHelper.mtime("a", Date.now() + 1000000);
611610
testHelper.tick(400, function() {
612-
w.watch([path.join(fixtures, "a")], []);
611+
w.watch([path.join(fixtures, "a")], [], Date.now());
612+
testHelper.tick(1000, function() {
613+
w2.close();
614+
w.close();
615+
done();
616+
});
617+
});
618+
});
619+
});
620+
});
621+
622+
it("should create different watchers for different options", function(done) {
623+
var w = new Watchpack({
624+
aggregateTimeout: 1000
625+
});
626+
var w2 = new Watchpack({
627+
aggregateTimeout: 1000
628+
});
629+
testHelper.file("a");
630+
testHelper.tick(400, function() {
631+
w.watch([path.join(fixtures, "a")], [], Date.now());
632+
w2.watch([path.join(fixtures, "a")], [], Date.now());
633+
testHelper.tick(1000, function() {
634+
testHelper.file("a");
635+
testHelper.tick(400, function() {
636+
testHelper.file("a");
613637
testHelper.tick(1000, function() {
614638
w2.close();
615639
w.close();
640+
console.log("test done");
616641
done();
617642
});
618643
});

0 commit comments

Comments
 (0)
Please sign in to comment.