From da2cde9af35ed684d6c316ec2417b741026dc8b9 Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Wed, 26 Jul 2023 13:48:54 -0600 Subject: [PATCH] `declaration-data.js` changes up to `9b524d7`. --- static/declaration-data.js | 54 ++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/static/declaration-data.js b/static/declaration-data.js index ae34585..291e09c 100644 --- a/static/declaration-data.js +++ b/static/declaration-data.js @@ -24,7 +24,7 @@ export class DeclarationDataCenter { /** * Used to implement the singleton, in case we need to fetch data mutiple times in the same page. */ - static singleton = null; + static requestSingleton = null; /** * Construct a DeclarationDataCenter with given data. @@ -41,26 +41,31 @@ export class DeclarationDataCenter { * @returns {Promise} */ static async init() { - if (!DeclarationDataCenter.singleton) { - const dataListUrl = new URL( - `${SITE_ROOT}/declarations/declaration-data.bmp`, - window.location - ); - - // try to use cache first - const data = await fetchCachedDeclarationData().catch(_e => null); - if (data) { - // if data is defined, use the cached one. - DeclarationDataCenter.singleton = new DeclarationDataCenter(data); - } else { - // undefined. then fetch the data from the server. - const dataListRes = await fetch(dataListUrl); - const data = await dataListRes.json(); - await cacheDeclarationData(data); - DeclarationDataCenter.singleton = new DeclarationDataCenter(data); - } + if (DeclarationDataCenter.requestSingleton === null) { + DeclarationDataCenter.requestSingleton = DeclarationDataCenter.getData(); + } + return await DeclarationDataCenter.requestSingleton; + } + + static async getData() { + const dataListUrl = new URL( + `${SITE_ROOT}/declarations/declaration-data.bmp`, + window.location + ); + + // try to use cache first + const data = await fetchCachedDeclarationData().catch(_e => null); + if (data) { + // if data is defined, use the cached one. + return new DeclarationDataCenter(data); + } else { + // undefined. then fetch the data from the server. + const dataListRes = await fetch(dataListUrl); + const data = await dataListRes.json(); + // TODO https://github.com/leanprover/doc-gen4/issues/133 + // await cacheDeclarationData(data); + return new DeclarationDataCenter(data); } - return DeclarationDataCenter.singleton; } /** @@ -268,10 +273,15 @@ async function fetchCachedDeclarationData() { return new Promise((resolve, reject) => { let transactionRequest = store.get(CACHE_DB_KEY); transactionRequest.onsuccess = function (event) { - resolve(event.result); + // TODO: This API is not thought 100% through. If we have a DB cached + // already it will not even ask the remote for a new one so we end up + // with outdated declaration-data. This has to have some form of cache + // invalidation: https://github.com/leanprover/doc-gen4/issues/133 + // resolve(event.target.result); + resolve(undefined); }; transactionRequest.onerror = function (event) { reject(new Error(`fail to store declaration data`)); }; }); -} +} \ No newline at end of file