Skip to content

Commit

Permalink
pref: load js and wasm in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
arianrhodsandlot committed Jan 7, 2024
1 parent ce4aed6 commit cef0ca8
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/nostalgist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,21 +494,37 @@ export class Nostalgist {

private async getCoreOption() {
const { core, resolveCoreJs, resolveCoreWasm } = this.options
const coreDict =
typeof core === 'string'
? { name: core, js: await resolveCoreJs(core, this.options), wasm: await resolveCoreWasm(core, this.options) }
: core

let coreDict
if (typeof core === 'string') {
const [js, wasm] = await Promise.all([resolveCoreJs(core, this.options), resolveCoreWasm(core, this.options)])
coreDict = { name: core, js, wasm }
} else {
coreDict = core
}
let { name, js, wasm } = coreDict

const promises = []
if (typeof js === 'string') {
const response = await fetch(js)
js = await response.text()
promises.push(
(async () => {
const response = await fetch(js)
js = await response.text()
})(),
)
}
if (typeof wasm === 'string') {
const response = await fetch(wasm)
wasm = await response.arrayBuffer()
promises.push(
(async () => {
const response = await fetch(wasm as string)
wasm = await response.arrayBuffer()
})(),
)
}
if (promises.length > 0) {
await Promise.all(promises)
}
return { name, js, wasm }

return { name, js, wasm: wasm as ArrayBuffer }
}

private async resolveFile(file: NostalgistOptionsFile, resolveFunction: NostalgistResolveFileFunction) {
Expand Down

0 comments on commit cef0ca8

Please sign in to comment.