diff --git a/ui/sources/services/core.js b/ui/sources/services/core.js index aa0b4c7..af4360f 100644 --- a/ui/sources/services/core.js +++ b/ui/sources/services/core.js @@ -34,6 +34,9 @@ export default class Core { /** @type {Graphics} */ #graphics = null; + /** @type {(variables: Variable[]) => void} */ + #on_variables = null; + /** * @param {string} name * @param {WebAssembly.Memory} memory @@ -55,23 +58,17 @@ export default class Core { Core.#running = new Promise(resolve => Core.#stop = resolve); this.#graphics = new Graphics(canvas); + this.#on_variables = on_variables const origin = location.origin + location.pathname.substring(0, location.pathname.lastIndexOf('/')); const config = { core: this.#name, system, rom, origin, memory: this.#memory }; const script = await (await fetch('worker.js')).text(); const handler = async message => { - switch (message.data.type) { - case 'thread': - const thread = new Parallel(Interop, false, handler); - const core = await thread.create(this.#name, script); - await core.init(Parallel.instrument(this), await Files.clone(), { ...config, ...message.data }); - this.#threads.push(thread); - break; - case 'variables': - on_variables(message.data.variables); - break; - } + const thread = new Parallel(Interop, false, handler); + const core = await thread.create(this.#name, script); + await core.init(Parallel.instrument(this), await Files.clone(), { ...config, ...message.data }); + this.#threads.push(thread); } this.#parallel = new Parallel(Interop, false, handler); @@ -126,13 +123,21 @@ export default class Core { /** * @param {Audio} audio - * @returns + * @returns {void} */ play(audio) { const audio_view = new Float32Array(this.#memory.buffer, audio.data, audio.frames * 2); AudioPlayer.queue(audio_view.slice(), audio.rate); } + /** + * @param {Variable[]} variables + * @returns {void} + */ + variables(variables) { + this.#on_variables(variables); + } + /** @param {Settings} settings @returns {Promise} */ async settings(settings) { await this.#interop?.variables(settings.variables); } diff --git a/ui/sources/services/interop.js b/ui/sources/services/interop.js index 4dbf9f1..2276d5b 100644 --- a/ui/sources/services/interop.js +++ b/ui/sources/services/interop.js @@ -127,23 +127,18 @@ export default class Interop { const junie_interop_video = (video_c) => { const video = Video.parse(config.memory, video_c); - if (!video.data) - return; - - this.#core.draw(video); + if (video.data) + this.#core.draw(video); }; const junie_interop_audio = (audio_c) => { const audio = Audio.parse(config.memory, audio_c); - if (!audio.frames) - return; - - this.#core.play(audio); + if (audio.frames) + this.#core.play(audio); }; const junie_interop_variables = (variables_c) => { - const variables = Variable.parse(this.#instance, variables_c); - postMessage({ type: 'variables', variables }); + this.#core.variables(Variable.parse(this.#instance, variables_c)); } const source = await WebAssembly.instantiateStreaming(fetch(`${config.origin}/modules/${config.core}.wasm`), { @@ -151,7 +146,7 @@ export default class Interop { wasi_snapshot_preview1: this.#wasi.environment, wasi: { 'thread-spawn': (start_arg) => { const id = filesystem.id(); - postMessage({ type: 'thread', id, fds: this.#wasi.fds, start_arg }); + postMessage({ id, fds: this.#wasi.fds, start_arg }); return id; }}, });