优化
This commit is contained in:
@@ -2,6 +2,7 @@ package io.legado.app.help.coroutine
|
|||||||
|
|
||||||
import io.legado.app.utils.printOnDebug
|
import io.legado.app.utils.printOnDebug
|
||||||
import kotlinx.coroutines.CancellationException
|
import kotlinx.coroutines.CancellationException
|
||||||
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
import kotlinx.coroutines.CompletionHandler
|
import kotlinx.coroutines.CompletionHandler
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.CoroutineStart
|
import kotlinx.coroutines.CoroutineStart
|
||||||
@@ -25,7 +26,7 @@ import kotlin.coroutines.CoroutineContext
|
|||||||
class Coroutine<T>(
|
class Coroutine<T>(
|
||||||
val scope: CoroutineScope,
|
val scope: CoroutineScope,
|
||||||
context: CoroutineContext = Dispatchers.IO,
|
context: CoroutineContext = Dispatchers.IO,
|
||||||
val startOption: CoroutineStart = CoroutineStart.DEFAULT,
|
startOption: CoroutineStart = CoroutineStart.DEFAULT,
|
||||||
val executeContext: CoroutineContext = Dispatchers.Main,
|
val executeContext: CoroutineContext = Dispatchers.Main,
|
||||||
block: suspend CoroutineScope.() -> T
|
block: suspend CoroutineScope.() -> T
|
||||||
) {
|
) {
|
||||||
@@ -57,6 +58,9 @@ 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 val lazyWaiter =
|
||||||
|
if (startOption === CoroutineStart.LAZY) CompletableDeferred<Unit>() else null
|
||||||
|
|
||||||
val isCancelled: Boolean
|
val isCancelled: Boolean
|
||||||
get() = job.isCancelled
|
get() = job.isCancelled
|
||||||
|
|
||||||
@@ -156,15 +160,16 @@ class Coroutine<T>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun start() {
|
fun start() {
|
||||||
job.start()
|
lazyWaiter?.complete(Unit)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun executeInternal(
|
private fun executeInternal(
|
||||||
context: CoroutineContext,
|
context: CoroutineContext,
|
||||||
block: suspend CoroutineScope.() -> T
|
block: suspend CoroutineScope.() -> T
|
||||||
): Job {
|
): Job {
|
||||||
return (scope.plus(executeContext)).launch(start = startOption) {
|
return (scope.plus(executeContext)).launch(start = CoroutineStart.ATOMIC) {
|
||||||
try {
|
try {
|
||||||
|
lazyWaiter?.await()
|
||||||
start?.let { dispatchVoidCallback(this, it) }
|
start?.let { dispatchVoidCallback(this, it) }
|
||||||
ensureActive()
|
ensureActive()
|
||||||
val value = executeBlock(this, context, timeMillis ?: 0L, block)
|
val value = executeBlock(this, context, timeMillis ?: 0L, block)
|
||||||
|
|||||||
@@ -147,7 +147,9 @@ class ImportBookViewModel(application: Application) : BaseViewModel(application)
|
|||||||
if (it.isDir) {
|
if (it.isDir) {
|
||||||
n++
|
n++
|
||||||
channel.trySend(it)
|
channel.trySend(it)
|
||||||
} else {
|
} else if (it.name.matches(bookFileRegex)
|
||||||
|
|| it.name.matches(archiveFileRegex)
|
||||||
|
) {
|
||||||
list.add(it)
|
list.add(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user