MCPcopy
hub / github.com/webpack/webpack / makeCacheableWithContext

Function makeCacheableWithContext

lib/util/identifier.js:183–294  ·  view source on GitHub ↗
(fn)

Source from the content-addressed store, hash-verified

181 * @returns {MakeCacheableWithContextResult & { bindCache: BindCacheForContext, bindContextCache: BindContextCacheForContext }} cacheable function with context
182 */
183const makeCacheableWithContext = (fn) => {
184 /** @typedef {Map<string, Map<string, string>>} InnerCache */
185 /** @type {WeakMap<AssociatedObjectForCache, InnerCache>} */
186 const cache = new WeakMap();
187
188 /** @type {MakeCacheableWithContextResult & { bindCache: BindCacheForContext, bindContextCache: BindContextCacheForContext }} */
189 const cachedFn = (context, identifier, associatedObjectForCache) => {
190 if (!associatedObjectForCache) return fn(context, identifier);
191
192 let innerCache = cache.get(associatedObjectForCache);
193 if (innerCache === undefined) {
194 innerCache = new Map();
195 cache.set(associatedObjectForCache, innerCache);
196 }
197
198 /** @type {undefined | string} */
199 let cachedResult;
200 let innerSubCache = innerCache.get(context);
201 if (innerSubCache === undefined) {
202 innerCache.set(context, (innerSubCache = new Map()));
203 } else {
204 cachedResult = innerSubCache.get(identifier);
205 }
206
207 if (cachedResult !== undefined) {
208 return cachedResult;
209 }
210 const result = fn(context, identifier);
211 innerSubCache.set(identifier, result);
212 return result;
213 };
214
215 /** @type {BindCacheForContext} */
216 cachedFn.bindCache = (associatedObjectForCache) => {
217 /** @type {undefined | InnerCache} */
218 let innerCache;
219 if (associatedObjectForCache) {
220 innerCache = cache.get(associatedObjectForCache);
221 if (innerCache === undefined) {
222 innerCache = new Map();
223 cache.set(associatedObjectForCache, innerCache);
224 }
225 } else {
226 innerCache = new Map();
227 }
228
229 /**
230 * Returns the returned relative path.
231 * @param {string} context context used to create relative path
232 * @param {string} identifier identifier used to create relative path
233 * @returns {string} the returned relative path
234 */
235 const boundFn = (context, identifier) => {
236 /** @type {undefined | string} */
237 let cachedResult;
238 let innerSubCache = innerCache.get(context);
239 if (innerSubCache === undefined) {
240 innerCache.set(context, (innerSubCache = new Map()));

Callers 1

identifier.jsFile · 0.85

Calls 2

getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected