Is there a way to chain `onEach` with `MutableStat...
# coroutines
k
Is there a way to chain
onEach
with
MutableStateFlow
and return a
MutableStateFlow
? My use case -- calling
SavedStateHandle.getLiveData(key: String)
will automatically save new emissions to that live data into the saved state. I would like something like this to be possible with StateFlow
Copy code
fun <T> SavedStateHandle.getStateFlow(key: String, defaultValue: T): MutableStateFlow<T> {
    return MutableStateFlow(this[key] ?: defaultValue).onEach {
        this[key] = it
    }
}
However,
onEach
returns a regular instance of
Flow
. Is this something that's been discussed? If not, I'm more than willing to open an issue.
z
I asked about this sort of thing a few months ago, the response was “it’s harder than it looks” for transforming operators, but I don’t think that argument applies to side effect operators like
onEach
. If you file this, you might want to contrast it with the issue I filed: https://github.com/Kotlin/kotlinx.coroutines/issues/2081
k
Thanks! I think I will file this issue.
z
Cool, please link here when you do so people can follow up!
k
Aaaaaaaand it was closed.