Expose CoroutineScopeUtils to public

This commit is contained in:
Him188 2020-09-02 20:20:26 +08:00
parent 1dcff8149c
commit decf1920b4
5 changed files with 58 additions and 37 deletions

View File

@ -20,6 +20,7 @@ import net.mamoe.mirai.console.plugin.PluginManager
import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.safeLoader
import net.mamoe.mirai.console.plugin.ResourceContainer.Companion.asResourceContainer
import net.mamoe.mirai.console.plugin.jvm.JvmPlugin
import net.mamoe.mirai.console.util.NamedSupervisorJob
import net.mamoe.mirai.utils.MiraiLogger
import java.io.File
import java.io.InputStream
@ -159,16 +160,6 @@ internal abstract class JvmPluginInternal(
// endregion
}
@Suppress("FunctionName")
internal class NamedSupervisorJob(
private val name: String,
parent: Job? = null
) : CompletableJob by SupervisorJob(parent) {
override fun toString(): String {
return "NamedSupervisorJob($name)"
}
}
internal inline fun AtomicLong.updateWhen(condition: (Long) -> Boolean, update: (Long) -> Long): Boolean {
while (true) {
val current = value

View File

@ -11,8 +11,8 @@ package net.mamoe.mirai.console.internal.util
import kotlinx.coroutines.*
import kotlinx.coroutines.future.future
import net.mamoe.mirai.console.internal.plugin.NamedSupervisorJob
import net.mamoe.mirai.console.plugin.jvm.JavaPluginScheduler
import net.mamoe.mirai.console.util.CoroutineScopeUtils.overrideWithSupervisorJob
import java.util.concurrent.Callable
import java.util.concurrent.CompletableFuture
import java.util.concurrent.Future
@ -21,7 +21,7 @@ import kotlin.coroutines.CoroutineContext
internal class JavaPluginSchedulerImpl internal constructor(parentCoroutineContext: CoroutineContext) : CoroutineScope,
JavaPluginScheduler {
override val coroutineContext: CoroutineContext =
parentCoroutineContext + NamedSupervisorJob(this.toString(), parentCoroutineContext[Job])
parentCoroutineContext.overrideWithSupervisorJob(this.toString())
override fun repeating(intervalMs: Long, runnable: Runnable): Future<Void?> {
return this.future {

View File

@ -11,27 +11,43 @@
package net.mamoe.mirai.console.util
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import net.mamoe.mirai.console.internal.plugin.NamedSupervisorJob
import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
internal fun CoroutineContext.overrideWithSupervisorJob(name: String? = null): CoroutineContext =
this + NamedSupervisorJob(name ?: "<unnamed>", this[Job])
@ConsoleExperimentalAPI
public object CoroutineScopeUtils {
@JvmStatic
@ConsoleExperimentalAPI
public fun CoroutineContext.overrideWithSupervisorJob(name: String? = null): CoroutineContext =
this + NamedSupervisorJob(name ?: "<unnamed>", this[Job])
internal fun CoroutineScope.childScope(
name: String? = null,
context: CoroutineContext = EmptyCoroutineContext
): CoroutineScope =
CoroutineScope(this.childScopeContext(name, context))
@JvmStatic
@ConsoleExperimentalAPI
public fun CoroutineScope.childScope(
name: String? = null,
context: CoroutineContext = EmptyCoroutineContext
): CoroutineScope =
CoroutineScope(this.childScopeContext(name, context))
internal fun CoroutineScope.childScopeContext(
name: String? = null,
context: CoroutineContext = EmptyCoroutineContext
): CoroutineContext =
this.coroutineContext.overrideWithSupervisorJob(name) + context.let {
if (name != null) it + CoroutineName(name)
else it
@JvmStatic
@ConsoleExperimentalAPI
public fun CoroutineScope.childScopeContext(
name: String? = null,
context: CoroutineContext = EmptyCoroutineContext
): CoroutineContext =
this.coroutineContext.overrideWithSupervisorJob(name) + context.let {
if (name != null) it + CoroutineName(name)
else it
}
}
@ConsoleExperimentalAPI
public class NamedSupervisorJob @JvmOverloads constructor(
private val name: String,
parent: Job? = null
) : CompletableJob by SupervisorJob(parent) {
override fun toString(): String {
return "NamedSupervisorJob($name)"
}
}

View File

@ -30,7 +30,6 @@ import net.mamoe.mirai.console.MiraiConsoleFrontEndDescription
import net.mamoe.mirai.console.MiraiConsoleImplementation
import net.mamoe.mirai.console.data.MultiFilePluginDataStorage
import net.mamoe.mirai.console.data.PluginDataStorage
import net.mamoe.mirai.console.internal.plugin.NamedSupervisorJob
import net.mamoe.mirai.console.plugin.DeferredPluginLoader
import net.mamoe.mirai.console.plugin.PluginLoader
import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader
@ -38,6 +37,7 @@ import net.mamoe.mirai.console.pure.ConsoleInputImpl.requestInput
import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
import net.mamoe.mirai.console.util.ConsoleInput
import net.mamoe.mirai.console.util.ConsoleInternalAPI
import net.mamoe.mirai.console.util.NamedSupervisorJob
import net.mamoe.mirai.utils.*
import org.fusesource.jansi.Ansi
import org.jline.reader.LineReader
@ -71,11 +71,12 @@ class MiraiConsoleImplementationPure
override val dataStorageForBuiltIns: PluginDataStorage = MultiFilePluginDataStorage(rootPath.resolve("data")),
override val configStorageForJarPluginLoader: PluginDataStorage = MultiFilePluginDataStorage(rootPath.resolve("config")),
override val configStorageForBuiltIns: PluginDataStorage = MultiFilePluginDataStorage(rootPath.resolve("config"))
) : MiraiConsoleImplementation, CoroutineScope by CoroutineScope(NamedSupervisorJob("MiraiConsoleImplementationPure") +
CoroutineExceptionHandler { coroutineContext, throwable ->
val coroutineName = coroutineContext[CoroutineName]?.name ?: "<unnamed>"
MiraiConsole.mainLogger.error("Exception in coroutine $coroutineName", throwable)
}) {
) : MiraiConsoleImplementation, CoroutineScope by CoroutineScope(
NamedSupervisorJob("MiraiConsoleImplementationPure") +
CoroutineExceptionHandler { coroutineContext, throwable ->
val coroutineName = coroutineContext[CoroutineName]?.name ?: "<unnamed>"
MiraiConsole.mainLogger.error("Exception in coroutine $coroutineName", throwable)
}) {
override val consoleInput: ConsoleInput get() = ConsoleInputImpl
override fun createLoginSolver(requesterBot: Long, configuration: BotConfiguration): LoginSolver {

View File

@ -14,21 +14,24 @@
"INVISIBLE_SETTER",
"INVISIBLE_GETTER",
"INVISIBLE_ABSTRACT_MEMBER_FROM_SUPER",
"INVISIBLE_ABSTRACT_MEMBER_FROM_SUPE_WARNING"
)
@file:OptIn(ConsoleInternalAPI::class)
package net.mamoe.mirai.console.pure
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.runBlocking
import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.MiraiConsoleImplementation
import net.mamoe.mirai.console.MiraiConsoleImplementation.Companion.start
import net.mamoe.mirai.console.data.AutoSavePluginDataHolder
import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
import net.mamoe.mirai.console.util.ConsoleInternalAPI
import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope
import net.mamoe.mirai.message.data.Message
import net.mamoe.mirai.utils.DefaultLogger
import net.mamoe.mirai.utils.minutesToMillis
import java.io.PrintStream
/**
@ -56,6 +59,16 @@ object MiraiConsolePureLoader {
}
}
internal object ConsoleDataHolder : AutoSavePluginDataHolder,
CoroutineScope by MiraiConsole.childScope("ConsoleDataHolder") {
@ConsoleExperimentalAPI
override val autoSaveIntervalMillis: LongRange = 1.minutesToMillis..10.minutesToMillis
@ConsoleExperimentalAPI
override val dataHolderName: String
get() = "Pure"
}
internal fun overrideSTD() {
System.setOut(
PrintStream(