From 19132b359b05b39ea41e6f38530dc54427b2e76b Mon Sep 17 00:00:00 2001
From: Karlatemp <kar@kasukusakura.com>
Date: Thu, 29 Jun 2023 13:50:20 +0800
Subject: [PATCH] [core] Provide CoroutineScope to service

---
 .../kotlin/network/components/EncryptServiceHolder.kt     | 5 ++++-
 mirai-core/src/commonMain/kotlin/spi/EncryptService.kt    | 8 +++++++-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/mirai-core/src/commonMain/kotlin/network/components/EncryptServiceHolder.kt b/mirai-core/src/commonMain/kotlin/network/components/EncryptServiceHolder.kt
index 4b36ef8b1..6cc3c382b 100644
--- a/mirai-core/src/commonMain/kotlin/network/components/EncryptServiceHolder.kt
+++ b/mirai-core/src/commonMain/kotlin/network/components/EncryptServiceHolder.kt
@@ -17,6 +17,7 @@ import net.mamoe.mirai.internal.spi.GlobalEncryptServiceUsage
 import net.mamoe.mirai.internal.utils.actualCacheDir
 import net.mamoe.mirai.internal.utils.workingDirPath
 import net.mamoe.mirai.utils.buildTypeSafeMap
+import net.mamoe.mirai.utils.childScope
 
 internal interface EncryptServiceHolder {
     companion object : ComponentKey<EncryptServiceHolder>
@@ -52,7 +53,9 @@ internal class EncryptServiceHolderImpl(
                     set(EncryptServiceContext.KEY_DEVICE_INFO, ssoProcessorContext.device)
                     set(EncryptServiceContext.KEY_BOT_WORKING_DIR, bot.configuration.workingDirPath)
                     set(EncryptServiceContext.KEY_BOT_CACHING_DIR, bot.configuration.actualCacheDir().absolutePath)
-                })
+                }),
+
+                bot.childScope(name = "Encrypt Service"),
             )
             isAvailable = true
         }
diff --git a/mirai-core/src/commonMain/kotlin/spi/EncryptService.kt b/mirai-core/src/commonMain/kotlin/spi/EncryptService.kt
index b7dbcb67a..7f8951045 100644
--- a/mirai-core/src/commonMain/kotlin/spi/EncryptService.kt
+++ b/mirai-core/src/commonMain/kotlin/spi/EncryptService.kt
@@ -10,6 +10,7 @@
 
 package net.mamoe.mirai.internal.spi
 
+import kotlinx.coroutines.CoroutineScope
 import net.mamoe.mirai.Bot
 import net.mamoe.mirai.spi.BaseService
 import net.mamoe.mirai.spi.SpiServiceLoader
@@ -43,7 +44,12 @@ public class EncryptServiceContext @MiraiInternalApi constructor(
  */
 public interface EncryptService : BaseService {
     /** 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)