Only start getting schemas once on page load

This commit is contained in:
Misode
2021-06-25 03:01:27 +02:00
parent 373698ebbc
commit f2df089150
2 changed files with 18 additions and 10 deletions

View File

@@ -25,7 +25,7 @@ type VersionData = {
schemas: SchemaRegistry,
blockStates: BlockStateRegistry,
}
const Versions: Record<string, VersionData> = {}
const Versions: Record<string, VersionData | Promise<VersionData>> = {}
type ModelData = {
model: DataModel,
@@ -46,15 +46,19 @@ const versionGetter: {
async function getVersion(id: VersionId): Promise<VersionData> {
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]
}

View File

@@ -1,3 +1,7 @@
export function isPromise(obj: any): obj is Promise<any> {
return typeof (obj as any)?.then === 'function'
}
const dec2hex = (dec: number) => ('0' + dec.toString(16)).substr(-2)
export function hexId(length = 12) {