Move EventChannel kotlin function reference extensions to separate file

This commit is contained in:
Him188 2020-12-27 15:05:30 +08:00
parent 7df0c96004
commit ac95ef4472
3 changed files with 201 additions and 179 deletions

View File

@ -455,185 +455,6 @@ public open class EventChannel<out BaseEvent : Event> @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 <reified E : Event> subscribe(
crossinline handler: (E) -> ListeningStatus,
priority: EventPriority = EventPriority.NORMAL,
concurrency: Listener.ConcurrencyKind = CONCURRENT,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Listener<E> = 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 <reified E : Event> subscribe(
crossinline handler: E.(E) -> ListeningStatus,
priority: EventPriority = EventPriority.NORMAL,
concurrency: Listener.ConcurrencyKind = CONCURRENT,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Listener<E> = 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 <reified E : Event> subscribe(
crossinline handler: suspend (E) -> ListeningStatus,
priority: EventPriority = EventPriority.NORMAL,
concurrency: Listener.ConcurrencyKind = CONCURRENT,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Listener<E> = 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 <reified E : Event> subscribe(
crossinline handler: suspend E.(E) -> ListeningStatus,
priority: EventPriority = EventPriority.NORMAL,
concurrency: Listener.ConcurrencyKind = CONCURRENT,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Listener<E> = 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 <reified E : Event> subscribeAlways(
crossinline handler: (E) -> Unit,
priority: EventPriority = EventPriority.NORMAL,
concurrency: Listener.ConcurrencyKind = CONCURRENT,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Listener<E> = 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 <reified E : Event> subscribeAlways(
crossinline handler: E.(E) -> Unit,
priority: EventPriority = EventPriority.NORMAL,
concurrency: Listener.ConcurrencyKind = CONCURRENT,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Listener<E> = 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 <reified E : Event> subscribeAlways(
crossinline handler: suspend (E) -> Unit,
priority: EventPriority = EventPriority.NORMAL,
concurrency: Listener.ConcurrencyKind = CONCURRENT,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Listener<E> = 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 <reified E : Event> subscribeAlways(
crossinline handler: suspend E.(E) -> Unit,
priority: EventPriority = EventPriority.NORMAL,
concurrency: Listener.ConcurrencyKind = CONCURRENT,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Listener<E> = subscribeAlways(E::class, coroutineContext, concurrency, priority) { handler(this) }
// endregion
// region Java API
/**

View File

@ -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 <BaseEvent : Event, reified E : Event> EventChannel<BaseEvent>.subscribe(
crossinline handler: suspend E.(E) -> ListeningStatus,
priority: EventPriority = EventPriority.NORMAL,
concurrency: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Listener<E> = 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 <BaseEvent : Event, reified E : Event> EventChannel<BaseEvent>.subscribe(
crossinline handler: suspend (E) -> ListeningStatus,
priority: EventPriority = EventPriority.NORMAL,
concurrency: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Listener<E> = 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 <BaseEvent : Event, reified E : Event> EventChannel<BaseEvent>.subscribe(
crossinline handler: E.(E) -> ListeningStatus,
priority: EventPriority = EventPriority.NORMAL,
concurrency: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Listener<E> = 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 <BaseEvent : Event, reified E : Event> EventChannel<BaseEvent>.subscribe(
crossinline handler: (E) -> ListeningStatus,
priority: EventPriority = EventPriority.NORMAL,
concurrency: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Listener<E> = 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 <BaseEvent : Event, reified E : Event> EventChannel<BaseEvent>.subscribeAlways(
crossinline handler: suspend (E) -> Unit,
priority: EventPriority = EventPriority.NORMAL,
concurrency: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Listener<E> = 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 <BaseEvent : Event, reified E : Event> EventChannel<BaseEvent>.subscribeAlways(
crossinline handler: suspend E.(E) -> Unit,
priority: EventPriority = EventPriority.NORMAL,
concurrency: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Listener<E> = 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 <BaseEvent : Event, reified E : Event> EventChannel<BaseEvent>.subscribeAlways(
crossinline handler: E.(E) -> Unit,
priority: EventPriority = EventPriority.NORMAL,
concurrency: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Listener<E> = subscribeAlways(E::class, coroutineContext, concurrency, priority) { handler(this) }
/**
* 支持 Kotlin 带接收者的挂起函数的函数引用的监听方式.
* ```
* fun onMessage(event: GroupMessageEvent) {
*
* }
* eventChannel.subscribeAlways(::onMessage)
* ```
* @see subscribeAlways
*/
@JvmSynthetic
@LowPriorityInOverloadResolution
public inline fun <BaseEvent : Event, reified E : Event> EventChannel<BaseEvent>.subscribeAlways(
crossinline handler: (E) -> Unit,
priority: EventPriority = EventPriority.NORMAL,
concurrency: Listener.ConcurrencyKind = Listener.ConcurrencyKind.CONCURRENT,
coroutineContext: CoroutineContext = EmptyCoroutineContext
): Listener<E> = subscribeAlways(E::class, coroutineContext, concurrency, priority) { handler(this) }

View File

@ -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);
}
}