fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.
https://fzyzcjy.github.io/flutter_rust_bridge/
MIT License
4.29k stars 301 forks source link

Allow drop of JoinHandle in web #2383

Open sampaioletti opened 2 weeks ago

sampaioletti commented 2 weeks ago

By making the send a panic on fail

https://github.com/fzyzcjy/flutter_rust_bridge/blob/acf0d3248b2cd1edc2514d17a03e982f0fa6df78/frb_rust/src/rust_async/web.rs#L44

it prevents a scenario where you aren't concerned with the output or the join handle, and is inconsistent with the non-web behavior.

In my case I'm spawning the local to process some messages

#[flutter_rust_bridge::frb]
pub async fn start_processing()->Ctx{
  let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<String>();
  flutter_rust_bridge::spawn(async move {
      loop {
          let Some(msg) = rx.recv().await else {
              log::info!("Exiting");
              return;
          };
          log::info!("process msg");
          process(msg).await;
      }
  });
  Ctx{tx}
}

Everything works as expected.

I eventually drop the returned Ctx and am presented with a panic when the spawn returns and attempts to send the output on the channel.

Since I believe the only failure for sending the output would be when the user drops the join handle it would seem reasonable to ignore that error and not panic.

I'd be happy to submit a PR if that is acceptable.

welcome[bot] commented 2 weeks ago

Hi! Thanks for opening your first issue here! :smile:

fzyzcjy commented 2 weeks ago

I'd be happy to submit a PR if that is acceptable.

If we align the web behavior to be consistent with non-web behavior, then I guess that change would be quite welcome!