From a9059b9ce2a2b3705ef3321fa09ccc656ca88b60 Mon Sep 17 00:00:00 2001 From: Administrator <1760316362@qq.com> Date: Mon, 15 Jul 2019 17:21:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/coroutine/Coroutine.kt | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt b/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt index 686d6b9fc..76e4a8439 100644 --- a/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt +++ b/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt @@ -108,14 +108,7 @@ class Coroutine() { { start?.let { it() } - val time = timeMillis - val result = if (time != null && time > 0) { - withTimeout(time) { - executeBlock(block) - } - } else { - executeBlock(block) - } + val result = executeBlock(block, timeMillis ?: 0L) success?.let { it(result) } }, @@ -134,10 +127,11 @@ class Coroutine() { }) } - private suspend fun executeBlock(block: suspend CoroutineScope.() -> T): T? { - return withContext(Dispatchers.IO) { + private suspend fun executeBlock(block: suspend CoroutineScope.() -> T, timeMillis: Long): T? { + val asyncBlock = withContext(Dispatchers.IO) { block() } + return if (timeMillis > 0L) withTimeout(timeMillis) { asyncBlock } else asyncBlock } private suspend fun tryCatch(