Fix #649 optimize page load by using mcdoc symbols export

This commit is contained in:
Misode
2024-12-13 22:00:00 +01:00
parent cc5b79b4e5
commit da910f42b4
2 changed files with 42 additions and 10 deletions

View File

@@ -12,11 +12,11 @@ declare var __LATEST_VERSION__: string
export const latestVersion = __LATEST_VERSION__ ?? ''
const mcmetaUrl = 'https://raw.githubusercontent.com/misode/mcmeta'
const mcmetaTarballUrl = 'https://github.com/misode/mcmeta/tarball'
const vanillaMcdocUrl = 'https://raw.githubusercontent.com/SpyglassMC/vanilla-mcdoc'
const changesUrl = 'https://raw.githubusercontent.com/misode/technical-changes'
const fixesUrl = 'https://raw.githubusercontent.com/misode/mcfixes'
const versionDiffUrl = 'https://mcmeta-diff.misode.workers.dev'
const whatsNewUrl = 'https://whats-new.misode.workers.dev'
const vanillaMcdocUrl = 'https://proxy.misode.workers.dev/mcdoc'
type McmetaTypes = 'summary' | 'data' | 'data-json' | 'assets' | 'assets-json' | 'registries' | 'atlas'
@@ -48,9 +48,14 @@ export function getVersionChecksum(versionId: VersionId) {
return version.ref
}
export async function fetchVanillaMcdoc() {
export interface VanillaMcdocSymbols {
ref: string,
mcdoc: Record<string, unknown>,
'mcdoc/dispatcher': Record<string, Record<string, unknown>>,
}
export async function fetchVanillaMcdoc(): Promise<VanillaMcdocSymbols> {
try {
return cachedFetch(vanillaMcdocUrl, { decode: res => res.arrayBuffer(), refresh: true })
return cachedFetch<VanillaMcdocSymbols>(`${vanillaMcdocUrl}/generated/symbols.json`, { refresh: true })
} catch (e) {
throw new Error(`Error occured while fetching vanilla-mcdoc: ${message(e)}`)
}

View File

@@ -12,7 +12,7 @@ import { TextDocument } from 'vscode-languageserver-textdocument'
import type { ConfigGenerator } from '../Config.js'
import siteConfig from '../Config.js'
import { computeIfAbsent, genPath } from '../Utils.js'
import type { VersionMeta } from './DataFetcher.js'
import type { VanillaMcdocSymbols, VersionMeta } from './DataFetcher.js'
import { fetchBlockStates, fetchRegistries, fetchVanillaMcdoc, fetchVersions, getVersionChecksum } from './DataFetcher.js'
import { IndexedDbFileSystem } from './FileSystem.js'
import type { VersionId } from './Versions.js'
@@ -302,7 +302,7 @@ export class SpyglassService {
defaultConfig: core.ConfigService.merge(core.VanillaConfig, {
env: {
gameVersion: version.dynamic ? version.id : version.ref,
dependencies: ['@vanilla-mcdoc', '@misode-mcdoc'],
dependencies: ['@misode-mcdoc'],
customResources: {
text_component: {
category: 'text_component',
@@ -366,11 +366,10 @@ async function compressBall(files: [string, string][]): Promise<Uint8Array> {
const initialize: core.ProjectInitializer = async (ctx) => {
const { config, logger, meta, externals, cacheRoot } = ctx
meta.registerDependencyProvider('@vanilla-mcdoc', async () => {
const uri: string = new core.Uri('downloads/vanilla-mcdoc.tar.gz', cacheRoot).toString()
const buffer = await fetchVanillaMcdoc()
await core.fileUtil.writeFile(externals, uri, new Uint8Array(buffer))
return { info: { startDepth: 1 }, uri }
const vanillaMcdoc = await fetchVanillaMcdoc()
meta.registerSymbolRegistrar('vanilla-mcdoc', {
checksum: vanillaMcdoc.ref,
registrar: vanillaMcdocRegistrar(vanillaMcdoc),
})
meta.registerDependencyProvider('@misode-mcdoc', async () => {
@@ -476,3 +475,31 @@ function registerAttributes(meta: core.MetaRegistry, release: ReleaseVersion, ve
},
})
}
const VanillaMcdocUri = 'mcdoc://vanilla-mcdoc/symbols.json'
function vanillaMcdocRegistrar(vanillaMcdoc: VanillaMcdocSymbols): core.SymbolRegistrar {
return (symbols) => {
const start = performance.now()
for (const [id, typeDef] of Object.entries(vanillaMcdoc.mcdoc)) {
symbols.query(VanillaMcdocUri, 'mcdoc', id).enter({
data: { data: { typeDef } },
usage: { type: 'declaration' },
})
}
for (const [dispatcher, ids] of Object.entries(vanillaMcdoc['mcdoc/dispatcher'])) {
symbols.query(VanillaMcdocUri, 'mcdoc/dispatcher', dispatcher)
.enter({ usage: { type: 'declaration' } })
.onEach(Object.entries(ids), ([id, typeDef], query) => {
query.member(id, (memberQuery) => {
memberQuery.enter({
data: { data: { typeDef } },
usage: { type: 'declaration' },
})
})
})
}
const duration = performance.now() - start
console.log(`[vanillaMcdocRegistrar] Done in ${duration}ms`)
}
}