From e563b37ef081edcebf91df77cffd475372497c50 Mon Sep 17 00:00:00 2001 From: Horis <8674809+821938089@users.noreply.github.com> Date: Wed, 5 Feb 2025 15:08:44 +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 --- .../io/legado/app/help/coroutine/Coroutine.kt | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 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 8c493a549..2ca8ed9f6 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 @@ -2,7 +2,6 @@ package io.legado.app.help.coroutine import io.legado.app.utils.printOnDebug import kotlinx.coroutines.CancellationException -import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CompletionHandler import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineStart @@ -26,7 +25,7 @@ import kotlin.coroutines.CoroutineContext class Coroutine( val scope: CoroutineScope, context: CoroutineContext = Dispatchers.IO, - startOption: CoroutineStart = CoroutineStart.DEFAULT, + val startOption: CoroutineStart = CoroutineStart.DEFAULT, val executeContext: CoroutineContext = Dispatchers.Main, block: suspend CoroutineScope.() -> T ) { @@ -58,9 +57,6 @@ class Coroutine( private var timeMillis: Long? = null private var errorReturn: Result? = null - private val lazyWaiter = - if (startOption === CoroutineStart.LAZY) CompletableDeferred() else null - val isCancelled: Boolean get() = job.isCancelled @@ -160,16 +156,19 @@ class Coroutine( } fun start() { - lazyWaiter?.complete(Unit) + if (startOption === CoroutineStart.LAZY && isCancelled) { + cancel() + } else { + job.start() + } } private fun executeInternal( context: CoroutineContext, block: suspend CoroutineScope.() -> T ): Job { - return (scope.plus(executeContext)).launch(start = CoroutineStart.ATOMIC) { + return (scope.plus(executeContext)).launch(start = startOption) { try { - lazyWaiter?.await() start?.let { dispatchVoidCallback(this, it) } ensureActive() val value = executeBlock(this, context, timeMillis ?: 0L, block)