mirror of
https://github.com/home-assistant/android
synced 2024-10-15 12:32:54 +00:00
Add throttling/debouncing on Wear OS for registry updates (#3517)
This commit is contained in:
parent
7d6f11af4f
commit
d81913b7ea
|
@ -0,0 +1,20 @@
|
|||
package io.homeassistant.companion.android.util
|
||||
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.conflate
|
||||
import kotlinx.coroutines.flow.transform
|
||||
|
||||
/**
|
||||
* Emit the first value from a Flow, and then after the period has passed the last item emitted
|
||||
* by the Flow during that period (if different). This ensures throttling/debouncing but also
|
||||
* always receiving the first and last item of the Flow.
|
||||
*
|
||||
* From https://github.com/Kotlin/kotlinx.coroutines/issues/1446#issuecomment-1198103541
|
||||
*/
|
||||
fun <T> Flow<T>.throttleLatest(delayMillis: Long): Flow<T> = this
|
||||
.conflate()
|
||||
.transform {
|
||||
emit(it)
|
||||
delay(delayMillis)
|
||||
}
|
|
@ -29,6 +29,7 @@ import io.homeassistant.companion.android.database.wear.getAll
|
|||
import io.homeassistant.companion.android.database.wear.getAllFlow
|
||||
import io.homeassistant.companion.android.sensors.SensorReceiver
|
||||
import io.homeassistant.companion.android.util.RegistriesDataHandler
|
||||
import io.homeassistant.companion.android.util.throttleLatest
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
@ -217,7 +218,7 @@ class MainViewModel @Inject constructor(
|
|||
if (!homePresenter.isConnected() || isFavoritesOnly) {
|
||||
return
|
||||
}
|
||||
homePresenter.getAreaRegistryUpdates()?.collect {
|
||||
homePresenter.getAreaRegistryUpdates()?.throttleLatest(1000)?.collect {
|
||||
areaRegistry = homePresenter.getAreaRegistry()
|
||||
areas.clear()
|
||||
areaRegistry?.let {
|
||||
|
@ -231,7 +232,7 @@ class MainViewModel @Inject constructor(
|
|||
if (!homePresenter.isConnected() || isFavoritesOnly) {
|
||||
return
|
||||
}
|
||||
homePresenter.getDeviceRegistryUpdates()?.collect {
|
||||
homePresenter.getDeviceRegistryUpdates()?.throttleLatest(1000)?.collect {
|
||||
deviceRegistry = homePresenter.getDeviceRegistry()
|
||||
updateEntityDomains()
|
||||
}
|
||||
|
@ -241,7 +242,7 @@ class MainViewModel @Inject constructor(
|
|||
if (!homePresenter.isConnected()) {
|
||||
return
|
||||
}
|
||||
homePresenter.getEntityRegistryUpdates()?.collect {
|
||||
homePresenter.getEntityRegistryUpdates()?.throttleLatest(1000)?.collect {
|
||||
entityRegistry = homePresenter.getEntityRegistry()
|
||||
_supportedEntities.value = getSupportedEntities()
|
||||
updateEntityDomains()
|
||||
|
|
Loading…
Reference in a new issue