Use suspendCancellableCoroutine, improve performance

This commit is contained in:
Him188 2020-04-30 11:46:38 +08:00
parent 0d66a9959a
commit 01fc3f4c32

View File

@ -6,6 +6,7 @@ import javafx.stage.Modality
import javafx.stage.StageStyle
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.suspendCancellableCoroutine
import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.command.CommandManager
import net.mamoe.mirai.console.command.CommandManager.runCommand
@ -16,12 +17,13 @@ import net.mamoe.mirai.console.graphical.view.dialog.InputDialog
import net.mamoe.mirai.console.graphical.view.dialog.VerificationCodeFragment
import net.mamoe.mirai.console.plugins.PluginManager
import net.mamoe.mirai.console.utils.MiraiConsoleUI
import net.mamoe.mirai.network.WrongPasswordException
import net.mamoe.mirai.network.CustomLoginFailedException
import net.mamoe.mirai.utils.LoginSolver
import net.mamoe.mirai.utils.SimpleLogger.LogPriority
import tornadofx.*
import java.text.SimpleDateFormat
import java.util.*
import kotlin.coroutines.resume
class MiraiGraphicalUIController : Controller(), MiraiConsoleUI {
@ -105,17 +107,11 @@ class MiraiGraphicalUIController : Controller(), MiraiConsoleUI {
}
}
override suspend fun requestInput(hint: String): String {
var ret: String? = null
// UI必须在UI线程执行requestInput在协程种被调用
override suspend fun requestInput(hint: String): String =
suspendCancellableCoroutine {
Platform.runLater {
ret = InputDialog(hint).open()
it.resume(InputDialog(hint).open())
}
while (ret == null) {
delay(1000)
}
return ret!!
}
override fun pushBotAdminStatus(identity: Long, admins: List<Long>) = Platform.runLater {