https://kotlinlang.org logo
#coroutines
Title
# coroutines
k

kevin.cianfarini

10/27/2020, 3:07 AM
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

Zach Klippenstein (he/him) [MOD]

10/27/2020, 4:17 PM
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

kevin.cianfarini

10/27/2020, 4:28 PM
Thanks! I think I will file this issue.
z

Zach Klippenstein (he/him) [MOD]

10/27/2020, 5:57 PM
Cool, please link here when you do so people can follow up!
k

kevin.cianfarini

10/28/2020, 2:15 PM
Aaaaaaaand it was closed.
58 Views