diff --git a/mirai-console/backend/mirai-console/src/MiraiConsole.kt b/mirai-console/backend/mirai-console/src/MiraiConsole.kt index 736d1abc9..98e9ef02d 100644 --- a/mirai-console/backend/mirai-console/src/MiraiConsole.kt +++ b/mirai-console/backend/mirai-console/src/MiraiConsole.kt @@ -28,7 +28,7 @@ import net.mamoe.mirai.console.plugin.loader.PluginLoader import net.mamoe.mirai.console.util.AnsiMessageBuilder import net.mamoe.mirai.console.util.ConsoleExperimentalApi import net.mamoe.mirai.console.util.ConsoleInternalApi -import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScopeContext +import net.mamoe.mirai.utils.childScopeContext import net.mamoe.mirai.console.util.SemVersion import net.mamoe.mirai.utils.BotConfiguration import net.mamoe.mirai.utils.MiraiLogger diff --git a/mirai-console/backend/mirai-console/src/command/CommandSender.kt b/mirai-console/backend/mirai-console/src/command/CommandSender.kt index f70bd0321..5c51eda96 100644 --- a/mirai-console/backend/mirai-console/src/command/CommandSender.kt +++ b/mirai-console/backend/mirai-console/src/command/CommandSender.kt @@ -29,14 +29,14 @@ import net.mamoe.mirai.console.permission.AbstractPermitteeId import net.mamoe.mirai.console.permission.Permittee import net.mamoe.mirai.console.permission.PermitteeId import net.mamoe.mirai.console.util.ConsoleExperimentalApi -import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope -import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScopeContext import net.mamoe.mirai.console.util.MessageScope import net.mamoe.mirai.contact.* import net.mamoe.mirai.event.events.* import net.mamoe.mirai.message.MessageReceipt import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.message.data.PlainText +import net.mamoe.mirai.utils.childScope +import net.mamoe.mirai.utils.childScopeContext import kotlin.contracts.InvocationKind import kotlin.contracts.contract import kotlin.coroutines.CoroutineContext diff --git a/mirai-console/backend/mirai-console/src/internal/command/CommandManagerImpl.kt b/mirai-console/backend/mirai-console/src/internal/command/CommandManagerImpl.kt index aa258d219..b14d9b939 100644 --- a/mirai-console/backend/mirai-console/src/internal/command/CommandManagerImpl.kt +++ b/mirai-console/backend/mirai-console/src/internal/command/CommandManagerImpl.kt @@ -23,7 +23,7 @@ import net.mamoe.mirai.console.command.resolve.CommandCallResolver.Companion.res import net.mamoe.mirai.console.command.resolve.getOrElse import net.mamoe.mirai.console.internal.util.ifNull import net.mamoe.mirai.console.permission.PermissionService.Companion.testPermission -import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope +import net.mamoe.mirai.utils.childScope import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.message.data.toMessageChain import net.mamoe.mirai.utils.MiraiLogger diff --git a/mirai-console/backend/mirai-console/src/internal/data/builtins/ConsoleDataScope.kt b/mirai-console/backend/mirai-console/src/internal/data/builtins/ConsoleDataScope.kt index 11a8ec50f..1a513772c 100644 --- a/mirai-console/backend/mirai-console/src/internal/data/builtins/ConsoleDataScope.kt +++ b/mirai-console/backend/mirai-console/src/internal/data/builtins/ConsoleDataScope.kt @@ -16,7 +16,7 @@ import net.mamoe.mirai.console.data.PluginConfig import net.mamoe.mirai.console.data.PluginData import net.mamoe.mirai.console.data.PluginDataStorage import net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge -import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope +import net.mamoe.mirai.utils.childScope import net.mamoe.mirai.utils.minutesToMillis diff --git a/mirai-console/backend/mirai-console/src/internal/plugin/BuiltInJvmPluginLoaderImpl.kt b/mirai-console/backend/mirai-console/src/internal/plugin/BuiltInJvmPluginLoaderImpl.kt index 684c53846..b74ec3bdd 100644 --- a/mirai-console/backend/mirai-console/src/internal/plugin/BuiltInJvmPluginLoaderImpl.kt +++ b/mirai-console/backend/mirai-console/src/internal/plugin/BuiltInJvmPluginLoaderImpl.kt @@ -22,7 +22,7 @@ import net.mamoe.mirai.console.plugin.jvm.* import net.mamoe.mirai.console.plugin.loader.AbstractFilePluginLoader import net.mamoe.mirai.console.plugin.loader.PluginLoadException import net.mamoe.mirai.console.plugin.name -import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope +import net.mamoe.mirai.utils.childScope import net.mamoe.mirai.utils.MiraiLogger import net.mamoe.mirai.utils.verbose import java.io.File diff --git a/mirai-console/backend/mirai-console/src/internal/plugin/PluginManagerImpl.kt b/mirai-console/backend/mirai-console/src/internal/plugin/PluginManagerImpl.kt index 0faebb324..442de6489 100644 --- a/mirai-console/backend/mirai-console/src/internal/plugin/PluginManagerImpl.kt +++ b/mirai-console/backend/mirai-console/src/internal/plugin/PluginManagerImpl.kt @@ -27,8 +27,8 @@ import net.mamoe.mirai.console.plugin.jvm.JvmPlugin import net.mamoe.mirai.console.plugin.loader.PluginLoadException import net.mamoe.mirai.console.plugin.loader.PluginLoader import net.mamoe.mirai.console.plugin.name -import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope import net.mamoe.mirai.console.util.SemVersion +import net.mamoe.mirai.utils.childScope import net.mamoe.mirai.utils.info import java.io.File import java.nio.file.Path diff --git a/mirai-console/backend/mirai-console/src/internal/util/JavaPluginSchedulerImpl.kt b/mirai-console/backend/mirai-console/src/internal/util/JavaPluginSchedulerImpl.kt index 213025999..f0d5d1448 100644 --- a/mirai-console/backend/mirai-console/src/internal/util/JavaPluginSchedulerImpl.kt +++ b/mirai-console/backend/mirai-console/src/internal/util/JavaPluginSchedulerImpl.kt @@ -12,7 +12,7 @@ package net.mamoe.mirai.console.internal.util import kotlinx.coroutines.* import kotlinx.coroutines.future.future import net.mamoe.mirai.console.plugin.jvm.JavaPluginScheduler -import net.mamoe.mirai.console.util.CoroutineScopeUtils.overrideWithSupervisorJob +import net.mamoe.mirai.utils.newCoroutineContextWithSupervisorJob 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.overrideWithSupervisorJob(this.toString()) + parentCoroutineContext.newCoroutineContextWithSupervisorJob(this.toString()) override fun repeating(intervalMs: Long, runnable: Runnable): Future { return this.future { diff --git a/mirai-console/backend/mirai-console/src/util/CoroutineScopeUtils.kt b/mirai-console/backend/mirai-console/src/util/CoroutineScopeUtils.kt index afd3fc17c..485ac8509 100644 --- a/mirai-console/backend/mirai-console/src/util/CoroutineScopeUtils.kt +++ b/mirai-console/backend/mirai-console/src/util/CoroutineScopeUtils.kt @@ -13,17 +13,25 @@ import kotlinx.atomicfu.atomic import kotlinx.atomicfu.loop import kotlinx.coroutines.* import net.mamoe.mirai.console.internal.util.runIgnoreException +import net.mamoe.mirai.utils.DeprecatedSinceMirai import net.mamoe.mirai.utils.currentTimeMillis import kotlin.coroutines.CoroutineContext import kotlin.coroutines.EmptyCoroutineContext +@Suppress("DEPRECATION", "DeprecatedCallableAddReplaceWith") +@Deprecated("No longer supported, deprecated for removal.") +@DeprecatedSinceMirai(warningSince = "2.10.0-RC") @ConsoleExperimentalApi public object CoroutineScopeUtils { + @Deprecated("No longer supported, deprecated for removal.") + @DeprecatedSinceMirai(warningSince = "2.10.0-RC") @JvmStatic @ConsoleExperimentalApi public fun CoroutineContext.overrideWithSupervisorJob(name: String? = null): CoroutineContext = this + NamedSupervisorJob(name ?: "", this[Job]) + @Deprecated("No longer supported, deprecated for removal.") + @DeprecatedSinceMirai(warningSince = "2.10.0-RC") @JvmStatic @ConsoleExperimentalApi public fun CoroutineScope.childScope( @@ -32,6 +40,8 @@ public object CoroutineScopeUtils { ): CoroutineScope = CoroutineScope(this.childScopeContext(name, context)) + @Deprecated("No longer supported, deprecated for removal.") + @DeprecatedSinceMirai(warningSince = "2.10.0-RC") @JvmStatic @ConsoleExperimentalApi public fun CoroutineScope.childScopeContext( @@ -90,6 +100,8 @@ internal fun CoroutineScope.launchTimedTask( action: suspend CoroutineScope.() -> Unit, ) = TimedTask(this, coroutineContext, intervalMillis, action) +@Deprecated("No longer supported, deprecated for removal.") +@DeprecatedSinceMirai(warningSince = "2.10.0-RC") @ConsoleExperimentalApi public class NamedSupervisorJob @JvmOverloads constructor( private val name: String, diff --git a/mirai-console/frontend/mirai-console-terminal/build.gradle.kts b/mirai-console/frontend/mirai-console-terminal/build.gradle.kts index 695527c91..58681a7b4 100644 --- a/mirai-console/frontend/mirai-console-terminal/build.gradle.kts +++ b/mirai-console/frontend/mirai-console-terminal/build.gradle.kts @@ -20,6 +20,7 @@ dependencies { compileAndTestRuntime(project(":mirai-console")) compileAndTestRuntime(project(":mirai-core-api")) + compileAndTestRuntime(project(":mirai-core-utils")) compileAndTestRuntime(kotlin("stdlib-jdk8", Versions.kotlinStdlib)) // must specify `compileOnly` explicitly testApi(project(":mirai-core")) diff --git a/mirai-console/frontend/mirai-console-terminal/src/MiraiConsoleImplementationTerminal.kt b/mirai-console/frontend/mirai-console-terminal/src/MiraiConsoleImplementationTerminal.kt index 61fc9fc82..4f4190b9d 100644 --- a/mirai-console/frontend/mirai-console-terminal/src/MiraiConsoleImplementationTerminal.kt +++ b/mirai-console/frontend/mirai-console-terminal/src/MiraiConsoleImplementationTerminal.kt @@ -22,10 +22,7 @@ package net.mamoe.mirai.console.terminal -import kotlinx.coroutines.CancellationException -import kotlinx.coroutines.CoroutineExceptionHandler -import kotlinx.coroutines.CoroutineName -import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.* import net.mamoe.mirai.console.ConsoleFrontEndImplementation import net.mamoe.mirai.console.MiraiConsole import net.mamoe.mirai.console.MiraiConsoleFrontEndDescription @@ -37,7 +34,10 @@ import net.mamoe.mirai.console.plugin.loader.PluginLoader import net.mamoe.mirai.console.terminal.ConsoleInputImpl.requestInput import net.mamoe.mirai.console.terminal.noconsole.AllEmptyLineReader import net.mamoe.mirai.console.terminal.noconsole.NoConsole -import net.mamoe.mirai.console.util.* +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.SemVersion import net.mamoe.mirai.utils.* import org.fusesource.jansi.Ansi import org.jline.reader.LineReader @@ -66,7 +66,7 @@ open class MiraiConsoleImplementationTerminal override val configStorageForJvmPluginLoader: PluginDataStorage = MultiFilePluginDataStorage(rootPath.resolve("config")), override val configStorageForBuiltIns: PluginDataStorage = MultiFilePluginDataStorage(rootPath.resolve("config")), ) : MiraiConsoleImplementation, CoroutineScope by CoroutineScope( - NamedSupervisorJob("MiraiConsoleImplementationTerminal") + + SupervisorJob() + CoroutineName("MiraiConsoleImplementationTerminal") + CoroutineExceptionHandler { coroutineContext, throwable -> if (throwable is CancellationException) { return@CoroutineExceptionHandler diff --git a/mirai-console/frontend/mirai-console-terminal/src/MiraiConsoleTerminalLoader.kt b/mirai-console/frontend/mirai-console-terminal/src/MiraiConsoleTerminalLoader.kt index 1c2fcf31e..523587330 100644 --- a/mirai-console/frontend/mirai-console-terminal/src/MiraiConsoleTerminalLoader.kt +++ b/mirai-console/frontend/mirai-console-terminal/src/MiraiConsoleTerminalLoader.kt @@ -29,9 +29,9 @@ import net.mamoe.mirai.console.data.AutoSavePluginDataHolder import net.mamoe.mirai.console.terminal.noconsole.SystemOutputPrintStream 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.MiraiLogger +import net.mamoe.mirai.utils.childScope import java.io.FileDescriptor import java.io.FileOutputStream import java.io.PrintStream diff --git a/mirai-core-utils/src/commonMain/kotlin/CoroutineUtils.kt b/mirai-core-utils/src/commonMain/kotlin/CoroutineUtils.kt index a1086b250..77afab183 100644 --- a/mirai-core-utils/src/commonMain/kotlin/CoroutineUtils.kt +++ b/mirai-core-utils/src/commonMain/kotlin/CoroutineUtils.kt @@ -98,6 +98,36 @@ public fun CoroutineScope.hierarchicalName( name: String, ): CoroutineName = this.coroutineContext.hierarchicalName(name) +public fun CoroutineContext.newCoroutineContextWithSupervisorJob(name: String? = null): CoroutineContext = + this + CoroutineName(name ?: "") + SupervisorJob(this[Job]) + +public fun CoroutineScope.childScope( + name: String? = null, + context: CoroutineContext = EmptyCoroutineContext +): CoroutineScope = + CoroutineScope(this.childScopeContext(name, context)) + +public fun CoroutineContext.childScope( + name: String? = null, + context: CoroutineContext = EmptyCoroutineContext +): CoroutineScope = + CoroutineScope(this.childScopeContext(name, context)) + +public fun CoroutineScope.childScopeContext( + name: String? = null, + context: CoroutineContext = EmptyCoroutineContext +): CoroutineContext = + this.coroutineContext.childScopeContext(name, context) + +public fun CoroutineContext.childScopeContext( + name: String? = null, + context: CoroutineContext = EmptyCoroutineContext +): CoroutineContext = + this.newCoroutineContextWithSupervisorJob(name) + context.let { + if (name != null) it + CoroutineName(name) + else it + } + public inline fun runUnwrapCancellationException(block: () -> R): R { try { return block()