From 41004ae41cb9d8b881d7bc5b60ccb222f61bf145 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Fri, 14 Oct 2022 22:07:22 +0200 Subject: [PATCH] Use async-await syntax in runCached --- src/cache.js | 112 ++++++++++++++++----------------------------------- 1 file changed, 34 insertions(+), 78 deletions(-) diff --git a/src/cache.js b/src/cache.js index 355f8fe1..08598e14 100644 --- a/src/cache.js +++ b/src/cache.js @@ -29,9 +29,9 @@ function asPromise(func) { }); } -/** @type {WeakMap>} */ +/** @type {WeakMap} */ const snapshots = new WeakMap(); -/** @type {WeakMap, Promise>} */ +/** @type {WeakMap} */ const faviconCache = new WeakMap(); /** @@ -50,7 +50,7 @@ const faviconCache = new WeakMap(); * * @returns {Promise} */ -function runCached( +async function runCached( absoluteFilePaths, pluginInstance, useWebpackCache, @@ -61,46 +61,44 @@ function runCached( ) { const latestSnapShot = snapshots.get(pluginInstance); - /** @type {Promise | undefined} */ - const cachedFavicons = latestSnapShot && faviconCache.get(latestSnapShot); - - if (latestSnapShot && cachedFavicons) { - return isSnapShotValid(latestSnapShot, compilation).then((isValid) => { - // If the source files have changed clear all caches - // and try again - if (!isValid) { - faviconCache.delete(latestSnapShot); - - return runCached( - absoluteFilePaths, - pluginInstance, - useWebpackCache, - compilation, - eTags, - idGenerator, - generator - ); + if (latestSnapShot) { + const cachedFavicons = faviconCache.get(latestSnapShot); + if (cachedFavicons) { + const isValid = await asPromise((cb) => + compilation.fileSystemInfo.checkSnapshotValid(latestSnapShot, cb) + ); + + if (isValid) { + // If the cache is valid return the result directly from cache + return cachedFavicons; } - // If the cache is valid return the result directly from cache - return cachedFavicons; - }); + // If the source files have changed clear all caches and try again + faviconCache.delete(latestSnapShot); + } } // Store a snapshot of the filesystem // to find out if the logo was changed - const newSnapShot = createSnapshot( - { - fileDependencies: absoluteFilePaths.filter(Boolean), - contextDependencies: [], - missingDependencies: [], - }, - compilation + const newSnapShot = await asPromise((cb) => + compilation.fileSystemInfo.createSnapshot( + new Date().getTime(), + absoluteFilePaths.filter(Boolean), + [], + [], + {}, + cb + ) ); + + if (!newSnapShot) { + throw new Error('Could not create Snapshot'); + } + snapshots.set(pluginInstance, newSnapShot); // Start generating the favicons - const faviconsGenerationsPromise = useWebpackCache + const faviconsGenerations = await (useWebpackCache ? runWithFileCache( absoluteFilePaths, compilation, @@ -110,37 +108,12 @@ function runCached( ) : readFiles(absoluteFilePaths, compilation).then((fileContents) => generator(fileContents, idGenerator(fileContents)) - ); + )); // Store the promise of the favicon compilation in cache - faviconCache.set(newSnapShot, faviconsGenerationsPromise); + faviconCache.set(newSnapShot, faviconsGenerations); - return faviconsGenerationsPromise; -} - -/** - * Create a snapshot - * @param {{fileDependencies: string[], contextDependencies: string[], missingDependencies: string[]}} fileDependencies - * @param {WebpackCompilation} mainCompilation - * @returns {Promise} - */ -async function createSnapshot(fileDependencies, mainCompilation) { - const snapshot = await asPromise((cb) => - mainCompilation.fileSystemInfo.createSnapshot( - new Date().getTime(), - fileDependencies.fileDependencies, - fileDependencies.contextDependencies, - fileDependencies.missingDependencies, - {}, - cb - ) - ); - - if (!snapshot) { - throw new Error('Could not create Snapshot'); - } - - return snapshot; + return faviconsGenerations; } /** @@ -208,21 +181,4 @@ function readFiles(absoluteFilePaths, compilation) { ); } -/** - * Returns true if the files inside this snapshot - * have not been changed - * - * @param {Promise} snapshotPromise - * @param {WebpackCompilation} mainCompilation - * @returns {Promise} - */ -async function isSnapShotValid(snapshotPromise, mainCompilation) { - const snapshot = await snapshotPromise; - const isValid = await asPromise((cb) => - mainCompilation.fileSystemInfo.checkSnapshotValid(snapshot, cb) - ); - - return Boolean(isValid); -} - module.exports = { runCached };