Skip to content

Commit

Permalink
Only load glkaudio if it's actually needed
Browse files Browse the repository at this point in the history
  • Loading branch information
curiousdannii committed Dec 13, 2024
1 parent 8b028d5 commit 2c60d01
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/glkaudio/build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/bin/bash

npx wasm-pack build --target web
rm pkg/.gitignore
rm pkg/.gitignore

# Inject an export so that we can synchronously check if we've already initiated the module
echo "export {wasm}" >> pkg/glkaudio.js
echo "export let wasm: InitOutput | undefined" >> pkg/glkaudio.d.ts
1 change: 1 addition & 0 deletions src/glkaudio/pkg/glkaudio.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ export function initSync(module: { module: SyncInitInput } | SyncInitInput): Ini
* @returns {Promise<InitOutput>}
*/
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
export let wasm: InitOutput | undefined
1 change: 1 addition & 0 deletions src/glkaudio/pkg/glkaudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,4 @@ async function __wbg_init(module_or_path) {

export { initSync };
export default __wbg_init;
export {wasm}
13 changes: 5 additions & 8 deletions src/glkote/web/schannels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ import {fetch_resource} from '../../common/file/browser.js'
import * as protocol from '../../common/protocol.js'
import WebGlkOte from './web.js'

import GlkAudio_init, {decode as GlkAudio_decode} from 'glkaudio'
import GlkAudio_init, {decode as GlkAudio_decode, wasm as GlkAudio_is_ready} from 'glkaudio'

export class SoundChannelManager extends Map<number, SoundChannel> {
private context: AudioContext
private glkote: WebGlkOte
private loaded = false

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

// Do operations
if (ops) {
// Load the glkaudio library only when we actually have something to do
// We still might be loading it unnecessarily, but it's not very big
if (!this.loaded) {
await GlkAudio_init({module_or_path: fetch_resource(this.glkote.options, 'glkaudio_bg.wasm')})
this.loaded = true
}
this.get(id)!.do_ops(ops)
}
}
Expand Down Expand Up @@ -120,6 +113,10 @@ export class SoundChannel {
this.buffer = await context.decodeAudioData(chunk.content!.slice().buffer)
}
catch {
// Load the glkaudio library if it hasn't been yet
if (!GlkAudio_is_ready) {
await GlkAudio_init({module_or_path: fetch_resource(this.glkote.options, 'glkaudio_bg.wasm')})
}
const decoded = GlkAudio_decode(chunk.content!)
this.buffer = await context.decodeAudioData(decoded.buffer)
}
Expand Down

0 comments on commit 2c60d01

Please sign in to comment.