-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add compose state molecule function #89
base: trunk
Are you sure you want to change the base?
Conversation
Is this meaningfully different than holding a |
All I can think of is that you can automatically subscribe when you use it in Compose(As you know, it will not be subscribed if you use StateFlow.value.). Google had a tweet recommending using Compose State in ViewModel and this was discussed once. |
Yeah I'm not really opposed to adding this API. Like I said, we talked about exposing a |
In my use case, I was still only thinking about UiModel in ViewModel, but it may be possible to use Compose's State in the entire app. For example, it may be easier to use Compose than Flow's zip. In that case, it would be simpler to not use Flow as a return value for the entire application. |
Is the benefit of this PR only that it omits an extra call to |
Matt and I talked about this a long time ago. I think the summary is that if you can hand a State directly to Compose UI then why not just expose it as a composable function that returns T instead? The nice thing about Flow and StateFlow is that you can consume them at a different layer than Compose UI. And it's true you can do that with State (via snapshotFlow), but if you do that then why not just produce a Flow or StateFlow directly? Can someone convince me otherwise? |
Thanks. Usage pattern in ViewModel Other Usage Patterns |
I mean yeah if you want to run your molecule in the UI layer composition (meaning it inherently has the lifecycle of the UI). If you instead prefer to run molecule in something with a longer lifecycle, then you need to have a holder like StateFlow or snapshot State. The difference between the two, for me specifically, boils down to their difference in API. The StateFlow API is clunky to use compared to snapshot State. You need to expose your data type inside a container type and of course call The tradeoff for the "fluid" API however is the setup you need to have to "listen" to snapshot state. But if the tailend of all your state reads is in a composable (whether UI or Molecule) then its, subjectively, better to expose APIs driven by snapshot State. |
I think your argument overstates the problems with The problem of mapping a The risk of adding this is very low. So we can just add it the next time I'm at a computer. But I also think its utility remains pretty low as well. |
Will need some tests. If you want to add them feel free. Otherwise I'll get to it sometime this week. |
Compose State can be used in this PR of Compose when creating a UiModel with ViewModel.
It can be used without StateFlow.collectAsState() when the user's UI is Compose.
This change is just a suggestion, I have not written any tests yet. I would first like to get your opinion on what you think about this change.
We need to keep the State alive through Configuration change by ViewModel.