kunfei 4 лет назад
Родитель
Сommit
82715fe127
1 измененных файлов с 4 добавлено и 2 удалено
  1. 4 2
      app/src/main/java/io/legado/app/utils/RegexExtensions.kt

+ 4 - 2
app/src/main/java/io/legado/app/utils/RegexExtensions.kt

@@ -9,6 +9,8 @@ import kotlinx.coroutines.suspendCancellableCoroutine
 import splitties.init.appCtx
 import kotlin.coroutines.resume
 
+private val scope = MainScope()
+
 /**
  * 带有超时检测的正则替换
  * 超时重启apk,线程不能强制结束,只能重启apk
@@ -16,7 +18,7 @@ import kotlin.coroutines.resume
 suspend fun CharSequence.replace(regex: Regex, replacement: String, timeout: Long): String {
     val charSequence = this
     return suspendCancellableCoroutine { block ->
-        val scope = MainScope().launch(IO) {
+        val job = scope.launch(IO) {
             try {
                 val result = regex.replace(charSequence, replacement)
                 block.resume(result)
@@ -25,7 +27,7 @@ suspend fun CharSequence.replace(regex: Regex, replacement: String, timeout: Lon
             }
         }
         mainHandler.postDelayed({
-            if (scope.isActive) {
+            if (job.isActive) {
                 val timeoutMsg = "替换超时,将在3秒后重启应用\n替换规则$regex\n替换内容:${this}"
                 val exception = RegexTimeoutException(timeoutMsg)
                 block.cancel(exception)