Is there a nice way to convert a shared flow to a ...
# coroutines
r
Is there a nice way to convert a shared flow to a state flow by just providing the initial value? The flow is already hot, so I'm hoping I wouldn't need to provide a scope (and wouldn't have a separate collection coroutine running)
The closest I can find is
MutableStateFlow().onCompletion{ emitAll(sharedFlow) }
which loses the state flow type
g
Do you mean something like:
sharedFlow.stateIn(someCoroutineScope)
?
r
Kind of, but I want to provide the initial value, and ideally not have collection running in another coroutine
j
How would you imagine it would work behind the scenes?
u
You could provide your own implementation of the StateFlow interface:
Copy code
public interface StateFlow<out T> : SharedFlow<T> {
    /**
     * The current value of this state flow.
     */
    public val value: T
}
But it’s hard to get right. If look at google for implementations of `fun StateFlow.map(…):StateFlow`you will probably find some inspiration
r
I realized that the constantly up to date
value
accessor was driving most of the problems, and I didn't actually need it (I just needed the initial value to pass into
collectAsState
, so I went with that and just passed around the initial value and flow of updates
👍 1