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:
parent
0ea78d1725
commit
f011e0a2b4
1 changed files with 2 additions and 1 deletions
|
@ -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))
|
||||
|
|
Loading…
Reference in a new issue