From 511273517ef06257c08d31563c714be959cc0604 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Sun, 2 Oct 2022 16:06:42 +0100 Subject: [PATCH] extracting flow extension for starting and ignoring future emissions --- .../app/dapk/st/core/extensions/FlowExtensions.kt | 10 ++++------ .../kotlin/app/dapk/st/messenger/TimelineUseCase.kt | 6 ++++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/src/main/kotlin/app/dapk/st/core/extensions/FlowExtensions.kt b/core/src/main/kotlin/app/dapk/st/core/extensions/FlowExtensions.kt index 2ffc7da..92be2fb 100644 --- a/core/src/main/kotlin/app/dapk/st/core/extensions/FlowExtensions.kt +++ b/core/src/main/kotlin/app/dapk/st/core/extensions/FlowExtensions.kt @@ -1,11 +1,7 @@ package app.dapk.st.core.extensions -import kotlinx.coroutines.InternalCoroutinesApi -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.filter -import kotlinx.coroutines.flow.takeWhile +import kotlinx.coroutines.flow.* -@OptIn(InternalCoroutinesApi::class) suspend fun Flow.firstOrNull(count: Int, predicate: suspend (T) -> Boolean): T? { var counter = 0 @@ -21,4 +17,6 @@ suspend fun Flow.firstOrNull(count: Int, predicate: suspend (T) -> Boolea } return result -} \ No newline at end of file +} + +fun Flow.startAndIgnoreEmissions(): Flow = this.map { false }.onStart { emit(true) }.filter { it } \ No newline at end of file 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 629c466..c1d9538 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 @@ -1,5 +1,6 @@ package app.dapk.st.messenger +import app.dapk.st.core.extensions.startAndIgnoreEmissions import app.dapk.st.matrix.common.RoomId import app.dapk.st.matrix.common.RoomMember import app.dapk.st.matrix.common.UserId @@ -7,7 +8,8 @@ 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.* +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.combine internal typealias ObserveTimelineUseCase = (RoomId, UserId) -> Flow @@ -42,7 +44,7 @@ internal class TimelineUseCaseImpl( } private fun roomDatasource(roomId: RoomId) = combine( - syncService.startSyncing().map { false }.onStart { emit(true) }.filter { it }, + syncService.startSyncing().startAndIgnoreEmissions(), syncService.room(roomId) ) { _, room -> room } }