Skip to content

Commit da910f4

Browse files
committed
Fix #649 optimize page load by using mcdoc symbols export
1 parent cc5b79b commit da910f4

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

src/app/services/DataFetcher.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ declare var __LATEST_VERSION__: string
1212
export const latestVersion = __LATEST_VERSION__ ?? ''
1313
const mcmetaUrl = 'https://raw.githubusercontent.com/misode/mcmeta'
1414
const mcmetaTarballUrl = 'https://github.com/misode/mcmeta/tarball'
15+
const vanillaMcdocUrl = 'https://raw.githubusercontent.com/SpyglassMC/vanilla-mcdoc'
1516
const changesUrl = 'https://raw.githubusercontent.com/misode/technical-changes'
1617
const fixesUrl = 'https://raw.githubusercontent.com/misode/mcfixes'
1718
const versionDiffUrl = 'https://mcmeta-diff.misode.workers.dev'
1819
const whatsNewUrl = 'https://whats-new.misode.workers.dev'
19-
const vanillaMcdocUrl = 'https://proxy.misode.workers.dev/mcdoc'
2020

2121
type McmetaTypes = 'summary' | 'data' | 'data-json' | 'assets' | 'assets-json' | 'registries' | 'atlas'
2222

@@ -48,9 +48,14 @@ export function getVersionChecksum(versionId: VersionId) {
4848
return version.ref
4949
}
5050

51-
export async function fetchVanillaMcdoc() {
51+
export interface VanillaMcdocSymbols {
52+
ref: string,
53+
mcdoc: Record<string, unknown>,
54+
'mcdoc/dispatcher': Record<string, Record<string, unknown>>,
55+
}
56+
export async function fetchVanillaMcdoc(): Promise<VanillaMcdocSymbols> {
5257
try {
53-
return cachedFetch(vanillaMcdocUrl, { decode: res => res.arrayBuffer(), refresh: true })
58+
return cachedFetch<VanillaMcdocSymbols>(`${vanillaMcdocUrl}/generated/symbols.json`, { refresh: true })
5459
} catch (e) {
5560
throw new Error(`Error occured while fetching vanilla-mcdoc: ${message(e)}`)
5661
}

src/app/services/Spyglass.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { TextDocument } from 'vscode-languageserver-textdocument'
1212
import type { ConfigGenerator } from '../Config.js'
1313
import siteConfig from '../Config.js'
1414
import { computeIfAbsent, genPath } from '../Utils.js'
15-
import type { VersionMeta } from './DataFetcher.js'
15+
import type { VanillaMcdocSymbols, VersionMeta } from './DataFetcher.js'
1616
import { fetchBlockStates, fetchRegistries, fetchVanillaMcdoc, fetchVersions, getVersionChecksum } from './DataFetcher.js'
1717
import { IndexedDbFileSystem } from './FileSystem.js'
1818
import type { VersionId } from './Versions.js'
@@ -302,7 +302,7 @@ export class SpyglassService {
302302
defaultConfig: core.ConfigService.merge(core.VanillaConfig, {
303303
env: {
304304
gameVersion: version.dynamic ? version.id : version.ref,
305-
dependencies: ['@vanilla-mcdoc', '@misode-mcdoc'],
305+
dependencies: ['@misode-mcdoc'],
306306
customResources: {
307307
text_component: {
308308
category: 'text_component',
@@ -366,11 +366,10 @@ async function compressBall(files: [string, string][]): Promise<Uint8Array> {
366366
const initialize: core.ProjectInitializer = async (ctx) => {
367367
const { config, logger, meta, externals, cacheRoot } = ctx
368368

369-
meta.registerDependencyProvider('@vanilla-mcdoc', async () => {
370-
const uri: string = new core.Uri('downloads/vanilla-mcdoc.tar.gz', cacheRoot).toString()
371-
const buffer = await fetchVanillaMcdoc()
372-
await core.fileUtil.writeFile(externals, uri, new Uint8Array(buffer))
373-
return { info: { startDepth: 1 }, uri }
369+
const vanillaMcdoc = await fetchVanillaMcdoc()
370+
meta.registerSymbolRegistrar('vanilla-mcdoc', {
371+
checksum: vanillaMcdoc.ref,
372+
registrar: vanillaMcdocRegistrar(vanillaMcdoc),
374373
})
375374

376375
meta.registerDependencyProvider('@misode-mcdoc', async () => {
@@ -476,3 +475,31 @@ function registerAttributes(meta: core.MetaRegistry, release: ReleaseVersion, ve
476475
},
477476
})
478477
}
478+
479+
const VanillaMcdocUri = 'mcdoc://vanilla-mcdoc/symbols.json'
480+
481+
function vanillaMcdocRegistrar(vanillaMcdoc: VanillaMcdocSymbols): core.SymbolRegistrar {
482+
return (symbols) => {
483+
const start = performance.now()
484+
for (const [id, typeDef] of Object.entries(vanillaMcdoc.mcdoc)) {
485+
symbols.query(VanillaMcdocUri, 'mcdoc', id).enter({
486+
data: { data: { typeDef } },
487+
usage: { type: 'declaration' },
488+
})
489+
}
490+
for (const [dispatcher, ids] of Object.entries(vanillaMcdoc['mcdoc/dispatcher'])) {
491+
symbols.query(VanillaMcdocUri, 'mcdoc/dispatcher', dispatcher)
492+
.enter({ usage: { type: 'declaration' } })
493+
.onEach(Object.entries(ids), ([id, typeDef], query) => {
494+
query.member(id, (memberQuery) => {
495+
memberQuery.enter({
496+
data: { data: { typeDef } },
497+
usage: { type: 'declaration' },
498+
})
499+
})
500+
})
501+
}
502+
const duration = performance.now() - start
503+
console.log(`[vanillaMcdocRegistrar] Done in ${duration}ms`)
504+
}
505+
}

0 commit comments

Comments
 (0)