From f2df089150e6eef9b112ac698cda9145a51e1665 Mon Sep 17 00:00:00 2001 From: Misode Date: Fri, 25 Jun 2021 03:01:27 +0200 Subject: [PATCH] Only start getting schemas once on page load --- src/app/Schemas.ts | 24 ++++++++++++++---------- src/app/Utils.ts | 4 ++++ 2 files changed, 18 insertions(+), 10 deletions(-) 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) {