diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt index aad6f4bb4..ab0de0ac4 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt @@ -25,7 +25,7 @@ import net.mamoe.mirai.console.command.ConsoleCommandSender import net.mamoe.mirai.console.data.PluginDataStorage import net.mamoe.mirai.console.internal.plugin.CuiPluginCenter import net.mamoe.mirai.console.internal.plugin.PluginManagerImpl -import net.mamoe.mirai.console.internal.utils.ConsoleBuiltInPluginDataStorage +import net.mamoe.mirai.console.internal.util.ConsoleBuiltInPluginDataStorage import net.mamoe.mirai.console.plugin.PluginLoader import net.mamoe.mirai.console.plugin.PluginManager import net.mamoe.mirai.console.plugin.center.PluginCenter diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/utils/BotManagerImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/util/BotManagerImpl.kt similarity index 98% rename from backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/utils/BotManagerImpl.kt rename to backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/util/BotManagerImpl.kt index 85b6347b3..0d5c44c8f 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/utils/BotManagerImpl.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/util/BotManagerImpl.kt @@ -9,7 +9,7 @@ @file:Suppress("MemberVisibilityCanBePrivate") -package net.mamoe.mirai.console.internal.utils +package net.mamoe.mirai.console.internal.util import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/util/JavaPluginSchedulerImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/util/JavaPluginSchedulerImpl.kt new file mode 100644 index 000000000..fcbbf5ead --- /dev/null +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/util/JavaPluginSchedulerImpl.kt @@ -0,0 +1,65 @@ +/* + * Copyright 2019-2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 with Mamoe Exceptions 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 with Mamoe Exceptions license that can be found via the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.console.internal.util + +import kotlinx.coroutines.* +import kotlinx.coroutines.future.future +import net.mamoe.mirai.console.util.JavaPluginScheduler +import java.util.concurrent.Callable +import java.util.concurrent.CompletableFuture +import java.util.concurrent.Future +import kotlin.coroutines.CoroutineContext + +internal class JavaPluginSchedulerImpl internal constructor(parentCoroutineContext: CoroutineContext) : CoroutineScope, + JavaPluginScheduler { + override val coroutineContext: CoroutineContext = + parentCoroutineContext + SupervisorJob(parentCoroutineContext[Job]) + + override fun repeating(intervalMs: Long, runnable: Runnable): Future { + return this.future { + while (isActive) { + withContext(Dispatchers.IO) { runnable.run() } + delay(intervalMs) + } + null + } + } + + override fun delayed(delayMillis: Long, runnable: Runnable): CompletableFuture { + return future { + delay(delayMillis) + withContext(Dispatchers.IO) { + runnable.run() + } + null + } + } + + override fun delayed(delayMillis: Long, runnable: Callable): CompletableFuture { + return future { + delay(delayMillis) + withContext(Dispatchers.IO) { runnable.call() } + null + } + } + + override fun async(supplier: Callable): Future { + return future { + withContext(Dispatchers.IO) { supplier.call() } + } + } + + override fun async(runnable: Runnable): Future { + return future { + withContext(Dispatchers.IO) { runnable.run() } + null + } + } +} \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/Annotations.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/Annotations.kt index 0a4b1f632..8f4776b45 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/Annotations.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/Annotations.kt @@ -7,6 +7,9 @@ * https://github.com/mamoe/mirai/blob/master/LICENSE */ +@file:JvmMultifileClass +@file:JvmName("ConsoleUtils") + package net.mamoe.mirai.console.util import kotlin.annotation.AnnotationTarget.* diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/BotManager.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/BotManager.kt index 5a4c2fe7e..7290d0918 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/BotManager.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/BotManager.kt @@ -8,11 +8,13 @@ */ @file:Suppress("NOTHING_TO_INLINE") +@file:JvmMultifileClass +@file:JvmName("ConsoleUtils") package net.mamoe.mirai.console.util import net.mamoe.mirai.Bot -import net.mamoe.mirai.console.internal.utils.BotManagerImpl +import net.mamoe.mirai.console.internal.util.BotManagerImpl import net.mamoe.mirai.contact.User public interface BotManager { diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/ConsoleInput.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/ConsoleInput.kt index f6a54bb26..eb40973bf 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/ConsoleInput.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/ConsoleInput.kt @@ -8,6 +8,8 @@ */ @file:Suppress("INAPPLICABLE_JVM_NAME", "unused") +@file:JvmMultifileClass +@file:JvmName("ConsoleUtils") package net.mamoe.mirai.console.util diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/JavaPluginScheduler.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/JavaPluginScheduler.kt index 57d55e682..17bfe23e3 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/JavaPluginScheduler.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/JavaPluginScheduler.kt @@ -7,10 +7,14 @@ * https://github.com/mamoe/mirai/blob/master/LICENSE */ +@file:JvmMultifileClass +@file:JvmName("ConsoleUtils") + package net.mamoe.mirai.console.util -import kotlinx.coroutines.* -import kotlinx.coroutines.future.future +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Runnable +import net.mamoe.mirai.console.internal.util.JavaPluginSchedulerImpl import net.mamoe.mirai.console.plugin.jvm.JavaPlugin import java.util.concurrent.Callable import java.util.concurrent.CompletableFuture @@ -19,16 +23,13 @@ import kotlin.coroutines.CoroutineContext /** - * 拥有生命周期管理的 Java 线程池. + * 拥有生命周期管理的简单 Java 线程池. * * 在插件被 [卸载][JavaPlugin.onDisable] 时将会自动停止. * * @see JavaPlugin.scheduler 获取实例 */ -public class JavaPluginScheduler internal constructor(parentCoroutineContext: CoroutineContext) : CoroutineScope { - public override val coroutineContext: CoroutineContext = - parentCoroutineContext + SupervisorJob(parentCoroutineContext[Job]) - +public interface JavaPluginScheduler : CoroutineScope { /** * 新增一个 Repeating Task (定时任务) * @@ -36,60 +37,39 @@ public class JavaPluginScheduler internal constructor(parentCoroutineContext: Co * * @see Future.cancel 取消这个任务 */ - public fun repeating(intervalMs: Long, runnable: Runnable): Future { - return this.future { - while (isActive) { - withContext(Dispatchers.IO) { runnable.run() } - delay(intervalMs) - } - null - } - } + public fun repeating(intervalMs: Long, runnable: Runnable): Future /** * 新增一个 Delayed Task (延迟任务) * * 在延迟 [delayMillis] 后执行 [runnable] */ - public fun delayed(delayMillis: Long, runnable: Runnable): CompletableFuture { - return future { - delay(delayMillis) - withContext(Dispatchers.IO) { - runnable.run() - } - null - } - } + public fun delayed(delayMillis: Long, runnable: Runnable): CompletableFuture /** * 新增一个 Delayed Task (延迟任务) * * 在延迟 [delayMillis] 后执行 [runnable] */ - public fun delayed(delayMillis: Long, runnable: Callable): CompletableFuture { - return future { - delay(delayMillis) - withContext(Dispatchers.IO) { runnable.call() } - null - } - } + public fun delayed(delayMillis: Long, runnable: Callable): CompletableFuture /** * 异步执行一个任务, 最终返回 [Future], 与 Java 使用方法无异, 但效率更高且可以在插件关闭时停止 */ - public fun async(supplier: Callable): Future { - return future { - withContext(Dispatchers.IO) { supplier.call() } - } - } + public fun async(supplier: Callable): Future /** * 异步执行一个任务, 没有返回 */ - public fun async(runnable: Runnable): Future { - return future { - withContext(Dispatchers.IO) { runnable.run() } - null - } + public fun async(runnable: Runnable): Future + + public companion object { + /** + * 创建一个 [JavaPluginScheduler] + */ + @JvmStatic + @JvmName("create") + public operator fun invoke(parentCoroutineContext: CoroutineContext): JavaPluginScheduler = + JavaPluginSchedulerImpl(parentCoroutineContext) } } \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/retryCatching.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/retryCatching.kt index e62934e3b..765a18152 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/retryCatching.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/util/retryCatching.kt @@ -8,6 +8,8 @@ */ @file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE") +@file:JvmMultifileClass +@file:JvmName("ConsoleUtils") package net.mamoe.mirai.console.util