From bf6fba7159c6a1111607571cf6e60c834220b5e1 Mon Sep 17 00:00:00 2001 From: DatLag Date: Sun, 17 Dec 2023 17:36:52 +0100 Subject: [PATCH] fix crashes --- .../datlag/burningseries/model/common/ExtendCoroutine.kt | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) 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) {