diff --git a/src/app/Schemas.ts b/src/app/Schemas.ts index e51519a4..ac21d8ba 100644 --- a/src/app/Schemas.ts +++ b/src/app/Schemas.ts @@ -25,7 +25,7 @@ type VersionData = { schemas: SchemaRegistry, blockStates: BlockStateRegistry, } -const Versions: Record = {} +const Versions: Record> = {} type ModelData = { model: DataModel, @@ -46,15 +46,19 @@ const versionGetter: { async function getVersion(id: VersionId): Promise { if (!Versions[id]) { - try { - const collections = versionGetter[id].getCollections() - const blockStates: BlockStateRegistry = {} - await fetchData(id, collections, blockStates) - const schemas = versionGetter[id].getSchemas(collections) - Versions[id] = { collections, schemas, blockStates } - } catch (e) { - throw new Error(`Cannot get version "${id}": ${e.message}`) - } + Versions[id] = (async () => { + try { + const collections = versionGetter[id].getCollections() + const blockStates: BlockStateRegistry = {} + await fetchData(id, collections, blockStates) + const schemas = versionGetter[id].getSchemas(collections) + Versions[id] = { collections, schemas, blockStates } + return Versions[id] + } catch (e) { + throw new Error(`Cannot get version "${id}": ${e.message}`) + } + })() + return Versions[id] } return Versions[id] } diff --git a/src/app/Utils.ts b/src/app/Utils.ts index db051abe..5732eca8 100644 --- a/src/app/Utils.ts +++ b/src/app/Utils.ts @@ -1,3 +1,7 @@ +export function isPromise(obj: any): obj is Promise { + return typeof (obj as any)?.then === 'function' +} + const dec2hex = (dec: number) => ('0' + dec.toString(16)).substr(-2) export function hexId(length = 12) {