1
0
Fork 0
mirror of https://github.com/MatomoCamp/matomocamp-companion-android.git synced 2024-09-19 16:13:46 +02:00

use a conflated channel for SharedPreferences.getAsFlow()

This commit is contained in:
Christophe Beyls 2021-12-26 15:55:12 +01:00
parent 0ea78d1725
commit f011e0a2b4

View file

@ -5,6 +5,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.onStart
/**
@ -36,7 +37,7 @@ private inline fun <T> SharedPreferences.getAsFlow(
}
registerOnSharedPreferenceChangeListener(listener)
awaitClose { unregisterOnSharedPreferenceChangeListener(listener) }
}.onStart {
}.conflate().onStart {
// By emitting the initial value here, we prevent the upstream callbackFlow from starting
// if the Flow consumer is only interested in the first value
emit(valueProvider(key))