Skip to content

Commit cf71cd0

Browse files
committed
faster way to provide got handlers for cache
1 parent 3f1cab2 commit cf71cd0

File tree

3 files changed

+51
-41
lines changed

3 files changed

+51
-41
lines changed

lib/Cache.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,23 @@ const { AsyncParallelHook, AsyncSeriesBailHook, SyncHook } = require("tapable");
99

1010
/** @typedef {(result: any, callback: (err?: Error) => void) => void} GotHandler */
1111

12+
const needCalls = (times, callback) => {
13+
return err => {
14+
if (--times === 0) {
15+
return callback(err);
16+
}
17+
if (err && times > 0) {
18+
times = NaN;
19+
return callback(err);
20+
}
21+
};
22+
};
23+
1224
class Cache {
1325
constructor() {
1426
this.hooks = {
1527
/** @type {AsyncSeriesBailHook<string, string>} */
16-
get: new AsyncSeriesBailHook(["identifier", "etag"]),
17-
/** @type {AsyncParallelHook<string, string, any>} */
18-
got: new AsyncParallelHook(["identifier", "etag", "data"]),
28+
get: new AsyncSeriesBailHook(["identifier", "etag", "gotHandlers"]),
1929
/** @type {AsyncParallelHook<string, string, any>} */
2030
store: new AsyncParallelHook(["identifier", "etag", "data"]),
2131
/** @type {SyncHook} */
@@ -28,18 +38,24 @@ class Cache {
2838
}
2939

3040
get(identifier, etag, callback) {
31-
this.hooks.get.callAsync(identifier, etag, (err, result) => {
41+
const gotHandlers = [];
42+
this.hooks.get.callAsync(identifier, etag, gotHandlers, (err, result) => {
3243
if (err) {
3344
callback(err);
3445
return;
3546
}
36-
this.hooks.got.callAsync(identifier, etag, result, err => {
37-
if (err) {
38-
callback(err);
39-
return;
47+
if (gotHandlers.length > 1) {
48+
const innerCallback = needCalls(gotHandlers.length, () =>
49+
callback(null, result)
50+
);
51+
for (const gotHandler of gotHandlers) {
52+
gotHandler(result, innerCallback);
4053
}
54+
} else if (gotHandlers.length === 1) {
55+
gotHandlers[0](result, () => callback(null, result));
56+
} else {
4157
callback(null, result);
42-
});
58+
}
4359
});
4460
}
4561

lib/cache/FileCachePlugin.js

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ class FileCachePlugin {
108108
*/
109109
constructor(options) {
110110
this.options = options;
111-
this.missingEntries = new Set();
112111
}
113112

114113
/**
@@ -179,7 +178,6 @@ class FileCachePlugin {
179178
});
180179
}
181180
const storeEntry = (identifier, etag, data) => {
182-
this.missingEntries.delete(identifier);
183181
const entry = {
184182
identifier,
185183
data: etag ? () => data : data,
@@ -227,20 +225,10 @@ class FileCachePlugin {
227225
}
228226
};
229227
compiler.cache.hooks.store.tapPromise("FileCachePlugin", storeEntry);
230-
compiler.cache.hooks.got.tapPromise(
231-
"FileCachePlugin",
232-
(identifier, etag, result) => {
233-
if (result !== undefined && this.missingEntries.has(identifier)) {
234-
return storeEntry(identifier, etag, result);
235-
} else {
236-
return resolvedPromise;
237-
}
238-
}
239-
);
240228

241229
compiler.cache.hooks.get.tapPromise(
242230
"FileCachePlugin",
243-
(identifier, etag) => {
231+
(identifier, etag, gotHandlers) => {
244232
let logMessage;
245233
let cacheEntryPromise;
246234
if (store === "pack") {
@@ -252,38 +240,43 @@ class FileCachePlugin {
252240
logMessage = filename;
253241
cacheEntryPromise = serializer.deserializeFromFile(filename);
254242
}
243+
const registerGot = () => {
244+
gotHandlers.push((result, callback) => {
245+
if (result !== undefined) {
246+
storeEntry(identifier, etag, result).then(callback, callback);
247+
} else {
248+
callback();
249+
}
250+
});
251+
};
255252
return cacheEntryPromise.then(
256253
cacheEntry => {
257254
if (cacheEntry === undefined) {
258-
this.missingEntries.add(identifier);
259-
return;
255+
return registerGot();
260256
}
261257
if (cacheEntry.identifier !== identifier) {
262258
if (log >= 3) {
263259
console.warn(
264260
`Restored ${identifier} from ${logMessage}, but identifier doesn't match.`
265261
);
266262
}
267-
this.missingEntries.add(identifier);
268-
return;
263+
return registerGot();
269264
}
270265
if (cacheEntry.etag !== etag) {
271266
if (log >= 3) {
272267
console.warn(
273268
`Restored ${identifier} from ${logMessage}, but etag doesn't match.`
274269
);
275270
}
276-
this.missingEntries.add(identifier);
277-
return;
271+
return registerGot();
278272
}
279273
if (cacheEntry.version !== version) {
280274
if (log >= 3) {
281275
console.warn(
282276
`Restored ${identifier} from ${logMessage}, but version doesn't match.`
283277
);
284278
}
285-
this.missingEntries.add(identifier);
286-
return;
279+
return registerGot();
287280
}
288281
if (log >= 3) {
289282
console.warn(`Restored ${identifier} from ${logMessage}.`);
@@ -292,14 +285,14 @@ class FileCachePlugin {
292285
return cacheEntry.data;
293286
},
294287
err => {
295-
this.missingEntries.add(identifier);
296288
if (log >= 1 && err && err.code !== "ENOENT") {
297289
console.warn(
298290
`Restoring failed for ${identifier} from ${logMessage}: ${
299291
log >= 4 ? err.stack : err
300292
}`
301293
);
302294
}
295+
registerGot();
303296
}
304297
);
305298
}

lib/cache/MemoryCachePlugin.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,21 @@ class MemoryCachePlugin {
2323
cache.set(identifier, { etag, data });
2424
}
2525
);
26-
compiler.cache.hooks.got.tap(
26+
compiler.cache.hooks.get.tap(
2727
"MemoryCachePlugin",
28-
(identifier, etag, result) => {
29-
if (result !== undefined) {
30-
cache.set(identifier, { etag, data: result });
28+
(identifier, etag, gotHandlers) => {
29+
const cacheEntry = cache.get(identifier);
30+
if (cacheEntry !== undefined && cacheEntry.etag === etag) {
31+
return cacheEntry.data;
3132
}
33+
gotHandlers.push((result, callback) => {
34+
if (result !== undefined) {
35+
cache.set(identifier, { etag, data: result });
36+
}
37+
return callback();
38+
});
3239
}
3340
);
34-
compiler.cache.hooks.get.tap("MemoryCachePlugin", (identifier, etag) => {
35-
const cacheEntry = cache.get(identifier);
36-
if (cacheEntry !== undefined && cacheEntry.etag === etag) {
37-
return cacheEntry.data;
38-
}
39-
});
4041
}
4142
}
4243
module.exports = MemoryCachePlugin;

0 commit comments

Comments
 (0)