thlorenz / rid

Rust integrated Dart framework providing an easy way to build Flutter apps with Rust.
64 stars 4 forks source link

The right way to send streams from Dart to Rust #30

Closed enzotar closed 2 years ago

enzotar commented 2 years ago

I am trying to send event streams, mouse, keyboard, etc. from Dart to Rust. What is the right way to model this?

Would you send a Rid message on every event?

And what data type would you suggest for performance, assuming on the Dart side each event is an object with a few attributes? I tried sending with Msg::Add(Vec<u8>) but that didn't work. I get the following error: custom attribute panicked message: not yet implemented: RustArg::from::Composite::Composite::Vecr Would you send a JSON string?

thlorenz commented 2 years ago

Yes the right way is to use rid::post with a Reply that contains the data. At this point only Strings are supported to be sent. If you need to include more data what you should do instead is have an event that tells the app that something changed and have it retrieve the updated data via a call to the Store.

Have a look at this example: https://github.com/thlorenz/rid-examples/blob/master/flutter/todo_cubit/lib/blocs/cubit/todo_cubit.dart#L31-L33

which demonstrates how to listen to updates to a Todo and retrieve them from the store via _refresh.

So in your case you can export a field: Vec<u8> from the Store, and each time it changes you set it on the store and then do something like rid::post(Reply::FieldChanged).

Would you send a Rid message on every event?

That depends if you need to show it right away or if you can throttle them, i.e. only send a message every 5 secs or so.

Hope that helps and sorry for the delay.

Feel free to ask more if it isn't clear yet.