`declaration-data.js` changes up to `9b524d7`.

finite-set-exercises
Joshua Potter 2023-07-26 13:48:54 -06:00
parent c3b579aed1
commit da2cde9af3
1 changed files with 32 additions and 22 deletions

View File

@ -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. * 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. * Construct a DeclarationDataCenter with given data.
@ -41,26 +41,31 @@ export class DeclarationDataCenter {
* @returns {Promise<DeclarationDataCenter>} * @returns {Promise<DeclarationDataCenter>}
*/ */
static async init() { static async init() {
if (!DeclarationDataCenter.singleton) { if (DeclarationDataCenter.requestSingleton === null) {
const dataListUrl = new URL( DeclarationDataCenter.requestSingleton = DeclarationDataCenter.getData();
`${SITE_ROOT}/declarations/declaration-data.bmp`, }
window.location return await DeclarationDataCenter.requestSingleton;
); }
// try to use cache first static async getData() {
const data = await fetchCachedDeclarationData().catch(_e => null); const dataListUrl = new URL(
if (data) { `${SITE_ROOT}/declarations/declaration-data.bmp`,
// if data is defined, use the cached one. window.location
DeclarationDataCenter.singleton = new DeclarationDataCenter(data); );
} else {
// undefined. then fetch the data from the server. // try to use cache first
const dataListRes = await fetch(dataListUrl); const data = await fetchCachedDeclarationData().catch(_e => null);
const data = await dataListRes.json(); if (data) {
await cacheDeclarationData(data); // if data is defined, use the cached one.
DeclarationDataCenter.singleton = new DeclarationDataCenter(data); 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) => { return new Promise((resolve, reject) => {
let transactionRequest = store.get(CACHE_DB_KEY); let transactionRequest = store.get(CACHE_DB_KEY);
transactionRequest.onsuccess = function (event) { 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) { transactionRequest.onerror = function (event) {
reject(new Error(`fail to store declaration data`)); reject(new Error(`fail to store declaration data`));
}; };
}); });
} }