Skip to content

Commit 2c60d01

Browse files
committed
Only load glkaudio if it's actually needed
1 parent 8b028d5 commit 2c60d01

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

src/glkaudio/build.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#!/bin/bash
22

33
npx wasm-pack build --target web
4-
rm pkg/.gitignore
4+
rm pkg/.gitignore
5+
6+
# Inject an export so that we can synchronously check if we've already initiated the module
7+
echo "export {wasm}" >> pkg/glkaudio.js
8+
echo "export let wasm: InitOutput | undefined" >> pkg/glkaudio.d.ts

src/glkaudio/pkg/glkaudio.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ export function initSync(module: { module: SyncInitInput } | SyncInitInput): Ini
3232
* @returns {Promise<InitOutput>}
3333
*/
3434
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
35+
export let wasm: InitOutput | undefined

src/glkaudio/pkg/glkaudio.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,4 @@ async function __wbg_init(module_or_path) {
159159

160160
export { initSync };
161161
export default __wbg_init;
162+
export {wasm}

src/glkote/web/schannels.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ import {fetch_resource} from '../../common/file/browser.js'
1313
import * as protocol from '../../common/protocol.js'
1414
import WebGlkOte from './web.js'
1515

16-
import GlkAudio_init, {decode as GlkAudio_decode} from 'glkaudio'
16+
import GlkAudio_init, {decode as GlkAudio_decode, wasm as GlkAudio_is_ready} from 'glkaudio'
1717

1818
export class SoundChannelManager extends Map<number, SoundChannel> {
1919
private context: AudioContext
2020
private glkote: WebGlkOte
21-
private loaded = false
2221

2322
constructor(glkote: WebGlkOte) {
2423
super()
@@ -39,12 +38,6 @@ export class SoundChannelManager extends Map<number, SoundChannel> {
3938

4039
// Do operations
4140
if (ops) {
42-
// Load the glkaudio library only when we actually have something to do
43-
// We still might be loading it unnecessarily, but it's not very big
44-
if (!this.loaded) {
45-
await GlkAudio_init({module_or_path: fetch_resource(this.glkote.options, 'glkaudio_bg.wasm')})
46-
this.loaded = true
47-
}
4841
this.get(id)!.do_ops(ops)
4942
}
5043
}
@@ -120,6 +113,10 @@ export class SoundChannel {
120113
this.buffer = await context.decodeAudioData(chunk.content!.slice().buffer)
121114
}
122115
catch {
116+
// Load the glkaudio library if it hasn't been yet
117+
if (!GlkAudio_is_ready) {
118+
await GlkAudio_init({module_or_path: fetch_resource(this.glkote.options, 'glkaudio_bg.wasm')})
119+
}
123120
const decoded = GlkAudio_decode(chunk.content!)
124121
this.buffer = await context.decodeAudioData(decoded.buffer)
125122
}

0 commit comments

Comments
 (0)