优化
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
package io.legado.app.help.coroutine
|
||||||
|
|
||||||
|
import kotlin.coroutines.cancellation.CancellationException
|
||||||
|
|
||||||
|
class ActivelyCancelException : CancellationException()
|
||||||
@@ -40,8 +40,6 @@ class Coroutine<T>(
|
|||||||
private var timeMillis: Long? = null
|
private var timeMillis: Long? = null
|
||||||
private var errorReturn: Result<T>? = null
|
private var errorReturn: Result<T>? = null
|
||||||
|
|
||||||
private var isCancelCalled = false
|
|
||||||
|
|
||||||
val isCancelled: Boolean
|
val isCancelled: Boolean
|
||||||
get() = job.isCancelled
|
get() = job.isCancelled
|
||||||
|
|
||||||
@@ -116,8 +114,7 @@ class Coroutine<T>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
//取消当前任务
|
//取消当前任务
|
||||||
fun cancel(cause: CancellationException? = null) {
|
fun cancel(cause: ActivelyCancelException = ActivelyCancelException()) {
|
||||||
isCancelCalled = true
|
|
||||||
job.cancel(cause)
|
job.cancel(cause)
|
||||||
cancel?.let {
|
cancel?.let {
|
||||||
MainScope().launch {
|
MainScope().launch {
|
||||||
@@ -149,7 +146,7 @@ class Coroutine<T>(
|
|||||||
success?.let { dispatchCallback(this, value, it) }
|
success?.let { dispatchCallback(this, value, it) }
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
e.printOnDebug()
|
e.printOnDebug()
|
||||||
if (e is CancellationException && !isCancelCalled) this@Coroutine.cancel()
|
if (e is ActivelyCancelException) this@Coroutine.cancel()
|
||||||
if (e is CancellationException && e !is TimeoutCancellationException) {
|
if (e is CancellationException && e !is TimeoutCancellationException) {
|
||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user