Skip to content
Snippets Groups Projects
Commit 4e0367a3 authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

Add an async/await variant for JSONLD.getInstance()

In theory this can replace all other API.
parent 378db982
No related branches found
No related tags found
No related merge requests found
......@@ -16,6 +16,23 @@ export default class JSONLD {
this.idToEntityNameMatchList = idToEntityNameMatchList;
}
static async getInstance(apiUrl, lang = 'de') {
let promise = JSONLD.promises[apiUrl];
if (promise === undefined) {
JSONLD.doInitializationOnce(apiUrl);
promise = new Promise((resolve, reject) => {
JSONLD.initialize(
apiUrl,
(instance) => resolve(instance),
(error) => reject(error),
lang
);
});
JSONLD.promises[apiUrl] = promise;
}
return promise;
}
static initialize(apiUrl, successFnc, failureFnc, lang = 'de') {
if (lang !== 'de') {
i18n.changeLanguage(lang);
......@@ -51,11 +68,11 @@ export default class JSONLD {
}
JSONLD.initStarted[apiUrl] = true;
JSONLD.doInitialization(apiUrl);
JSONLD._doInitialization(apiUrl);
// console.log("doInitializationOnce Done", apiUrl);
}
static doInitialization(apiUrl) {
static _doInitialization(apiUrl) {
const xhr = new XMLHttpRequest();
xhr.open("GET", apiUrl, true);
......@@ -156,7 +173,11 @@ export default class JSONLD {
*/
static executeFailureFunctions(apiUrl, message = "") {
if (JSONLD.failureFunctions[apiUrl] !== undefined) {
for (const fnc of JSONLD.failureFunctions[apiUrl]) if (typeof fnc == 'function') fnc();
for (const fnc of JSONLD.failureFunctions[apiUrl]) {
if (typeof fnc == 'function') {
fnc(new Error(message));
}
}
}
JSONLD.failureFunctions[apiUrl] = [];
......@@ -169,10 +190,6 @@ export default class JSONLD {
}
}
static getInstance(apiUrl) {
return JSONLD.instances[apiUrl];
}
getEntityForIdentifier(identifier) {
let entityName = this.getEntityNameForIdentifier(identifier);
return this.getEntityForEntityName(entityName);
......@@ -286,3 +303,4 @@ JSONLD.instances = {};
JSONLD.successFunctions = {};
JSONLD.failureFunctions = {};
JSONLD.initStarted = {};
JSONLD.promises = {};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment