Macro flutter_rust_bridge::spawn
source · macro_rules! spawn { ($($tt:tt)*) => { ... }; }
Expand description
Spawn a task using the internal thread pool. Interprets the parameters as a list of captured transferables to send to this thread.
Also see transfer
.
Example:
let (tx, rx) = std::sync::mpsc::channel();
flutter_rust_bridge::spawn!(|| {
tx.send(true).unwrap();
});
assert_eq!(rx.recv(), Ok(true));
Sending a JS transferable:
use web_sys::{MessagePort, MessageEvent};
use wasm_bindgen::prelude::*;
let channel = web_sys::MessageChannel::new().unwrap();
let onmessage = Closure::new(move |event: MessageEvent| {
assert!(event.data() == true);
});
channel.port1().set_onmessage(Some(onmessage.as_ref().unchecked_ref()));
onmessage.forget();
let port2 = channel.port2();
// Declare the transferable with the same name and type
flutter_rust_bridge::spawn!(|port2: MessagePort| {
port2.post_message(&JsValue::from(true)).unwrap();
});