Is there a proper way to observe a SharedFlow in a...
# compose
m
Is there a proper way to observe a SharedFlow in a Composable function, for StateFlow we'd use
collectAsState
but if I do that for a SharedFlow, it will remember the last value, instead of firing and forget
c
Can you describe your use case?
m
Basically I am trying to send a one time SideEffect that will play an animation
but I want to be able to do this everytime I send that SideEffect to play the animation
c
Someone may have a better answer, but you can collect the SharedFlow in a LaunchedEffect, then do what you will as new values come in
🙏 1
I have some similar-sounding requirement in my backlog currently so I’d be curious to know how this turns out for you
m
I will give you an update if it works out 👍
t
Had a similiar issue for navigation event. Found that extensionfunction on the web (can't remember where) and it works just fine.
Copy code
@Composable
fun <T> Flow<T>.collectAsEffect(
    context: CoroutineContext = EmptyCoroutineContext,
    block: (T) -> Unit,
) {
    LaunchedEffect(key1 = Unit) {
        onEach(block).flowOn(context).launchIn(this)
    }
}
🙌 1
m
@Chris Fillmore ^ -- I had a work around but this seems to be much cleaner
🙂 1
👍 1
Thank you @Tobias Gronbach
165 Views