Browse Source

Timeline: refactor a bit

jonny/proxy
ganfra 1 year ago
parent
commit
8c66924be9
  1. 5
      libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt
  2. 35
      libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/MatrixTimelineDiffProcessor.kt
  3. 8
      libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RustMatrixTimeline.kt

5
libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt

@ -108,13 +108,13 @@ class RustMatrixRoom( @@ -108,13 +108,13 @@ class RustMatrixRoom(
timelineLimit = null
)
roomListItem.subscribe(settings)
roomCoroutineScope.launch(coroutineDispatchers.computation) {
innerRoom.timelineDiffFlow { initialList ->
timeline.postItems(initialList)
}.onEach {
syncUpdateFlow.value = systemClock.epochMillis()
timeline.postDiff(it)
}.launchIn(roomCoroutineScope)
roomCoroutineScope.launch {
}.launchIn(this)
fetchMembers()
}
isInit.value = true
@ -360,7 +360,6 @@ class RustMatrixRoom( @@ -360,7 +360,6 @@ class RustMatrixRoom(
}
}
private suspend fun fetchMembers() = withContext(coroutineDispatchers.io) {
runCatching {
innerRoom.fetchMembers()

35
libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/MatrixTimelineDiffProcessor.kt

@ -19,11 +19,9 @@ package io.element.android.libraries.matrix.impl.timeline @@ -19,11 +19,9 @@ 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.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.matrix.rustcomponents.sdk.TimelineChange
import org.matrix.rustcomponents.sdk.TimelineDiff
import org.matrix.rustcomponents.sdk.TimelineItem
@ -31,20 +29,23 @@ import org.matrix.rustcomponents.sdk.TimelineItem @@ -31,20 +29,23 @@ import org.matrix.rustcomponents.sdk.TimelineItem
internal class MatrixTimelineDiffProcessor(
private val paginationState: MutableStateFlow<MatrixTimeline.PaginationState>,
private val timelineItems: MutableStateFlow<List<MatrixTimelineItem>>,
private val coroutineScope: CoroutineScope,
private val diffDispatcher: CoroutineDispatcher,
private val timelineItemFactory: MatrixTimelineItemMapper,
) {
fun postDiff(diff: TimelineDiff) {
coroutineScope.launch {
private val mutex = Mutex()
suspend fun postItems(items: List<TimelineItem>) {
updateTimelineItems {
applyDiff(diff)
val mappedItems = items.map { it.asMatrixTimelineItem() }
addAll(mappedItems)
updateBackPaginationState()
}
when (val firstItem = timelineItems.value.firstOrNull()) {
is MatrixTimelineItem.Virtual -> updateBackPaginationState(firstItem.virtual)
else -> updateBackPaginationState(null)
}
suspend fun postDiff(diff: TimelineDiff) {
updateTimelineItems {
applyDiff(diff)
updateBackPaginationState()
}
}
@ -68,7 +69,7 @@ internal class MatrixTimelineDiffProcessor( @@ -68,7 +69,7 @@ internal class MatrixTimelineDiffProcessor(
}
private suspend fun updateTimelineItems(block: MutableList<MatrixTimelineItem>.() -> Unit) =
withContext(diffDispatcher) {
mutex.withLock {
val mutableTimelineItems = timelineItems.value.toMutableList()
block(mutableTimelineItems)
timelineItems.value = mutableTimelineItems
@ -119,8 +120,14 @@ internal class MatrixTimelineDiffProcessor( @@ -119,8 +120,14 @@ internal class MatrixTimelineDiffProcessor(
}
}
private fun List<MatrixTimelineItem>.updateBackPaginationState() {
when (val firstItem = firstOrNull()) {
is MatrixTimelineItem.Virtual -> updateBackPaginationState(firstItem.virtual)
else -> updateBackPaginationState(null)
}
}
private fun TimelineItem.asMatrixTimelineItem(): MatrixTimelineItem {
return timelineItemFactory.map(this)
}
}

8
libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/timeline/RustMatrixTimeline.kt

@ -66,8 +66,6 @@ class RustMatrixTimeline( @@ -66,8 +66,6 @@ class RustMatrixTimeline(
private val timelineDiffProcessor = MatrixTimelineDiffProcessor(
paginationState = paginationState,
timelineItems = timelineItems,
coroutineScope = roomCoroutineScope,
diffDispatcher = coroutineDispatchers.diffUpdateDispatcher,
timelineItemFactory = timelineItemFactory,
)
@ -80,11 +78,11 @@ class RustMatrixTimeline( @@ -80,11 +78,11 @@ class RustMatrixTimeline(
return timelineItems.sample(50)
}
internal fun postItems(items: List<TimelineItem>) {
timelineItems.value = items.map(timelineItemFactory::map)
internal suspend fun postItems(items: List<TimelineItem>) {
timelineDiffProcessor.postItems(items)
}
internal fun postDiff(timelineDiff: TimelineDiff) {
internal suspend fun postDiff(timelineDiff: TimelineDiff) {
timelineDiffProcessor.postDiff(timelineDiff)
}

Loading…
Cancel
Save