watchEventSource.js
8.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const fs = require("fs");
const path = require("path");
const { EventEmitter } = require("events");
const reducePlan = require("./reducePlan");
const IS_OSX = require("os").platform() === "darwin";
const IS_WIN = require("os").platform() === "win32";
const SUPPORTS_RECURSIVE_WATCHING = IS_OSX || IS_WIN;
const watcherLimit =
+process.env.WATCHPACK_WATCHER_LIMIT || (IS_OSX ? 2000 : 10000);
const recursiveWatcherLogging = !!process.env
.WATCHPACK_RECURSIVE_WATCHER_LOGGING;
let isBatch = false;
let watcherCount = 0;
/** @type {Map<Watcher, string>} */
const pendingWatchers = new Map();
/** @type {Map<string, RecursiveWatcher>} */
const recursiveWatchers = new Map();
/** @type {Map<string, DirectWatcher>} */
const directWatchers = new Map();
/** @type {Map<Watcher, RecursiveWatcher | DirectWatcher>} */
const underlyingWatcher = new Map();
class DirectWatcher {
constructor(filePath) {
this.filePath = filePath;
this.watchers = new Set();
this.watcher = undefined;
try {
const watcher = fs.watch(filePath);
this.watcher = watcher;
watcher.on("change", (type, filename) => {
for (const w of this.watchers) {
w.emit("change", type, filename);
}
});
watcher.on("error", error => {
for (const w of this.watchers) {
w.emit("error", error);
}
});
} catch (err) {
process.nextTick(() => {
for (const w of this.watchers) {
w.emit("error", err);
}
});
}
watcherCount++;
}
add(watcher) {
underlyingWatcher.set(watcher, this);
this.watchers.add(watcher);
}
remove(watcher) {
this.watchers.delete(watcher);
if (this.watchers.size === 0) {
directWatchers.delete(this.filePath);
watcherCount--;
if (this.watcher) this.watcher.close();
}
}
getWatchers() {
return this.watchers;
}
}
class RecursiveWatcher {
constructor(rootPath) {
this.rootPath = rootPath;
/** @type {Map<Watcher, string>} */
this.mapWatcherToPath = new Map();
/** @type {Map<string, Set<Watcher>>} */
this.mapPathToWatchers = new Map();
this.watcher = undefined;
try {
const watcher = fs.watch(rootPath, {
recursive: true
});
this.watcher = watcher;
watcher.on("change", (type, filename) => {
if (!filename) {
if (recursiveWatcherLogging) {
process.stderr.write(
`[watchpack] dispatch ${type} event in recursive watcher (${
this.rootPath
}) to all watchers\n`
);
}
for (const w of this.mapWatcherToPath.keys()) {
w.emit("change", type);
}
} else {
const dir = path.dirname(filename);
const watchers = this.mapPathToWatchers.get(dir);
if (recursiveWatcherLogging) {
process.stderr.write(
`[watchpack] dispatch ${type} event in recursive watcher (${
this.rootPath
}) for '${filename}' to ${
watchers ? watchers.size : 0
} watchers\n`
);
}
if (watchers === undefined) return;
for (const w of watchers) {
w.emit("change", type, path.basename(filename));
}
}
});
watcher.on("error", error => {
for (const w of this.mapWatcherToPath.keys()) {
w.emit("error", error);
}
});
} catch (err) {
process.nextTick(() => {
for (const w of this.mapWatcherToPath.keys()) {
w.emit("error", err);
}
});
}
watcherCount++;
if (recursiveWatcherLogging) {
process.stderr.write(
`[watchpack] created recursive watcher at ${rootPath}\n`
);
}
}
add(filePath, watcher) {
underlyingWatcher.set(watcher, this);
const subpath = filePath.slice(this.rootPath.length + 1) || ".";
this.mapWatcherToPath.set(watcher, subpath);
const set = this.mapPathToWatchers.get(subpath);
if (set === undefined) {
const newSet = new Set();
newSet.add(watcher);
this.mapPathToWatchers.set(subpath, newSet);
} else {
set.add(watcher);
}
}
remove(watcher) {
const subpath = this.mapWatcherToPath.get(watcher);
if (!subpath) return;
this.mapWatcherToPath.delete(watcher);
const set = this.mapPathToWatchers.get(subpath);
set.delete(watcher);
if (set.size === 0) {
this.mapPathToWatchers.delete(subpath);
}
if (this.mapWatcherToPath.size === 0) {
recursiveWatchers.delete(this.rootPath);
watcherCount--;
if (this.watcher) this.watcher.close();
if (recursiveWatcherLogging) {
process.stderr.write(
`[watchpack] closed recursive watcher at ${this.rootPath}\n`
);
}
}
}
getWatchers() {
return this.mapWatcherToPath;
}
}
class Watcher extends EventEmitter {
close() {
if (pendingWatchers.has(this)) {
pendingWatchers.delete(this);
return;
}
const watcher = underlyingWatcher.get(this);
watcher.remove(this);
underlyingWatcher.delete(this);
}
}
const createDirectWatcher = filePath => {
const existing = directWatchers.get(filePath);
if (existing !== undefined) return existing;
const w = new DirectWatcher(filePath);
directWatchers.set(filePath, w);
return w;
};
const createRecursiveWatcher = rootPath => {
const existing = recursiveWatchers.get(rootPath);
if (existing !== undefined) return existing;
const w = new RecursiveWatcher(rootPath);
recursiveWatchers.set(rootPath, w);
return w;
};
const execute = () => {
/** @type {Map<string, Watcher[] | Watcher>} */
const map = new Map();
const addWatcher = (watcher, filePath) => {
const entry = map.get(filePath);
if (entry === undefined) {
map.set(filePath, watcher);
} else if (Array.isArray(entry)) {
entry.push(watcher);
} else {
map.set(filePath, [entry, watcher]);
}
};
for (const [watcher, filePath] of pendingWatchers) {
addWatcher(watcher, filePath);
}
pendingWatchers.clear();
// Fast case when we are not reaching the limit
if (!SUPPORTS_RECURSIVE_WATCHING || watcherLimit - watcherCount >= map.size) {
// Create watchers for all entries in the map
for (const [filePath, entry] of map) {
const w = createDirectWatcher(filePath);
if (Array.isArray(entry)) {
for (const item of entry) w.add(item);
} else {
w.add(entry);
}
}
return;
}
// Reconsider existing watchers to improving watch plan
for (const watcher of recursiveWatchers.values()) {
for (const [w, subpath] of watcher.getWatchers()) {
addWatcher(w, path.join(watcher.rootPath, subpath));
}
}
for (const watcher of directWatchers.values()) {
for (const w of watcher.getWatchers()) {
addWatcher(w, watcher.filePath);
}
}
// Merge map entries to keep watcher limit
// Create a 10% buffer to be able to enter fast case more often
const plan = reducePlan(map, watcherLimit * 0.9);
// Update watchers for all entries in the map
for (const [filePath, entry] of plan) {
if (entry.size === 1) {
for (const [watcher, filePath] of entry) {
const w = createDirectWatcher(filePath);
const old = underlyingWatcher.get(watcher);
if (old === w) continue;
w.add(watcher);
if (old !== undefined) old.remove(watcher);
}
} else {
const filePaths = new Set(entry.values());
if (filePaths.size > 1) {
const w = createRecursiveWatcher(filePath);
for (const [watcher, watcherPath] of entry) {
const old = underlyingWatcher.get(watcher);
if (old === w) continue;
w.add(watcherPath, watcher);
if (old !== undefined) old.remove(watcher);
}
} else {
for (const filePath of filePaths) {
const w = createDirectWatcher(filePath);
for (const watcher of entry.keys()) {
const old = underlyingWatcher.get(watcher);
if (old === w) continue;
w.add(watcher);
if (old !== undefined) old.remove(watcher);
}
}
}
}
}
};
exports.watch = filePath => {
const watcher = new Watcher();
// Find an existing watcher
const directWatcher = directWatchers.get(filePath);
if (directWatcher !== undefined) {
directWatcher.add(watcher);
return watcher;
}
let current = filePath;
for (;;) {
const recursiveWatcher = recursiveWatchers.get(current);
if (recursiveWatcher !== undefined) {
recursiveWatcher.add(filePath, watcher);
return watcher;
}
const parent = path.dirname(current);
if (parent === current) break;
current = parent;
}
// Queue up watcher for creation
pendingWatchers.set(watcher, filePath);
if (!isBatch) execute();
return watcher;
};
exports.batch = fn => {
isBatch = true;
try {
fn();
} finally {
isBatch = false;
execute();
}
};
exports.getNumberOfWatchers = () => {
return watcherCount;
};