avoiding duplicated room event emissions when watching a specific room id

This commit is contained in:
Adam Brown 2022-10-01 14:03:30 +01:00 committed by Adam Brown
parent 74dff9ccf2
commit 8e336fd730
2 changed files with 6 additions and 11 deletions

View File

@ -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<RoomState> {
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)
}

View File

@ -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<MessengerState>
@ -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 }
}