Procházet zdrojové kódy

Add Java-friendly EventChannel.exceptionHandler API, close #1953

Him188 před 3 roky
rodič
revize
1a2241b8b2

+ 1 - 0
mirai-core-api/compatibility-validation/android/api/android.api

@@ -1737,6 +1737,7 @@ public abstract class net/mamoe/mirai/event/EventChannel {
 	public static synthetic fun asChannel$default (Lnet/mamoe/mirai/event/EventChannel;ILkotlin/coroutines/CoroutineContext;Lnet/mamoe/mirai/event/ConcurrencyKind;Lnet/mamoe/mirai/event/EventPriority;ILjava/lang/Object;)Lkotlinx/coroutines/channels/Channel;
 	public abstract fun asFlow ()Lkotlinx/coroutines/flow/Flow;
 	public abstract fun context ([Lkotlin/coroutines/CoroutineContext;)Lnet/mamoe/mirai/event/EventChannel;
+	public final fun exceptionHandler (Ljava/util/function/Consumer;)Lnet/mamoe/mirai/event/EventChannel;
 	public final fun exceptionHandler (Lkotlin/jvm/functions/Function1;)Lnet/mamoe/mirai/event/EventChannel;
 	public final fun exceptionHandler (Lkotlinx/coroutines/CoroutineExceptionHandler;)Lnet/mamoe/mirai/event/EventChannel;
 	public final fun filter (Lkotlin/jvm/functions/Function1;)Lnet/mamoe/mirai/event/EventChannel;

+ 1 - 0
mirai-core-api/compatibility-validation/jvm/api/jvm.api

@@ -1737,6 +1737,7 @@ public abstract class net/mamoe/mirai/event/EventChannel {
 	public static synthetic fun asChannel$default (Lnet/mamoe/mirai/event/EventChannel;ILkotlin/coroutines/CoroutineContext;Lnet/mamoe/mirai/event/ConcurrencyKind;Lnet/mamoe/mirai/event/EventPriority;ILjava/lang/Object;)Lkotlinx/coroutines/channels/Channel;
 	public abstract fun asFlow ()Lkotlinx/coroutines/flow/Flow;
 	public abstract fun context ([Lkotlin/coroutines/CoroutineContext;)Lnet/mamoe/mirai/event/EventChannel;
+	public final fun exceptionHandler (Ljava/util/function/Consumer;)Lnet/mamoe/mirai/event/EventChannel;
 	public final fun exceptionHandler (Lkotlin/jvm/functions/Function1;)Lnet/mamoe/mirai/event/EventChannel;
 	public final fun exceptionHandler (Lkotlinx/coroutines/CoroutineExceptionHandler;)Lnet/mamoe/mirai/event/EventChannel;
 	public final fun filter (Lkotlin/jvm/functions/Function1;)Lnet/mamoe/mirai/event/EventChannel;

+ 11 - 0
mirai-core-api/src/commonMain/kotlin/event/EventChannel.kt

@@ -314,6 +314,17 @@ public abstract class EventChannel<out BaseEvent : Event> @MiraiInternalApi publ
         })
     }
 
+    /**
+     * 创建一个新的 [EventChannel], 该 [EventChannel] 包含 [`this.coroutineContext`][defaultCoroutineContext] 和添加的 [coroutineExceptionHandler]
+     * @see context
+     * @since 2.12
+     */
+    @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
+    @kotlin.internal.LowPriorityInOverloadResolution
+    public fun exceptionHandler(coroutineExceptionHandler: Consumer<Throwable>): EventChannel<BaseEvent> {
+        return exceptionHandler { coroutineExceptionHandler.accept(it) }
+    }
+
     /**
      * 将 [coroutineScope] 作为这个 [EventChannel] 的父作用域.
      *