extracting flow extension for starting and ignoring future emissions

This commit is contained in:
Adam Brown 2022-10-02 16:06:42 +01:00 committed by Adam Brown
parent 8e336fd730
commit 511273517e
2 changed files with 8 additions and 8 deletions

View File

@ -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 <T> Flow<T>.firstOrNull(count: Int, predicate: suspend (T) -> Boolean): T? {
var counter = 0
@ -21,4 +17,6 @@ suspend fun <T> Flow<T>.firstOrNull(count: Int, predicate: suspend (T) -> Boolea
}
return result
}
}
fun <T> Flow<T>.startAndIgnoreEmissions(): Flow<Boolean> = this.map { false }.onStart { emit(true) }.filter { it }

View File

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