Skip to content
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 StateFlow and MutableStateFlow support #109

Closed
robertjamison opened this issue Jun 12, 2024 · 3 comments
Closed

Add StateFlow and MutableStateFlow support #109

robertjamison opened this issue Jun 12, 2024 · 3 comments

Comments

@robertjamison
Copy link

robertjamison commented Jun 12, 2024

Flows require the direct statement of a CoroutineScope , stateIn, or collect() to properly access the data in a composable's LaunchedEffect. This is a lot of effort, and Flows are considered a cold state once completed.

Based on JetBrains' shift towards StateFlow and SharedFlow, I recommend adding a way to use both of these datatypes to the API, as well as their mutable versions a feature of the next update.

@xxfast
Copy link
Owner

xxfast commented Jun 14, 2024

Hi!. Do you mean changing the KStore's updates from Flow<T> to StateFlow<T>?

@robertjamison
Copy link
Author

Yes, but after a lot of thought, I think an easier option would be to include an example of how you properly generate a StateFlow<T> from the Flow<T> that KStore already outputs from store.update. It could be even as simple as linking to the page I sent before in the README.

@xxfast
Copy link
Owner

xxfast commented Jun 22, 2024

Hi @robertjamison.

I have some examples in my NYTimes app - but in that case I'm using molecule, so it might not work for your usecase

Coversion to StateFlow should be easy but requires some configurations to be made from the use-site

val playerFlow: StateFlow<Player?> = userStore.updates.stateIn(coroutineScope, SharingStarted.Lazily, null)

Here I'm making the following configurations

  1. the StateFlow will contain null as the initial value before the store is read. I usually tend to represent this as a Loading state
val Loading: Nothing? = null
val playerFlow: StateFlow<Player?> = userStore.updates.stateIn(coroutineScope, SharingStarted.Lazily, Loading)
  1. Sharing strategy here is Lazy
  2. You will need to provide a coroutine scope

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants