cunarist / rinf

Rust for native business logic, Flutter for flexible and beautiful GUI
MIT License
1.99k stars 72 forks source link

provide example stream with state class (for storing state data) #231

Closed erlangparasu closed 8 months ago

erlangparasu commented 11 months ago

Report

please provide example stream with state class (for storing state data) currently only example with single function it would be great if there was an example for use with a class as storing state

https://github.com/cunarist/rinf/issues/230

temeddix commented 11 months ago

Thanks for the idea :)

This will be a good addition, yes. I will try to apply it when I have time later. Meanwhile, any kind of PR is welcome!

temeddix commented 11 months ago

For now, I can suggest using the lazy_static Rust crate for state management.

t3hmrman commented 11 months ago

Hey @temeddix maybe also consider using OnceLock which is in the standard lib now. In practice you end up doing a little more to make things work, like making a helper function but it works quite well.

I'm doing some diving on how best to make cross-platform apps in Rust (or something other the duopoly of Swift/Kotlin, essentially) and rinf looks great but state management being in Dart does worry me a little bit.

It occurs to me to move the state completely to Rust (and thus every call to modify any state must immediately move to Rust), but it wasn't clear the expected pattern was -- is there even a point in declaring any meaningful Dart state if someone opts to create/manage state only in Rust?

ryanhossain9797 commented 11 months ago

I made a toy app for doing something similar

enum AppState
{
    InputScreen (...)
    ResultScreen(...)
}

enum AppAction 
{
    AddInput(...)
    Calculate
}

the rust requests respond with an empty unit. but it also sends the latest state down the stream

the app has no state, it's a stream builder

the state on rust end is stored in OnceCell Mutex

temeddix commented 11 months ago

OnceCell and OnceLock look like a good choice, given that it's now part of the standard library.

Would anyone mind sharing one's repository link here, so we can share the whold idea of how the app would look like?

For most GUI apps, 'widget state' is not recommended to be managed by the backend logic, but the core state might be handled by Rust. If anyone is open to sharing one's repository, please let me know!

ryanhossain9797 commented 11 months ago

https://github.com/ryanhossain9797/proportional_cost_splitter_app

Here's mine if you want to take a look

erlangparasu commented 8 months ago

Is there a documentation link yet?

temeddix commented 8 months ago

Not really, it's just that the global state variable is now included in the example code(native/hub/src/sample_functions.rs).

However it would be nice to also add the state management in the docs. Let's keep this open until I write the state+stream guide in the docs as well :)

temeddix commented 8 months ago

May I ask, does #301 look okay? @erlangparasu

erlangparasu commented 8 months ago

@temeddix Thanks a lot!