Commit db2bcb4e authored by Him188's avatar Him188

Enhance performance: no redundant lambda creation when constructing Bot

parent e37ace91
......@@ -15,6 +15,5 @@ object TIMPC : BotFactory {
/**
* 使用指定的 [配置][configuration] 构造 [Bot] 实例
*/
override fun Bot(account: BotAccount, configuration: (BotConfiguration.() -> Unit)?): Bot =
TIMPCBot(account, if (configuration == null) BotConfiguration.Default else BotConfiguration().apply(configuration))
override fun Bot(account: BotAccount, configuration: BotConfiguration): Bot = TIMPCBot(account, configuration)
}
\ No newline at end of file
......@@ -382,7 +382,7 @@ when (idHex.substring(0, 5)) {
internal object DebugNetworkHandler : BotNetworkHandler(), CoroutineScope {
override val supervisor: CompletableJob = SupervisorJob()
override val bot: Bot = TIMPC.Bot(qq ?: 0L, "", null)
override val bot: Bot = TIMPC.Bot(qq ?: 0L, "")
override suspend fun login() {}
......
......@@ -15,11 +15,23 @@ interface BotFactory {
/**
* 使用指定的 [配置][configuration] 构造 [Bot] 实例
*/
fun Bot(account: BotAccount, configuration: (BotConfiguration.() -> Unit)? = null): Bot
fun Bot(account: BotAccount, configuration: BotConfiguration = BotConfiguration.Default): Bot
/**
* 使用指定的 [配置][configuration] 构造 [Bot] 实例
*/
fun Bot(qq: Long, password: String, configuration: (BotConfiguration.() -> Unit)? = null): Bot =
fun Bot(qq: Long, password: String, configuration: BotConfiguration = BotConfiguration.Default): Bot =
this.Bot(BotAccount(qq, password), configuration)
}
\ No newline at end of file
}
/**
* 使用指定的 [配置][configuration] 构造 [Bot] 实例
*/
inline fun BotFactory.Bot(account: BotAccount, configuration: (BotConfiguration.() -> Unit)): Bot=
this.Bot(account, BotConfiguration().apply(configuration))
/**
* 使用指定的 [配置][configuration] 构造 [Bot] 实例
*/
inline fun BotFactory.Bot(qq: Long, password: String, configuration: (BotConfiguration.() -> Unit)): Bot =
this.Bot(qq, password, BotConfiguration().apply(configuration))
\ No newline at end of file
......@@ -5,7 +5,8 @@ package net.mamoe.mirai
import net.mamoe.mirai.utils.BotConfiguration
// Do not use ServiceLoader. Probably not working on MPP
private val factory = run {
@PublishedApi
internal val factory: BotFactory = run {
try {
Class.forName("net.mamoe.mirai.timpc.TIMPC").kotlin.objectInstance as BotFactory
} catch (ignored: Exception) {
......@@ -23,11 +24,17 @@ private val factory = run {
/**
* 加载现有协议的 [BotFactory], 并使用指定的 [配置][configuration] 构造 [Bot] 实例
*/
fun Bot(account: BotAccount, configuration: (BotConfiguration.() -> Unit)? = null): Bot =
fun Bot(account: BotAccount, configuration: BotConfiguration = BotConfiguration.Default): Bot =
factory.Bot(account, configuration)
/**
* 加载现有协议的 [BotFactory], 并使用指定的 [配置][configuration] 构造 [Bot] 实例
*/
fun Bot(qq: Long, password: String, configuration: (BotConfiguration.() -> Unit)? = null): Bot =
inline fun Bot(account: BotAccount, configuration: (BotConfiguration.() -> Unit)): Bot =
factory.Bot(account, configuration)
/**
* 加载现有协议的 [BotFactory], 并使用指定的 [配置][configuration] 构造 [Bot] 实例
*/
inline fun Bot(qq: Long, password: String, configuration: (BotConfiguration.() -> Unit)): Bot =
factory.Bot(qq, password, configuration)
\ No newline at end of file
......@@ -38,7 +38,7 @@ private fun readTestAccount(): BotAccount? {
@Suppress("UNUSED_VARIABLE")
suspend fun main() {
val bot = TIMPC.Bot(
val bot = TIMPC.Bot( // JVM 下也可以不写 `TIMPC.` 引用顶层函数
readTestAccount() ?: BotAccount(//填写你的账号
id = 1994701121,
passwordPlainText = "123456"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment