mirror of
https://github.com/mamoe/mirai.git
synced 2025-04-25 04:50:26 +08:00
Enhance performance: no redundant lambda creation when constructing Bot
This commit is contained in:
parent
e37ace9196
commit
db2bcb4e02
mirai-core-timpc/src
mirai-core/src
mirai-demos/mirai-demo-1/src/main/java/demo/subscribe
@ -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)
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用指定的 [配置][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))
|
@ -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)
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user