mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -1,30 +0,0 @@
|
||||
package io.legado.app.utils
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.supervisorScope
|
||||
import org.mozilla.javascript.Context
|
||||
import kotlin.contracts.ExperimentalContracts
|
||||
import kotlin.contracts.InvocationKind
|
||||
import kotlin.contracts.contract
|
||||
|
||||
@OptIn(ExperimentalContracts::class)
|
||||
inline fun <T> suspendContinuation(crossinline block: suspend CoroutineScope.() -> T): T {
|
||||
contract {
|
||||
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
val cx = Context.enter()
|
||||
try {
|
||||
val pending = cx.captureContinuation()
|
||||
pending.applicationState = suspend {
|
||||
supervisorScope {
|
||||
block()
|
||||
}
|
||||
}
|
||||
throw pending
|
||||
} catch (e: IllegalStateException) {
|
||||
return runBlocking { block() }
|
||||
} finally {
|
||||
Context.exit()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user