优化 #5716
This commit is contained in:
@@ -1,13 +1,16 @@
|
|||||||
package io.legado.app.utils
|
package io.legado.app.utils
|
||||||
|
|
||||||
import androidx.core.os.postDelayed
|
|
||||||
import com.script.ScriptBindings
|
import com.script.ScriptBindings
|
||||||
import com.script.rhino.RhinoScriptEngine
|
import com.script.rhino.RhinoScriptEngine
|
||||||
import io.legado.app.exception.RegexTimeoutException
|
import io.legado.app.exception.RegexTimeoutException
|
||||||
import io.legado.app.help.CrashHandler
|
import io.legado.app.help.CrashHandler
|
||||||
import io.legado.app.help.coroutine.Coroutine
|
import io.legado.app.help.coroutine.Coroutine
|
||||||
import kotlinx.coroutines.Dispatchers.IO
|
import kotlinx.coroutines.Dispatchers.IO
|
||||||
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import kotlinx.coroutines.selects.onTimeout
|
||||||
|
import kotlinx.coroutines.selects.select
|
||||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||||
import splitties.init.appCtx
|
import splitties.init.appCtx
|
||||||
import java.util.regex.Matcher
|
import java.util.regex.Matcher
|
||||||
@@ -19,13 +22,15 @@ private val handler by lazy { buildMainHandler() }
|
|||||||
/**
|
/**
|
||||||
* 带有超时检测的正则替换
|
* 带有超时检测的正则替换
|
||||||
*/
|
*/
|
||||||
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
fun CharSequence.replace(regex: Regex, replacement: String, timeout: Long): String {
|
fun CharSequence.replace(regex: Regex, replacement: String, timeout: Long): String {
|
||||||
val charSequence = this@replace
|
val charSequence = this@replace
|
||||||
val isJs = replacement.startsWith("@js:")
|
val isJs = replacement.startsWith("@js:")
|
||||||
val replacement1 = if (isJs) replacement.substring(4) else replacement
|
val replacement1 = if (isJs) replacement.substring(4) else replacement
|
||||||
return runBlocking {
|
return runBlocking {
|
||||||
suspendCancellableCoroutine { block ->
|
suspendCancellableCoroutine { block ->
|
||||||
val coroutine = Coroutine.async(executeContext = IO) {
|
Coroutine.async(executeContext = IO) {
|
||||||
|
val job = launch {
|
||||||
try {
|
try {
|
||||||
val pattern = regex.toPattern()
|
val pattern = regex.toPattern()
|
||||||
val matcher = pattern.matcher(charSequence)
|
val matcher = pattern.matcher(charSequence)
|
||||||
@@ -49,16 +54,18 @@ fun CharSequence.replace(regex: Regex, replacement: String, timeout: Long): Stri
|
|||||||
block.resumeWithException(e)
|
block.resumeWithException(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
handler.postDelayed(timeout) {
|
select {
|
||||||
if (coroutine.isActive) {
|
job.onJoin {}
|
||||||
|
onTimeout(timeout) {
|
||||||
val timeoutMsg =
|
val timeoutMsg =
|
||||||
"替换超时,3秒后还未结束将重启应用\n替换规则$regex\n替换内容:$charSequence"
|
"替换超时,3秒后还未结束将重启应用\n替换规则$regex\n替换内容:$charSequence"
|
||||||
val exception = RegexTimeoutException(timeoutMsg)
|
val exception = RegexTimeoutException(timeoutMsg)
|
||||||
block.cancel(exception)
|
block.cancel(exception)
|
||||||
appCtx.longToastOnUi(timeoutMsg)
|
appCtx.longToastOnUi(timeoutMsg)
|
||||||
CrashHandler.saveCrashInfo2File(exception)
|
CrashHandler.saveCrashInfo2File(exception)
|
||||||
handler.postDelayed(3000) {
|
select {
|
||||||
if (coroutine.isActive) {
|
job.onJoin {}
|
||||||
|
onTimeout(3000) {
|
||||||
appCtx.restart()
|
appCtx.restart()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,5 +73,6 @@ fun CharSequence.replace(regex: Regex, replacement: String, timeout: Long): Stri
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user