Consider:
use std::{cell::RefCell, rc::Rc};
use wasm_bindgen::{closure::Closure, JsCast};
use web_sys::{DedicatedWorkerGlobalScope, MessageEvent};
pub async fn main() {
let mut g: DedicatedWorkerGlobalScope = js_sys::global()
.dyn_into::<web_sys::DedicatedWorkerGlobalScope>()
.unwrap();
let (s, r) = async_channel::unbounded();
web_sys::console::log_1(&"Rust: setting up handler".into());
let onmessage_callback = Closure::wrap(Box::new(move |ev: MessageEvent| {
web_sys::console::log_1(&"Rust: on_message".into());
s.send(ev).await;})
as Box<dyn FnMut(MessageEvent)>);
g.set_onmessage(Some(onmessage_callback.as_ref().unchecked_ref()));
onmessage_callback.forget();
g.post_message(&"ready".into());
web_sys::console::log_1(&"Rust: main".into());}
-
This code here does not compile because I am using async in the event handler, which takes regular functions.
-
An alternative is to not use an async channel, to us a regular channel -- but then I run into the problem in the main loop, if there is nothing available, I need the code to busy wait.
I am looking for the following two properties:
-
in the main loop, if there is an item, we process it; if there is not an item, we async block and get resumed when an item is pushed
-
we can somehow route messages from the message handler to this channel