Implement Java specific scheduler; better coroutine exception handling

This commit is contained in:
Him188 2020-05-23 17:53:01 +08:00
parent 156c9e9cff
commit a861b73cc7
4 changed files with 9 additions and 117 deletions

View File

@ -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 <E> 事件类型
* @return 事件监听器. 可调用 {@link Listener#complete()} {@link Listener#completeExceptionally(Throwable)} 让监听正常停止或异常停止.
*/
@NotNull
public <E extends Event> Listener<E> subscribe(@NotNull Class<E> eventClass, @NotNull Function<E, ListeningStatus> onEvent) {
return EventsImplKt.subscribeEventForJaptOnly(eventClass, base, onEvent);
}
/**
* 监听一个事件, 直到手动停止.
* 机器人离线后不会停止监听.
*
* @param eventClass 事件类
* @param onEvent 事件处理. 返回 {@link ListeningStatus#LISTENING} 时继续监听.
* @param <E> 事件类型
* @return 事件监听器. 可调用 {@link Listener#complete()} {@link Listener#completeExceptionally(Throwable)} 让监听正常停止或异常停止.
*/
@NotNull
public <E extends Event> Listener<E> subscribeAlways(@NotNull Class<E> eventClass, @NotNull Consumer<E> onEvent) {
return EventsImplKt.subscribeEventForJaptOnly(eventClass, base, onEvent);
}
}

View File

@ -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 <E> 事件类型
* @return {@code event} 本身
*/
@NotNull
public static <E extends Event> E broadcast(@NotNull E event) {
return EventsImplKt.broadcast(event);
}
}

View File

@ -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 <E : Event> broadcast(e: E): E = runBlocking { e.broadcast() }
internal fun <E : Event> Class<E>.subscribeEventForJaptOnly(
scope: CoroutineScope,
onEvent: Function<E, ListeningStatus>
): Listener<E> = _subscribeEventForJaptOnly(scope, onEvent)
internal fun <E : Event> Class<E>.subscribeEventForJaptOnly(scope: CoroutineScope, onEvent: Consumer<E>): Listener<E> =
_subscribeEventForJaptOnly(scope, onEvent)

View File

@ -18,6 +18,7 @@ import kotlinx.coroutines.SupervisorJob
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient import kotlinx.serialization.Transient
import net.mamoe.mirai.console.MiraiConsole import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.scheduler.PluginScheduler
import net.mamoe.mirai.utils.MiraiLogger import net.mamoe.mirai.utils.MiraiLogger
import java.io.File import java.io.File
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
@ -45,7 +46,11 @@ interface JvmPlugin : Plugin, CoroutineScope {
abstract class JavaPlugin @JvmOverloads constructor( abstract class JavaPlugin @JvmOverloads constructor(
coroutineContext: CoroutineContext = EmptyCoroutineContext coroutineContext: CoroutineContext = EmptyCoroutineContext
) : JvmPlugin, JvmPluginImpl(coroutineContext) { ) : JvmPlugin, JvmPluginImpl(coroutineContext) {
// TODO: 2020/5/23 scheduler, event listener(?)
/**
* Java API Scheduler
*/
val scheduler: PluginScheduler? = PluginScheduler(this.coroutineContext)
} }
abstract class KotlinPlugin @JvmOverloads constructor( 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 logger: MiraiLogger by lazy { MiraiConsole.newLogger(this._description.name) }
final override val coroutineContext: CoroutineContext by lazy { final override val coroutineContext: CoroutineContext by lazy {
SupervisorJob(parentCoroutineContext[Job]) + CoroutineExceptionHandler { _, throwable -> CoroutineExceptionHandler { _, throwable -> logger.error(throwable) }
logger.error(throwable) .plus(parentCoroutineContext)
} .plus(SupervisorJob(parentCoroutineContext[Job]))
} }
} }