From 461ad7fa420d6df317f6a4fba8689f0b5c0e77b3 Mon Sep 17 00:00:00 2001 From: tarsin Date: Wed, 8 Feb 2023 22:38:18 +0800 Subject: [PATCH] Better impl of runSuspendCatching --- .../ehviewer/spider/SpiderQueenWorker.kt | 8 +++---- .../main/java/moe/tarsin/coroutines/Result.kt | 21 +++++++------------ 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/spider/SpiderQueenWorker.kt b/app/src/main/java/com/hippo/ehviewer/spider/SpiderQueenWorker.kt index 6a98a732b..2838038d7 100644 --- a/app/src/main/java/com/hippo/ehviewer/spider/SpiderQueenWorker.kt +++ b/app/src/main/java/com/hippo/ehviewer/spider/SpiderQueenWorker.kt @@ -22,7 +22,7 @@ import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Semaphore import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withPermit -import moe.tarsin.coroutines.runCatchingCancellationTransparent +import moe.tarsin.coroutines.runSuspendCatching import kotlin.coroutines.CoroutineContext class SpiderQueenWorker(private val queen: SpiderQueen) : CoroutineScope { @@ -148,7 +148,7 @@ class SpiderQueenWorker(private val queen: SpiderQueen) : CoroutineScope { "?nl=$skipHathKey" } } - runCatchingCancellationTransparent { + runSuspendCatching { getGalleryPage(pageUrl, spiderInfo.gid, spiderInfo.token).also { if (StringUtils.endsWith(it.imageUrl, URL_509_SUFFIX_ARRAY)) { // Get 509 @@ -193,7 +193,7 @@ class SpiderQueenWorker(private val queen: SpiderQueen) : CoroutineScope { error = "ShowKey error" return@repeat } - runCatchingCancellationTransparent { + runSuspendCatching { getGalleryPageApi( spiderInfo.gid, index, @@ -239,7 +239,7 @@ class SpiderQueenWorker(private val queen: SpiderQueen) : CoroutineScope { } Log.d(DEBUG_TAG, targetImageUrl) - runCatchingCancellationTransparent { + runSuspendCatching { Log.d(DEBUG_TAG, "Start download image $index") val success: Boolean = spiderDen.makeHttpCallAndSaveImage( index, diff --git a/app/src/main/java/moe/tarsin/coroutines/Result.kt b/app/src/main/java/moe/tarsin/coroutines/Result.kt index 11acbb34a..281fd0565 100644 --- a/app/src/main/java/moe/tarsin/coroutines/Result.kt +++ b/app/src/main/java/moe/tarsin/coroutines/Result.kt @@ -2,20 +2,13 @@ package moe.tarsin.coroutines import kotlinx.coroutines.CancellationException -inline fun runCatchingCancellationTransparent(block: () -> R): Result { - return try { - Result.success(block()) - } catch (e: Throwable) { - if (e is CancellationException) throw e - Result.failure(e) - } +inline fun Result<*>.except(): Result<*> = + onFailure { if (it is T) throw it } + +inline fun runSuspendCatching(block: () -> R): Result { + return runCatching(block).apply { except() } } -inline fun T.runCatchingCancellationTransparent(block: T.() -> R): Result { - return try { - Result.success(block()) - } catch (e: Throwable) { - if (e is CancellationException) throw e - Result.failure(e) - } +inline fun T.runSuspendCatching(block: T.() -> R): Result { + return runCatching(block).apply { except() } }