mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-01 12:06:10 +08:00
[console] Make JvmPluginLoader a child job of PluginManager
This commit is contained in:
parent
553ea9abbc
commit
b59c7336d0
@ -29,6 +29,7 @@ import net.mamoe.mirai.console.internal.command.CommandManagerImpl
|
|||||||
import net.mamoe.mirai.console.internal.data.builtins.ConsoleDataScopeImpl
|
import net.mamoe.mirai.console.internal.data.builtins.ConsoleDataScopeImpl
|
||||||
import net.mamoe.mirai.console.internal.logging.LoggerControllerImpl
|
import net.mamoe.mirai.console.internal.logging.LoggerControllerImpl
|
||||||
import net.mamoe.mirai.console.internal.plugin.BuiltInJvmPluginLoaderImpl
|
import net.mamoe.mirai.console.internal.plugin.BuiltInJvmPluginLoaderImpl
|
||||||
|
import net.mamoe.mirai.console.internal.plugin.impl
|
||||||
import net.mamoe.mirai.console.internal.pluginManagerImpl
|
import net.mamoe.mirai.console.internal.pluginManagerImpl
|
||||||
import net.mamoe.mirai.console.logging.LoggerController
|
import net.mamoe.mirai.console.logging.LoggerController
|
||||||
import net.mamoe.mirai.console.plugin.Plugin
|
import net.mamoe.mirai.console.plugin.Plugin
|
||||||
@ -350,7 +351,7 @@ public interface MiraiConsoleImplementation : CoroutineScope {
|
|||||||
* @since 2.10.0-RC
|
* @since 2.10.0-RC
|
||||||
*/
|
*/
|
||||||
public fun createDefaultJvmPluginLoader(coroutineContext: CoroutineContext): JvmPluginLoader =
|
public fun createDefaultJvmPluginLoader(coroutineContext: CoroutineContext): JvmPluginLoader =
|
||||||
BuiltInJvmPluginLoaderImpl(coroutineContext)
|
BuiltInJvmPluginLoaderImpl(coroutineContext + MiraiConsole.pluginManager.impl.coroutineContext.job)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @since 2.10.0-RC
|
* @since 2.10.0-RC
|
||||||
|
@ -194,6 +194,22 @@ internal abstract class JvmPluginInternal(
|
|||||||
CoroutineName("Plugin $dataHolderName")
|
CoroutineName("Plugin $dataHolderName")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val pluginParentJob: Job = run {
|
||||||
|
val job = parentCoroutineContext[Job] ?: JvmPluginLoader.coroutineContext[Job]!!
|
||||||
|
|
||||||
|
val pluginManagerJob = MiraiConsole.pluginManager.impl.coroutineContext.job
|
||||||
|
|
||||||
|
val allJobs = generateSequence(sequenceOf(pluginManagerJob)) { parentSeqs ->
|
||||||
|
parentSeqs.flatMap { it.children }
|
||||||
|
}.flatten()
|
||||||
|
|
||||||
|
check(allJobs.contains(job)) {
|
||||||
|
"The parent job of plugin `$id' not a child of PluginManager"
|
||||||
|
}
|
||||||
|
|
||||||
|
job
|
||||||
|
}
|
||||||
|
|
||||||
@JvmField
|
@JvmField
|
||||||
internal val coroutineContextInitializer = {
|
internal val coroutineContextInitializer = {
|
||||||
CoroutineExceptionHandler { context, throwable ->
|
CoroutineExceptionHandler { context, throwable ->
|
||||||
@ -203,16 +219,7 @@ internal abstract class JvmPluginInternal(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
.plus(parentCoroutineContext)
|
.plus(parentCoroutineContext)
|
||||||
.plus(CoroutineName("Plugin ${(this as AbstractJvmPlugin).dataHolderName}"))
|
.plus(SupervisorJob(pluginParentJob))
|
||||||
.plus(
|
|
||||||
SupervisorJob(parentCoroutineContext[Job] ?: JvmPluginLoader.coroutineContext[Job]!!)
|
|
||||||
)
|
|
||||||
.also {
|
|
||||||
if (!MiraiConsole.isActive) return@also
|
|
||||||
JvmPluginLoader.coroutineContext[Job]!!.invokeOnCompletion {
|
|
||||||
this.cancel()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.plus(_intrinsicCoroutineContext)
|
.plus(_intrinsicCoroutineContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user