From 8e336fd7305f8504f2f1d58579f746885c8b9581 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Sat, 1 Oct 2022 14:03:30 +0100 Subject: [PATCH] avoiding duplicated room event emissions when watching a specific room id --- .../kotlin/app/dapk/st/domain/sync/RoomPersistence.kt | 8 +++----- .../main/kotlin/app/dapk/st/messenger/TimelineUseCase.kt | 9 +++------ 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/domains/store/src/main/kotlin/app/dapk/st/domain/sync/RoomPersistence.kt b/domains/store/src/main/kotlin/app/dapk/st/domain/sync/RoomPersistence.kt index 7523e1d..5adf63e 100644 --- a/domains/store/src/main/kotlin/app/dapk/st/domain/sync/RoomPersistence.kt +++ b/domains/store/src/main/kotlin/app/dapk/st/domain/sync/RoomPersistence.kt @@ -13,10 +13,7 @@ import app.dapk.st.matrix.sync.RoomStore import com.squareup.sqldelight.runtime.coroutines.asFlow import com.squareup.sqldelight.runtime.coroutines.mapToList import com.squareup.sqldelight.runtime.coroutines.mapToOneNotNull -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.combine -import kotlinx.coroutines.flow.distinctUntilChanged -import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.* import kotlinx.serialization.json.Json private val json = Json @@ -54,12 +51,13 @@ internal class RoomPersistence( override fun latest(roomId: RoomId): Flow { val overviewFlow = database.overviewStateQueries.selectRoom(roomId.value).asFlow().mapToOneNotNull().map { json.decodeFromString(RoomOverview.serializer(), it) - } + }.distinctUntilChanged() return database.roomEventQueries.selectRoom(roomId.value) .asFlow() .mapToList() .map { it.map { json.decodeFromString(RoomEvent.serializer(), it) } } + .distinctUntilChanged() .combine(overviewFlow) { events, overview -> RoomState(overview, events) } diff --git a/features/messenger/src/main/kotlin/app/dapk/st/messenger/TimelineUseCase.kt b/features/messenger/src/main/kotlin/app/dapk/st/messenger/TimelineUseCase.kt index c117f39..629c466 100644 --- a/features/messenger/src/main/kotlin/app/dapk/st/messenger/TimelineUseCase.kt +++ b/features/messenger/src/main/kotlin/app/dapk/st/messenger/TimelineUseCase.kt @@ -7,10 +7,7 @@ import app.dapk.st.matrix.message.MessageService import app.dapk.st.matrix.room.RoomService import app.dapk.st.matrix.sync.RoomState import app.dapk.st.matrix.sync.SyncService -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.combine -import kotlinx.coroutines.flow.map -import kotlinx.coroutines.flow.onStart +import kotlinx.coroutines.flow.* internal typealias ObserveTimelineUseCase = (RoomId, UserId) -> Flow @@ -25,7 +22,7 @@ internal class TimelineUseCaseImpl( return combine( roomDatasource(roomId), messageService.localEchos(roomId), - syncService.events() + syncService.events(roomId) ) { roomState, localEchos, events -> MessengerState( roomState = when { @@ -45,7 +42,7 @@ internal class TimelineUseCaseImpl( } private fun roomDatasource(roomId: RoomId) = combine( - syncService.startSyncing().map { false }.onStart { emit(true) }, + syncService.startSyncing().map { false }.onStart { emit(true) }.filter { it }, syncService.room(roomId) ) { _, room -> room } }