From ac95ef4472b04efd0b276704fc0deb5381655f89 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 27 Dec 2020 15:05:30 +0800 Subject: [PATCH] Move EventChannel kotlin function reference extensions to separate file --- .../commonMain/kotlin/event/EventChannel.kt | 179 ----------------- .../event/EventChannelKotlinExtensions.kt | 181 ++++++++++++++++++ .../src/jvmTest/kotlin/JavaApiTests.java | 20 ++ 3 files changed, 201 insertions(+), 179 deletions(-) create mode 100644 mirai-core-api/src/commonMain/kotlin/event/EventChannelKotlinExtensions.kt diff --git a/mirai-core-api/src/commonMain/kotlin/event/EventChannel.kt b/mirai-core-api/src/commonMain/kotlin/event/EventChannel.kt index fba889c24..62f6d1249 100644 --- a/mirai-core-api/src/commonMain/kotlin/event/EventChannel.kt +++ b/mirai-core-api/src/commonMain/kotlin/event/EventChannel.kt @@ -455,185 +455,6 @@ public open class EventChannel @JvmOverloads constructor( // endregion - // region subscribe with Kotlin function reference - - - /** - * 支持 Kotlin 带接收者的挂起函数的函数引用的监听方式. - * - * ``` - * fun onMessage(event: GroupMessageEvent): ListeningStatus { - * return ListeningStatus.LISTENING - * } - * - * eventChannel.subscribe(::onMessage) - * ``` - * @see subscribe - */ - @JvmSynthetic - @LowPriorityInOverloadResolution - @JvmName("subscribe1") - public inline fun subscribe( - crossinline handler: (E) -> ListeningStatus, - priority: EventPriority = EventPriority.NORMAL, - concurrency: Listener.ConcurrencyKind = CONCURRENT, - coroutineContext: CoroutineContext = EmptyCoroutineContext - ): Listener = subscribe(E::class, coroutineContext, concurrency, priority) { handler(this) } - - /** - * 支持 Kotlin 带接收者的函数的函数引用的监听方式. - * - * ``` - * fun GroupMessageEvent.onMessage(event: GroupMessageEvent): ListeningStatus { - * return ListeningStatus.LISTENING - * } - * - * eventChannel.subscribe(GroupMessageEvent::onMessage) - * ``` - * @see subscribe - */ - @JvmSynthetic - @LowPriorityInOverloadResolution - @JvmName("subscribe2") - public inline fun subscribe( - crossinline handler: E.(E) -> ListeningStatus, - priority: EventPriority = EventPriority.NORMAL, - concurrency: Listener.ConcurrencyKind = CONCURRENT, - coroutineContext: CoroutineContext = EmptyCoroutineContext - ): Listener = subscribe(E::class, coroutineContext, concurrency, priority) { handler(this) } - - /** - * 支持 Kotlin 挂起函数的函数引用的监听方式. - * - * ``` - * suspend fun onMessage(event: GroupMessageEvent): ListeningStatus { - * return ListeningStatus.LISTENING - * } - * - * eventChannel.subscribe(::onMessage) - * ``` - * @see subscribe - */ - @JvmSynthetic - @LowPriorityInOverloadResolution - @JvmName("subscribe1") - public inline fun subscribe( - crossinline handler: suspend (E) -> ListeningStatus, - priority: EventPriority = EventPriority.NORMAL, - concurrency: Listener.ConcurrencyKind = CONCURRENT, - coroutineContext: CoroutineContext = EmptyCoroutineContext - ): Listener = subscribe(E::class, coroutineContext, concurrency, priority) { handler(this) } - - /** - * 支持 Kotlin 带接收者的挂起函数的函数引用的监听方式. - * - * ``` - * suspend fun GroupMessageEvent.onMessage(event: GroupMessageEvent): ListeningStatus { - * return ListeningStatus.LISTENING - * } - * - * eventChannel.subscribe(GroupMessageEvent::onMessage) - * ``` - * @see subscribe - */ - @JvmSynthetic - @LowPriorityInOverloadResolution - @JvmName("subscribe3") - public inline fun subscribe( - crossinline handler: suspend E.(E) -> ListeningStatus, - priority: EventPriority = EventPriority.NORMAL, - concurrency: Listener.ConcurrencyKind = CONCURRENT, - coroutineContext: CoroutineContext = EmptyCoroutineContext - ): Listener = subscribe(E::class, coroutineContext, concurrency, priority) { handler(this) } - - - // endregion - - // region subscribeAlways with Kotlin function references - - - /** - * 支持 Kotlin 带接收者的挂起函数的函数引用的监听方式. - * ``` - * fun onMessage(event: GroupMessageEvent) { - * - * } - * eventChannel.subscribeAlways(::onMessage) - * ``` - * @see subscribeAlways - */ - @JvmSynthetic - @LowPriorityInOverloadResolution - @JvmName("subscribeAlways1") - public inline fun subscribeAlways( - crossinline handler: (E) -> Unit, - priority: EventPriority = EventPriority.NORMAL, - concurrency: Listener.ConcurrencyKind = CONCURRENT, - coroutineContext: CoroutineContext = EmptyCoroutineContext - ): Listener = subscribeAlways(E::class, coroutineContext, concurrency, priority) { handler(this) } - - /** - * 支持 Kotlin 带接收者的函数的函数引用的监听方式. - * ``` - * fun GroupMessageEvent.onMessage(event: GroupMessageEvent) { - * - * } - * eventChannel.subscribeAlways(GroupMessageEvent::onMessage) - * ``` - * @see subscribeAlways - */ - @JvmSynthetic - @LowPriorityInOverloadResolution - @JvmName("subscribeAlways1") - public inline fun subscribeAlways( - crossinline handler: E.(E) -> Unit, - priority: EventPriority = EventPriority.NORMAL, - concurrency: Listener.ConcurrencyKind = CONCURRENT, - coroutineContext: CoroutineContext = EmptyCoroutineContext - ): Listener = subscribeAlways(E::class, coroutineContext, concurrency, priority) { handler(this) } - - /** - * 支持 Kotlin 挂起函数的函数引用的监听方式. - * ``` - * suspend fun onMessage(event: GroupMessageEvent) { - * - * } - * eventChannel.subscribeAlways(::onMessage) - * ``` - * @see subscribeAlways - */ - @JvmSynthetic - @LowPriorityInOverloadResolution - @JvmName("subscribe4") - public inline fun subscribeAlways( - crossinline handler: suspend (E) -> Unit, - priority: EventPriority = EventPriority.NORMAL, - concurrency: Listener.ConcurrencyKind = CONCURRENT, - coroutineContext: CoroutineContext = EmptyCoroutineContext - ): Listener = subscribeAlways(E::class, coroutineContext, concurrency, priority) { handler(this) } - - /** - * 支持 Kotlin 带接收者的挂起函数的函数引用的监听方式. - * ``` - * suspend fun GroupMessageEvent.onMessage(event: GroupMessageEvent) { - * - * } - * eventChannel.subscribeAlways(GroupMessageEvent::onMessage) - * ``` - * @see subscribeAlways - */ - @JvmSynthetic - @LowPriorityInOverloadResolution - @JvmName("subscribe1") - public inline fun subscribeAlways( - crossinline handler: suspend E.(E) -> Unit, - priority: EventPriority = EventPriority.NORMAL, - concurrency: Listener.ConcurrencyKind = CONCURRENT, - coroutineContext: CoroutineContext = EmptyCoroutineContext - ): Listener = subscribeAlways(E::class, coroutineContext, concurrency, priority) { handler(this) } - - // endregion - // region Java API /** diff --git a/mirai-core-api/src/commonMain/kotlin/event/EventChannelKotlinExtensions.kt b/mirai-core-api/src/commonMain/kotlin/event/EventChannelKotlinExtensions.kt new file mode 100644 index 000000000..c813b531d --- /dev/null +++ b/mirai-core-api/src/commonMain/kotlin/event/EventChannelKotlinExtensions.kt @@ -0,0 +1,181 @@ +/* + * Copyright 2019-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") +@file:JvmName("EventChannelKotlinExtensions") + +package net.mamoe.mirai.event + +import kotlin.coroutines.CoroutineContext +import kotlin.coroutines.EmptyCoroutineContext +import kotlin.internal.LowPriorityInOverloadResolution + + +/** + * 支持 Kotlin 带接收者的挂起函数的函数引用的监听方式. + * + * ``` + * suspend fun GroupMessageEvent.onMessage(event: GroupMessageEvent): ListeningStatus { + * return ListeningStatus.LISTENING + * } + * + * eventChannel.subscribe(GroupMessageEvent::onMessage) + * ``` + * @see subscribe + */ +@JvmSynthetic +@LowPriorityInOverloadResolution +public inline fun EventChannel.subscribe( + crossinline handler: suspend E.(E) -> ListeningStatus, + priority: EventPriority = EventPriority.NORMAL, + concurrency: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT, + coroutineContext: CoroutineContext = EmptyCoroutineContext +): Listener = subscribe(E::class, coroutineContext, concurrency, priority) { handler(this) } + +/** + * 支持 Kotlin 挂起函数的函数引用的监听方式. + * + * ``` + * suspend fun onMessage(event: GroupMessageEvent): ListeningStatus { + * return ListeningStatus.LISTENING + * } + * + * eventChannel.subscribe(::onMessage) + * ``` + * @see subscribe + */ +@JvmSynthetic +@LowPriorityInOverloadResolution +@JvmName("subscribe1") +public inline fun EventChannel.subscribe( + crossinline handler: suspend (E) -> ListeningStatus, + priority: EventPriority = EventPriority.NORMAL, + concurrency: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT, + coroutineContext: CoroutineContext = EmptyCoroutineContext +): Listener = subscribe(E::class, coroutineContext, concurrency, priority) { handler(this) } + +/** + * 支持 Kotlin 带接收者的函数的函数引用的监听方式. + * + * ``` + * fun GroupMessageEvent.onMessage(event: GroupMessageEvent): ListeningStatus { + * return ListeningStatus.LISTENING + * } + * + * eventChannel.subscribe(GroupMessageEvent::onMessage) + * ``` + * @see subscribe + */ +@JvmSynthetic +@LowPriorityInOverloadResolution +public inline fun EventChannel.subscribe( + crossinline handler: E.(E) -> ListeningStatus, + priority: EventPriority = EventPriority.NORMAL, + concurrency: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT, + coroutineContext: CoroutineContext = EmptyCoroutineContext +): Listener = subscribe(E::class, coroutineContext, concurrency, priority) { handler(this) } + +/** + * 支持 Kotlin 带接收者的挂起函数的函数引用的监听方式. + * + * ``` + * fun onMessage(event: GroupMessageEvent): ListeningStatus { + * return ListeningStatus.LISTENING + * } + * + * eventChannel.subscribe(::onMessage) + * ``` + * @see subscribe + */ +@JvmSynthetic +@LowPriorityInOverloadResolution +public inline fun EventChannel.subscribe( + crossinline handler: (E) -> ListeningStatus, + priority: EventPriority = EventPriority.NORMAL, + concurrency: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT, + coroutineContext: CoroutineContext = EmptyCoroutineContext +): Listener = subscribe(E::class, coroutineContext, concurrency, priority) { handler(this) } + +/** + * 支持 Kotlin 挂起函数的函数引用的监听方式. + * ``` + * suspend fun onMessage(event: GroupMessageEvent) { + * + * } + * eventChannel.subscribeAlways(::onMessage) + * ``` + * @see subscribeAlways + */ +@JvmName("subscribeAlways1") +@JvmSynthetic +@LowPriorityInOverloadResolution +public inline fun EventChannel.subscribeAlways( + crossinline handler: suspend (E) -> Unit, + priority: EventPriority = EventPriority.NORMAL, + concurrency: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT, + coroutineContext: CoroutineContext = EmptyCoroutineContext +): Listener = subscribeAlways(E::class, coroutineContext, concurrency, priority) { handler(this) } + + +/** + * 支持 Kotlin 带接收者的挂起函数的函数引用的监听方式. + * ``` + * suspend fun GroupMessageEvent.onMessage(event: GroupMessageEvent) { + * + * } + * eventChannel.subscribeAlways(GroupMessageEvent::onMessage) + * ``` + * @see subscribeAlways + */ +@JvmSynthetic +@LowPriorityInOverloadResolution +public inline fun EventChannel.subscribeAlways( + crossinline handler: suspend E.(E) -> Unit, + priority: EventPriority = EventPriority.NORMAL, + concurrency: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT, + coroutineContext: CoroutineContext = EmptyCoroutineContext +): Listener = subscribeAlways(E::class, coroutineContext, concurrency, priority) { handler(this) } + +/** + * 支持 Kotlin 带接收者的函数的函数引用的监听方式. + * ``` + * fun GroupMessageEvent.onMessage(event: GroupMessageEvent) { + * + * } + * eventChannel.subscribeAlways(GroupMessageEvent::onMessage) + * ``` + * @see subscribeAlways + */ +@JvmSynthetic +@LowPriorityInOverloadResolution +public inline fun EventChannel.subscribeAlways( + crossinline handler: E.(E) -> Unit, + priority: EventPriority = EventPriority.NORMAL, + concurrency: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT, + coroutineContext: CoroutineContext = EmptyCoroutineContext +): Listener = subscribeAlways(E::class, coroutineContext, concurrency, priority) { handler(this) } + +/** + * 支持 Kotlin 带接收者的挂起函数的函数引用的监听方式. + * ``` + * fun onMessage(event: GroupMessageEvent) { + * + * } + * eventChannel.subscribeAlways(::onMessage) + * ``` + * @see subscribeAlways + */ +@JvmSynthetic +@LowPriorityInOverloadResolution +public inline fun EventChannel.subscribeAlways( + crossinline handler: (E) -> Unit, + priority: EventPriority = EventPriority.NORMAL, + concurrency: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT, + coroutineContext: CoroutineContext = EmptyCoroutineContext +): Listener = subscribeAlways(E::class, coroutineContext, concurrency, priority) { handler(this) } \ No newline at end of file diff --git a/mirai-core/src/jvmTest/kotlin/JavaApiTests.java b/mirai-core/src/jvmTest/kotlin/JavaApiTests.java index c14ce8105..4c6ce159a 100644 --- a/mirai-core/src/jvmTest/kotlin/JavaApiTests.java +++ b/mirai-core/src/jvmTest/kotlin/JavaApiTests.java @@ -7,12 +7,17 @@ * https://github.com/mamoe/mirai/blob/master/LICENSE */ +import kotlin.coroutines.CoroutineContext; import net.mamoe.mirai.Bot; import net.mamoe.mirai.BotFactory; +import net.mamoe.mirai.event.EventHandler; +import net.mamoe.mirai.event.Events; import net.mamoe.mirai.event.ListeningStatus; +import net.mamoe.mirai.event.SimpleListenerHost; import net.mamoe.mirai.event.events.GroupMessageEvent; import net.mamoe.mirai.event.events.MessageEvent; import net.mamoe.mirai.utils.BotConfiguration; +import org.jetbrains.annotations.NotNull; /** * 仅用来测试调用,不会被单元测试运行 @@ -38,5 +43,20 @@ public class JavaApiTests { Bot b = event.getBot(); }); + + SimpleListenerHost slh = new SimpleListenerHost() { + + @Override + public void handleException(@NotNull CoroutineContext context, @NotNull Throwable exception) { + super.handleException(context, exception); + } + + @EventHandler + public void onMsg(GroupMessageEvent e) { + + } + }; + + Events.registerEvents(slh); } }