fix: race condition in declaration-data.js

main
Henrik 2023-07-20 22:42:49 +02:00
parent 9af4c720f0
commit 5ce54e8e10
1 changed files with 25 additions and 17 deletions

View File

@ -25,6 +25,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 singleton = null;
static requestSingleton = null;
/** /**
* Construct a DeclarationDataCenter with given data. * Construct a DeclarationDataCenter with given data.
@ -41,7 +42,16 @@ export class DeclarationDataCenter {
* @returns {Promise<DeclarationDataCenter>} * @returns {Promise<DeclarationDataCenter>}
*/ */
static async init() { static async init() {
if (!DeclarationDataCenter.singleton) { if (DeclarationDataCenter.singleton === null) {
if (DeclarationDataCenter.requestSingleton === null) {
DeclarationDataCenter.requestSingleton = DeclarationDataCenter.getData();
}
await DeclarationDataCenter.requestSingleton;
}
return DeclarationDataCenter.singleton;
}
static async getData() {
const dataListUrl = new URL( const dataListUrl = new URL(
`${SITE_ROOT}/declarations/declaration-data.bmp`, `${SITE_ROOT}/declarations/declaration-data.bmp`,
window.location window.location
@ -60,8 +70,6 @@ export class DeclarationDataCenter {
DeclarationDataCenter.singleton = new DeclarationDataCenter(data); DeclarationDataCenter.singleton = new DeclarationDataCenter(data);
} }
} }
return DeclarationDataCenter.singleton;
}
/** /**
* Search for a declaration. * Search for a declaration.