mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -2,6 +2,7 @@ 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
|
||||
@@ -25,7 +26,7 @@ import kotlin.coroutines.CoroutineContext
|
||||
class Coroutine<T>(
|
||||
val scope: CoroutineScope,
|
||||
context: CoroutineContext = Dispatchers.IO,
|
||||
val startOption: CoroutineStart = CoroutineStart.DEFAULT,
|
||||
startOption: CoroutineStart = CoroutineStart.DEFAULT,
|
||||
val executeContext: CoroutineContext = Dispatchers.Main,
|
||||
block: suspend CoroutineScope.() -> T
|
||||
) {
|
||||
@@ -57,6 +58,9 @@ class Coroutine<T>(
|
||||
private var timeMillis: Long? = null
|
||||
private var errorReturn: Result<T>? = null
|
||||
|
||||
private val lazyWaiter =
|
||||
if (startOption === CoroutineStart.LAZY) CompletableDeferred<Unit>() else null
|
||||
|
||||
val isCancelled: Boolean
|
||||
get() = job.isCancelled
|
||||
|
||||
@@ -156,15 +160,16 @@ class Coroutine<T>(
|
||||
}
|
||||
|
||||
fun start() {
|
||||
job.start()
|
||||
lazyWaiter?.complete(Unit)
|
||||
}
|
||||
|
||||
private fun executeInternal(
|
||||
context: CoroutineContext,
|
||||
block: suspend CoroutineScope.() -> T
|
||||
): Job {
|
||||
return (scope.plus(executeContext)).launch(start = startOption) {
|
||||
return (scope.plus(executeContext)).launch(start = CoroutineStart.ATOMIC) {
|
||||
try {
|
||||
lazyWaiter?.await()
|
||||
start?.let { dispatchVoidCallback(this, it) }
|
||||
ensureActive()
|
||||
val value = executeBlock(this, context, timeMillis ?: 0L, block)
|
||||
|
||||
@@ -147,7 +147,9 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
|
||||
if (it.isDir) {
|
||||
n++
|
||||
channel.trySend(it)
|
||||
} else {
|
||||
} else if (it.name.matches(bookFileRegex)
|
||||
|| it.name.matches(archiveFileRegex)
|
||||
) {
|
||||
list.add(it)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user