From a861b73cc7fee77552aad8a2b11e41c0a30ef6dc Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 23 May 2020 17:53:01 +0800 Subject: [PATCH] Implement Java specific scheduler; better coroutine exception handling --- .../mirai/console/event/EventListener.java | 51 ------------------- .../net/mamoe/mirai/console/event/Events.java | 30 ----------- .../mamoe/mirai/console/event/EventsImpl.kt | 32 ------------ .../mamoe/mirai/console/plugins/JvmPlugin.kt | 13 +++-- 4 files changed, 9 insertions(+), 117 deletions(-) delete mode 100644 backend/mirai-console/src/main/java/net/mamoe/mirai/console/event/EventListener.java delete mode 100644 backend/mirai-console/src/main/java/net/mamoe/mirai/console/event/Events.java delete mode 100644 backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/event/EventsImpl.kt diff --git a/backend/mirai-console/src/main/java/net/mamoe/mirai/console/event/EventListener.java b/backend/mirai-console/src/main/java/net/mamoe/mirai/console/event/EventListener.java deleted file mode 100644 index fbc6be9cb..000000000 --- a/backend/mirai-console/src/main/java/net/mamoe/mirai/console/event/EventListener.java +++ /dev/null @@ -1,51 +0,0 @@ -package net.mamoe.mirai.console.event; - -import net.mamoe.mirai.console.plugins.PluginBase; -import net.mamoe.mirai.event.Event; -import net.mamoe.mirai.event.Listener; -import net.mamoe.mirai.event.ListeningStatus; -import org.jetbrains.annotations.NotNull; - -import java.util.function.Consumer; -import java.util.function.Function; - -public class EventListener { - - PluginBase base; - - public EventListener( - PluginBase base - ){ - this.base = base; - } - - /** - * 监听一个事件, 当 {@code onEvent} 返回 {@link ListeningStatus#STOPPED} 时停止监听. - * 机器人离线后不会停止监听. - * - * @param eventClass 事件类 - * @param onEvent 事件处理. 返回 {@link ListeningStatus#LISTENING} 时继续监听. - * @param 事件类型 - * @return 事件监听器. 可调用 {@link Listener#complete()} 或 {@link Listener#completeExceptionally(Throwable)} 让监听正常停止或异常停止. - */ - @NotNull - public Listener subscribe(@NotNull Class eventClass, @NotNull Function onEvent) { - return EventsImplKt.subscribeEventForJaptOnly(eventClass, base, onEvent); - } - - - /** - * 监听一个事件, 直到手动停止. - * 机器人离线后不会停止监听. - * - * @param eventClass 事件类 - * @param onEvent 事件处理. 返回 {@link ListeningStatus#LISTENING} 时继续监听. - * @param 事件类型 - * @return 事件监听器. 可调用 {@link Listener#complete()} 或 {@link Listener#completeExceptionally(Throwable)} 让监听正常停止或异常停止. - */ - @NotNull - public Listener subscribeAlways(@NotNull Class eventClass, @NotNull Consumer onEvent) { - return EventsImplKt.subscribeEventForJaptOnly(eventClass, base, onEvent); - } - -} diff --git a/backend/mirai-console/src/main/java/net/mamoe/mirai/console/event/Events.java b/backend/mirai-console/src/main/java/net/mamoe/mirai/console/event/Events.java deleted file mode 100644 index 6dcdd0e2a..000000000 --- a/backend/mirai-console/src/main/java/net/mamoe/mirai/console/event/Events.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ - -package net.mamoe.mirai.console.event; - -import net.mamoe.mirai.event.Event; -import org.jetbrains.annotations.NotNull; - -/** - * 事件处理 - */ -public final class Events { - /** - * 阻塞地广播一个事件. - * - * @param event 事件 - * @param 事件类型 - * @return {@code event} 本身 - */ - @NotNull - public static E broadcast(@NotNull E event) { - return EventsImplKt.broadcast(event); - } -} \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/event/EventsImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/event/EventsImpl.kt deleted file mode 100644 index 738088acb..000000000 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/event/EventsImpl.kt +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ - -@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") - -package net.mamoe.mirai.console.event - -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.runBlocking -import net.mamoe.mirai.event.Event -import net.mamoe.mirai.event.Listener -import net.mamoe.mirai.event.ListeningStatus -import net.mamoe.mirai.event.broadcast -import net.mamoe.mirai.event.internal._subscribeEventForJaptOnly -import java.util.function.Consumer -import java.util.function.Function - -internal fun broadcast(e: E): E = runBlocking { e.broadcast() } - -internal fun Class.subscribeEventForJaptOnly( - scope: CoroutineScope, - onEvent: Function -): Listener = _subscribeEventForJaptOnly(scope, onEvent) - -internal fun Class.subscribeEventForJaptOnly(scope: CoroutineScope, onEvent: Consumer): Listener = - _subscribeEventForJaptOnly(scope, onEvent) \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/JvmPlugin.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/JvmPlugin.kt index 6cc2c9552..0ed7eae34 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/JvmPlugin.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/JvmPlugin.kt @@ -18,6 +18,7 @@ import kotlinx.coroutines.SupervisorJob import kotlinx.serialization.Serializable import kotlinx.serialization.Transient import net.mamoe.mirai.console.MiraiConsole +import net.mamoe.mirai.console.scheduler.PluginScheduler import net.mamoe.mirai.utils.MiraiLogger import java.io.File import kotlin.coroutines.CoroutineContext @@ -45,7 +46,11 @@ interface JvmPlugin : Plugin, CoroutineScope { abstract class JavaPlugin @JvmOverloads constructor( coroutineContext: CoroutineContext = EmptyCoroutineContext ) : JvmPlugin, JvmPluginImpl(coroutineContext) { - // TODO: 2020/5/23 scheduler, event listener(?) + + /** + * Java API Scheduler + */ + val scheduler: PluginScheduler? = PluginScheduler(this.coroutineContext) } abstract class KotlinPlugin @JvmOverloads constructor( @@ -102,9 +107,9 @@ internal abstract class JvmPluginImpl( final override val logger: MiraiLogger by lazy { MiraiConsole.newLogger(this._description.name) } final override val coroutineContext: CoroutineContext by lazy { - SupervisorJob(parentCoroutineContext[Job]) + CoroutineExceptionHandler { _, throwable -> - logger.error(throwable) - } + CoroutineExceptionHandler { _, throwable -> logger.error(throwable) } + .plus(parentCoroutineContext) + .plus(SupervisorJob(parentCoroutineContext[Job])) } }