[core] Provide CoroutineScope to service

This commit is contained in:
Karlatemp 2023-06-29 13:50:20 +08:00
parent c080cd19f2
commit 19132b359b
No known key found for this signature in database
GPG Key ID: BA173CA2B9956C59
2 changed files with 11 additions and 2 deletions

View File

@ -17,6 +17,7 @@ import net.mamoe.mirai.internal.spi.GlobalEncryptServiceUsage
import net.mamoe.mirai.internal.utils.actualCacheDir import net.mamoe.mirai.internal.utils.actualCacheDir
import net.mamoe.mirai.internal.utils.workingDirPath import net.mamoe.mirai.internal.utils.workingDirPath
import net.mamoe.mirai.utils.buildTypeSafeMap import net.mamoe.mirai.utils.buildTypeSafeMap
import net.mamoe.mirai.utils.childScope
internal interface EncryptServiceHolder { internal interface EncryptServiceHolder {
companion object : ComponentKey<EncryptServiceHolder> companion object : ComponentKey<EncryptServiceHolder>
@ -52,7 +53,9 @@ internal class EncryptServiceHolderImpl(
set(EncryptServiceContext.KEY_DEVICE_INFO, ssoProcessorContext.device) set(EncryptServiceContext.KEY_DEVICE_INFO, ssoProcessorContext.device)
set(EncryptServiceContext.KEY_BOT_WORKING_DIR, bot.configuration.workingDirPath) set(EncryptServiceContext.KEY_BOT_WORKING_DIR, bot.configuration.workingDirPath)
set(EncryptServiceContext.KEY_BOT_CACHING_DIR, bot.configuration.actualCacheDir().absolutePath) set(EncryptServiceContext.KEY_BOT_CACHING_DIR, bot.configuration.actualCacheDir().absolutePath)
}) }),
bot.childScope(name = "Encrypt Service"),
) )
isAvailable = true isAvailable = true
} }

View File

@ -10,6 +10,7 @@
package net.mamoe.mirai.internal.spi package net.mamoe.mirai.internal.spi
import kotlinx.coroutines.CoroutineScope
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.spi.BaseService import net.mamoe.mirai.spi.BaseService
import net.mamoe.mirai.spi.SpiServiceLoader import net.mamoe.mirai.spi.SpiServiceLoader
@ -43,7 +44,12 @@ public class EncryptServiceContext @MiraiInternalApi constructor(
*/ */
public interface EncryptService : BaseService { public interface EncryptService : BaseService {
/** service per bot */ /** service per bot */
public fun attachToBot(context: EncryptServiceContext): EncryptService = this public fun attachToBot(context: EncryptServiceContext, serviceSubScope: CoroutineScope): EncryptService {
/* cleanup:
serviceSubScope.coroutineContext.job.invokeOnCompletion { }
*/
return this
}
public fun initialize(context: EncryptServiceContext) public fun initialize(context: EncryptServiceContext)