WIP: experiment with passing data between Rust wasm modules

This commit is contained in:
Rob Watson 2020-09-17 15:34:38 +02:00
parent a30ca384b0
commit 5723985f6a
6 changed files with 38 additions and 10 deletions

View File

@ -28,5 +28,6 @@ features = [
"HtmlCanvasElement",
"CanvasRenderingContext2d",
"Window",
"File",
]

View File

@ -1,10 +1,18 @@
use super::canvas::Canvas;
use crate::agents::audio_agent::{self, AudioAgent};
use crate::components::Player;
use web_sys::HtmlInputElement;
use web_sys::{File, HtmlInputElement};
use yew::agent::Dispatcher;
use yew::prelude::*;
use yew::services::ConsoleService;
use wasm_bindgen::prelude::*;
use weblog::*;
#[wasm_bindgen(module = "/static/player.js")]
extern "C" {
fn get_val() -> i32;
fn load_file(file: &str) -> i32;
}
pub struct Container {
link: ComponentLink<Self>,
@ -88,8 +96,9 @@ impl Container {
let file_list = file_input.files().ok_or("could not get file list")?;
let file = file_list.get(0).ok_or("could not get file")?;
self.audio_agent
.send(audio_agent::Request::LoadSamplesFromFile(file));
load_file("oh!");
console_log!("Done.");
Ok(())
}

View File

@ -12,17 +12,10 @@ mod agents;
mod components;
mod utils;
#[wasm_bindgen(module = "/static/player.js")]
extern "C" {
fn get_val() -> i32;
}
#[wasm_bindgen(start)]
pub fn run_app() {
#[cfg(debug_assertions)]
panic::set_hook(Box::new(console_error_panic_hook::hook));
console_log!("Get value from player module:", get_val());
App::<components::App>::new().mount_to_body();
}

View File

@ -27,4 +27,8 @@ export function get_val() {
return wasmModule.get_val();
}
export function load_file(f) {
return wasmModule.load_file(f);
}
init();

View File

@ -10,3 +10,9 @@ crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = { version = "0.2" }
lazy_static = "1.4.0"
weblog = "0.2"
[dependencies.web-sys]
version = "0.3.44"
features = ["File"]

View File

@ -1,6 +1,21 @@
#[macro_use]
extern crate lazy_static;
use wasm_bindgen::prelude::*;
use web_sys::File;
use weblog::*;
lazy_static! {
static ref BUFFER: Vec<f32> = vec![];
}
#[wasm_bindgen]
pub fn get_val() -> i32 {
123
}
#[wasm_bindgen]
pub fn load_file(file_element_id: &str) -> i32 {
console_log!("Got file:", file_element_id);
0
}