Replies: 4 comments 3 replies
-
I guess streaming from dart to rust is more like a grammar sugar, instead of something deep. Streaming from rust to dart is indeed simple: Rust just throw (possibly multiple) data to dart side - nothing more.
It is mainly 2 parts:
|
Beta Was this translation helpful? Give feedback.
-
Thanks for the fast reply :)
Yeah, in native apps this is the easiest way and I can use Let's say that we are running the web app like this:
If we call something like |
Beta Was this translation helpful? Give feedback.
-
But it seems that when web worker 1 is occupied with a Rust loop, JS glue won't work either... so it's complicated 🥲 |
Beta Was this translation helpful? Give feedback.
-
A bit of boilerplate is needed, but I did manage to verify it works with WASM. It needs the pub struct Sender {
pub inner: RustOpaque<std::sync::mpsc::Sender<i32>>,
}
impl Sender {
pub fn send(&self, value: i32) {
self.inner.send(value).expect("Failed to send value");
}
}
pub struct Receiver {
pub inner: RustOpaque<std::sync::mpsc::Receiver<i32>>,
}
impl Receiver {
pub fn recv(&self) -> anyhow::Result<i32> {
self.inner
.recv()
.map_err(|err| anyhow::anyhow!("Failed to receive value: {err}"))
}
}
// Only for demonstration purposes
unsafe impl Send for Sender {}
unsafe impl Send for Receiver {}
pub struct MyChannel {
pub sender: Sender,
pub receiver: Receiver,
}
impl MyChannel {
pub fn new() -> MyChannel {
let (tx, rx) = std::sync::mpsc::channel();
MyChannel {
sender: Sender {
inner: RustOpaque::new(tx),
},
receiver: Receiver {
inner: RustOpaque::new(rx),
},
}
}
}
// An unclosed sender will hang up the receiver
pub fn close_sender(sender: Sender) {} To close the sender, one needs to set the inner opaque to be moved, resulting in this syntax: MyChannel channel;
api.closeSender(sender: channel.sender..inner.move = true); |
Beta Was this translation helpful? Give feedback.
-
Hi, this idea might have already been considered but I wanted to ask if this feature is possible. The doc says I can just call the rust function.
I know that simply calling rust function and putting the value in
std::sync::mpsc::channel
at the Rust side is possible. However this cannot work in WASM because Rust's channel does not support WASM.It will be very nice to stream directly from Dart to Rust for the sake of WASM environment. Would this be possible? Or would it be hard to implement?
Beta Was this translation helpful? Give feedback.
All reactions