RoomList/Timeline: makes sure initial values are set before computing diff/update

This commit is contained in:
ganfra 2023-06-27 10:47:14 +02:00
parent eb00ef3c06
commit 8f5fb64ba5
2 changed files with 10 additions and 0 deletions

View file

@ -17,6 +17,7 @@
package io.element.android.libraries.matrix.impl.room
import io.element.android.libraries.matrix.api.room.RoomSummary
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
@ -33,6 +34,7 @@ class RoomSummaryListProcessor(
) {
private val roomSummariesByIdentifier = HashMap<String, RoomSummary>()
private val initLatch = CompletableDeferred<Unit>()
private val mutex = Mutex()
suspend fun postEntries(entries: List<RoomListEntry>) {
@ -40,9 +42,12 @@ class RoomSummaryListProcessor(
Timber.v("Update rooms from postEntries (with ${entries.size} items) on ${Thread.currentThread()}")
addAll(entries.map(::buildSummaryForRoomListEntry))
}
initLatch.complete(Unit)
}
suspend fun postUpdate(update: RoomListEntriesUpdate) {
// Makes sure to process first entries before update.
initLatch.await()
updateRoomSummaries {
Timber.v("Update rooms from postUpdate ($update) on ${Thread.currentThread()}")
applyUpdate(update)

View file

@ -19,6 +19,7 @@ package io.element.android.libraries.matrix.impl.timeline
import io.element.android.libraries.matrix.api.timeline.MatrixTimeline
import io.element.android.libraries.matrix.api.timeline.MatrixTimelineItem
import io.element.android.libraries.matrix.api.timeline.item.virtual.VirtualTimelineItem
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
@ -32,6 +33,7 @@ internal class MatrixTimelineDiffProcessor(
private val timelineItemFactory: MatrixTimelineItemMapper,
) {
private val initLatch = CompletableDeferred<Unit>()
private val mutex = Mutex()
suspend fun postItems(items: List<TimelineItem>) {
@ -40,9 +42,12 @@ internal class MatrixTimelineDiffProcessor(
addAll(mappedItems)
updateBackPaginationState()
}
initLatch.complete(Unit)
}
suspend fun postDiff(diff: TimelineDiff) {
// Makes sure to process first items before diff.
initLatch.await()
updateTimelineItems {
applyDiff(diff)
updateBackPaginationState()