Room: add extension method awaitAllRoomsAreLoaded with Timeout
This commit is contained in:
parent
7dbac91cd3
commit
8852514652
2 changed files with 22 additions and 6 deletions
|
|
@ -16,13 +16,18 @@
|
|||
|
||||
package io.element.android.libraries.matrix.api.room
|
||||
|
||||
import kotlinx.coroutines.TimeoutCancellationException
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.firstOrNull
|
||||
import kotlinx.coroutines.withTimeout
|
||||
import timber.log.Timber
|
||||
import kotlin.time.Duration
|
||||
|
||||
interface RoomSummaryDataSource {
|
||||
|
||||
sealed class LoadingState {
|
||||
object NotLoaded : LoadingState()
|
||||
data class Loaded(val numberOfRooms: Int): LoadingState()
|
||||
data class Loaded(val numberOfRooms: Int) : LoadingState()
|
||||
}
|
||||
|
||||
fun updateAllRoomsVisibleRange(range: IntRange)
|
||||
|
|
@ -30,3 +35,15 @@ interface RoomSummaryDataSource {
|
|||
fun allRooms(): StateFlow<List<RoomSummary>>
|
||||
fun inviteRooms(): StateFlow<List<RoomSummary>>
|
||||
}
|
||||
|
||||
suspend fun RoomSummaryDataSource.awaitAllRoomsAreLoaded(timeout: Duration = Duration.INFINITE) {
|
||||
try {
|
||||
withTimeout(timeout) {
|
||||
allRoomsLoadingState().firstOrNull {
|
||||
it is RoomSummaryDataSource.LoadingState.Loaded
|
||||
}
|
||||
}
|
||||
} catch (timeoutException: TimeoutCancellationException) {
|
||||
Timber.v("AwaitAllRooms: no response after $timeout")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue