diff --git a/model/src/commonMain/kotlin/dev/datlag/burningseries/model/common/ExtendCoroutine.kt b/model/src/commonMain/kotlin/dev/datlag/burningseries/model/common/ExtendCoroutine.kt index 79b85971..784ac560 100644 --- a/model/src/commonMain/kotlin/dev/datlag/burningseries/model/common/ExtendCoroutine.kt +++ b/model/src/commonMain/kotlin/dev/datlag/burningseries/model/common/ExtendCoroutine.kt @@ -3,10 +3,7 @@ package dev.datlag.burningseries.model.common import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.coroutineScope -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.FlowCollector -import kotlinx.coroutines.flow.StateFlow -import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.* fun scopeCatching(block: () -> T): Result = try { Result.success(block()) @@ -55,7 +52,9 @@ inline fun Any?.safeCast(): T? { suspend fun Flow.collectSafe(collector: FlowCollector) { suspendCatching { this@collectSafe.collect(collector) - }.getOrNull() ?: collector.emit(this.first()) + }.getOrNull() ?: suspendCatching { + this@collectSafe.firstOrNull() + }.getOrNull()?.let { collector.emit(it) } } suspend fun StateFlow.collectSafe(collector: FlowCollector) {