Skip to content

Commit

Permalink
fix restoring from cache
Browse files Browse the repository at this point in the history
  • Loading branch information
vankop committed Feb 8, 2022
1 parent 02a7a9e commit cb28b82
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 39 deletions.
22 changes: 11 additions & 11 deletions lib/ContextModule.js
Expand Up @@ -92,17 +92,8 @@ class ContextModule extends Module {
* @param {ContextModuleOptions} options options object
*/
constructor(resolveDependencies, options) {
if (typeof options.resource !== "string") {
super("javascript/dynamic");
/** @type {ContextModuleOptions} */
this.options = {
...options,
resource: options.resource,
resourceQuery: options.resourceQuery || "",
resourceFragment: options.resourceFragment || ""
};
} else {
const parsed = parseResource(options.resource);
if (options && typeof options.resource === "string") {
const parsed = parseResource(options ? options.resource : "");
const resource = parsed.path;
const resourceQuery = (options && options.resourceQuery) || parsed.query;
const resourceFragment =
Expand All @@ -116,6 +107,15 @@ class ContextModule extends Module {
resourceQuery,
resourceFragment
};
} else {
super("javascript/dynamic");
/** @type {ContextModuleOptions} */
this.options = {
...options,
resource: options.resource,
resourceQuery: options.resourceQuery || "",
resourceFragment: options.resourceFragment || ""
};
}

// Info from Factory
Expand Down
108 changes: 80 additions & 28 deletions lib/cache/ResolverCachePlugin.js
Expand Up @@ -128,6 +128,11 @@ class ResolverCachePlugin {
fileDependencies: new LazySet(),
contextDependencies: new LazySet()
};
let yieldResult;
if (typeof newResolveContext.yield === "function") {
yieldResult = [];
newResolveContext.yield = obj => yieldResult.push(obj);
}
const propagate = key => {
if (resolveContext[key]) {
addAllToSet(resolveContext[key], newResolveContext[key]);
Expand Down Expand Up @@ -155,15 +160,19 @@ class ResolverCachePlugin {
snapshotOptions,
(err, snapshot) => {
if (err) return callback(err);
const resolveResult = result || yieldResult;
if (!snapshot) {
if (result) return callback(null, result);
if (resolveResult) return callback(null, resolveResult);
return callback();
}
itemCache.store(new CacheEntry(result, snapshot), storeErr => {
if (storeErr) return callback(storeErr);
if (result) return callback(null, result);
callback();
});
itemCache.store(
new CacheEntry(resolveResult, snapshot),
storeErr => {
if (storeErr) return callback(storeErr);
if (resolveResult) return callback(null, resolveResult);
callback();
}
);
}
);
}
Expand All @@ -173,6 +182,8 @@ class ResolverCachePlugin {
factory(type, hook) {
/** @type {Map<string, (function(Error=, Object=): void)[]>} */
const activeRequests = new Map();
/** @type {Map<string, [function(Error=, Object=): void, function(Error=, Object=): void][]>} */
const activeRequestsWithYield = new Map();
hook.tap(
"ResolverCachePlugin",
/**
Expand All @@ -197,29 +208,63 @@ class ResolverCachePlugin {
if (request._ResolverCachePluginCacheMiss || !fileSystemInfo) {
return callback();
}
const identifier = `${type}${optionsIdent}${objectToString(
request,
!cacheWithContext
)}`;
const activeRequest = activeRequests.get(identifier);
if (activeRequest) {
activeRequest.push(callback);
return;
const withYield = typeof resolveContext.yield === "function";
const identifier = `${type}${
withYield ? "|yield" : "|default"
}${optionsIdent}${objectToString(request, !cacheWithContext)}`;

if (withYield) {
const activeRequest = activeRequestsWithYield.get(identifier);
if (activeRequest) {
activeRequest[0].push(callback);
activeRequest[1].push(resolveContext.yield);
return;
}
} else {
const activeRequest = activeRequests.get(identifier);
if (activeRequest) {
activeRequest.push(callback);
return;
}
}
const itemCache = cache.getItemCache(identifier, null);
let callbacks;
const done = (err, result) => {
if (callbacks === undefined) {
callback(err, result);
callbacks = false;
} else {
for (const callback of callbacks) {
callback(err, result);
}
activeRequests.delete(identifier);
callbacks = false;
}
};
let callbacks, yields;
const done = withYield
? (err, result) => {
if (callbacks === undefined) {
if (err) {
callback(err);
} else {
if (result)
for (const r of result) resolveContext.yield(r);
callback(null, null);
}
yields = undefined;
callbacks = false;
} else {
for (let i = 0; i < callbacks.length; i++) {
const cb = callbacks[i];
const yield_ = yields[i];
if (result) for (const r of result) yield_(r);
cb(null, null);
}
activeRequestsWithYield.delete(identifier);
yields = undefined;
callbacks = false;
}
}
: (err, result) => {
if (callbacks === undefined) {
callback(err, result);
callbacks = false;
} else {
for (const callback of callbacks) {
callback(err, result);
}
activeRequests.delete(identifier);
callbacks = false;
}
};
/**
* @param {Error=} err error if any
* @param {CacheEntry=} cacheEntry cache entry
Expand Down Expand Up @@ -276,7 +321,14 @@ class ResolverCachePlugin {
}
};
itemCache.get(processCacheResult);
if (callbacks === undefined) {
if (withYield && callbacks === undefined) {
callbacks = [callback];
yields = [resolveContext.yield];
activeRequestsWithYield.set(
identifier,
/** @type {[any, any]} */ ([callbacks, yields])
);
} else if (callbacks === undefined) {
callbacks = [callback];
activeRequests.set(identifier, callbacks);
}
Expand Down

0 comments on commit cb28b82

Please sign in to comment.