letsar / binder

A lightweight, yet powerful way to bind your application state with your business logic.
MIT License
178 stars 12 forks source link

async calls in Computed #9

Closed erf closed 3 years ago

erf commented 3 years ago

One of the great things about Riverpod, is that you can use Providers to listen to and transform state from one to another. E.g. i transform a Firebase User to my custom AppUser, which requires a set of async calls to check if the user already exists in Firestore. It seem Computed is similar to Providers in this sense, but it seem i'm not allowed to add the async keyword to the Computed function. Is this something that could be changed or is there another way to solve combining states with async calls using binder?

Thanks for a VERY promising package! :)

letsar commented 3 years ago

In my opinion Computedshould stay synchronous to avoid complexity. What you want can be done with a StateRefand a bit of Logic. For example you can have an async method on a Logic which would fetch your FirebaseUser, checks if the user exists or not and set an AppUser which is stored in a StateRefat the end of the method.

If it's something done with a Stream, you can implement something like in this example: https://github.com/letsar/binder/blob/main/examples/firebase_login/lib/modules/authentication/logic.dart.

If it's not clear, you can provide me a simple example and I will be pleased to convert it to Binder.

erf commented 3 years ago

In my case User should be a StateRef and i have a couple of other states which depends on User changes, but i guess i could make a Logic for each of these states and have them all listen to the User stream. I think this should work fine for most cases, but it if i have more complex states, i can't reuse and watch state in the same convenient way like with Provider (using async calls), which is a bit sad. Maybe a ComputedFuture could be created for async calls which returns a future when watched?

I'm also a bit curious as to how i would initalize these states - in the authentication example i saw you called init in initState, could you not have Logic implement Loadable and use the load function instead of the init function, and you could load it using the LogicLoader, or do you do it like that since you'd just like to call it once?

erf commented 3 years ago

I'll close this issue for now, as the current solution is sufficient for my needs and my original question was answered. I'll create a new issue if a more concrete problem / need should occur.