Skip to content

Commit fa389f4

Browse files
committed
Refactor attachToBot to Factory
1 parent 787ee83 commit fa389f4

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

mirai-core/src/commonMain/kotlin/network/components/EncryptServiceHolder.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import net.mamoe.mirai.internal.AbstractBot
1313
import net.mamoe.mirai.internal.network.component.ComponentKey
1414
import net.mamoe.mirai.internal.spi.EncryptService
1515
import net.mamoe.mirai.internal.spi.EncryptServiceContext
16-
import net.mamoe.mirai.internal.spi.GlobalEncryptServiceUsage
1716
import net.mamoe.mirai.internal.utils.actualCacheDir
1817
import net.mamoe.mirai.internal.utils.workingDirPath
1918
import net.mamoe.mirai.utils.buildTypeSafeMap
@@ -45,9 +44,8 @@ internal class EncryptServiceHolderImpl(
4544
get() = service0 ?: error("Encrypt Service not available")
4645

4746
init {
48-
@OptIn(GlobalEncryptServiceUsage::class)
49-
EncryptService.instance?.let { globalService ->
50-
service0 = globalService.attachToBot(
47+
EncryptService.factory?.let { globalService ->
48+
service0 = globalService.createForBot(
5149
EncryptServiceContext(bot.id, buildTypeSafeMap {
5250
set(EncryptServiceContext.KEY_BOT_PROTOCOL, bot.configuration.protocol)
5351
set(EncryptServiceContext.KEY_DEVICE_INFO, ssoProcessorContext.device)

mirai-core/src/commonMain/kotlin/spi/EncryptService.kt

+15-16
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,7 @@ public class EncryptServiceContext @MiraiInternalApi constructor(
4242
/**
4343
* @since 2.15.0
4444
*/
45-
public interface EncryptService : BaseService {
46-
/** service per bot */
47-
public fun attachToBot(context: EncryptServiceContext, serviceSubScope: CoroutineScope): EncryptService {
48-
/* cleanup:
49-
serviceSubScope.coroutineContext.job.invokeOnCompletion { }
50-
*/
51-
return this
52-
}
53-
45+
public interface EncryptService {
5446
public fun initialize(context: EncryptServiceContext)
5547

5648
/**
@@ -91,8 +83,20 @@ public interface EncryptService : BaseService {
9183
public suspend fun sendMessage(remark: String, commandName: String, uin: Long, data: ByteArray): ChannelResult?
9284
}
9385

86+
// net.mamoe.mirai.internal.spi.EncryptService$Factory
87+
public interface Factory : BaseService {
88+
89+
/*
90+
* cleanup:
91+
* serviceSubScope.coroutineContext.job.invokeOnCompletion { }
92+
*/
93+
public fun createForBot(context: EncryptServiceContext, serviceSubScope: CoroutineScope): EncryptService
94+
}
95+
96+
9497
public companion object {
95-
private val loader = SpiServiceLoader(EncryptService::class)
98+
99+
private val loader = SpiServiceLoader(Factory::class)
96100

97101
private val warningAlert: Unit by lazy {
98102
val log = MiraiLogger.Factory.create(EncryptService::class, "EncryptService.alert")
@@ -113,15 +117,10 @@ public interface EncryptService : BaseService {
113117

114118
}
115119

116-
@GlobalEncryptServiceUsage
117-
internal val instance: EncryptService?
120+
internal val factory: Factory?
118121
get() {
119122
warningAlert
120123
return loader.service
121124
}
122125
}
123-
124126
}
125-
126-
@RequiresOptIn(message = "Global encrypt service used", level = RequiresOptIn.Level.ERROR)
127-
internal annotation class GlobalEncryptServiceUsage

0 commit comments

Comments
 (0)