On x86_64, to start a read, I just do std::thread::spawn(|| { ... }).
Question: On rust/wasm32/Chrome, is there a crate that provides similar functionality? The way it would work under the hood would be to start a new worker, then load the wasm32 in that web worker, and provide some type of mspc channel for sending msgs back & forth.
#[wasm_bindgen]
pub fn startup() {
// Here, we create our worker. In a larger app, multiple callbacks should be
// able to interact with the code in the worker. Therefore, we wrap it in
// `Rc<RefCell>` following the interior mutability pattern. Here, it would
// not be needed but we include the wrapping anyway as example.
let worker_handle = Rc::new(RefCell::new(Worker::new("./worker.js").unwrap()));
console::log_1(&"Created a new worker from within WASM".into());
// Pass the worker to the function which sets up the `oninput` callback.
setup_input_oninput_callback(worker_handle.clone());
}
This here, Worker::new looks like the std::thread::spawn right? It is calling worker.js, which goes back to JS land to setup a web worker then reload the wasm in that web worker.
Yeah, calling Worker::new() will spin off a web worker that executes a JavaScript script and gives you an object you can use to send messages to/from the worker.
I don't believe web workers let you execute WebAssemly modules directly so you'll need to write a glue script in JavaScript that imports your WebAssembly.