From 843643a78d92db8ecb40f9d529dafb68a396f1e5 Mon Sep 17 00:00:00 2001 From: Him188 <Him188@mamoe.net> Date: Fri, 14 Feb 2020 20:20:28 +0800 Subject: [PATCH] Add internal functions for japt --- .../net.mamoe.mirai/event/Subscribers.kt | 6 ++++ .../mirai/event/internal/EventInternalJvm.kt | 30 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/event/internal/EventInternalJvm.kt diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/Subscribers.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/Subscribers.kt index 234ed239e..fd6887e85 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/Subscribers.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/Subscribers.kt @@ -16,6 +16,7 @@ import kotlinx.coroutines.GlobalScope import net.mamoe.mirai.Bot import net.mamoe.mirai.event.internal.Handler import net.mamoe.mirai.event.internal.subscribeInternal +import net.mamoe.mirai.utils.MiraiInternalAPI import kotlin.coroutines.CoroutineContext /* @@ -96,6 +97,7 @@ interface Listener<in E : Event> : CompletableJob { * @see subscribeGroupMessages 监听群消息 DSL * @see subscribeFriendMessages 监听好友消息 DSL */ +@UseExperimental(MiraiInternalAPI::class) inline fun <reified E : Event> CoroutineScope.subscribe(crossinline handler: suspend E.(E) -> ListeningStatus): Listener<E> = E::class.subscribeInternal(Handler { it.handler(it); }) @@ -107,6 +109,7 @@ inline fun <reified E : Event> CoroutineScope.subscribe(crossinline handler: sus * * @see subscribe 获取更多说明 */ +@UseExperimental(MiraiInternalAPI::class) inline fun <reified E : Event> CoroutineScope.subscribeAlways(crossinline listener: suspend E.(E) -> Unit): Listener<E> = E::class.subscribeInternal(Handler { it.listener(it); ListeningStatus.LISTENING }) @@ -118,6 +121,7 @@ inline fun <reified E : Event> CoroutineScope.subscribeAlways(crossinline listen * * @see subscribe 获取更多说明 */ +@UseExperimental(MiraiInternalAPI::class) inline fun <reified E : Event> CoroutineScope.subscribeOnce(crossinline listener: suspend E.(E) -> Unit): Listener<E> = E::class.subscribeInternal(Handler { it.listener(it); ListeningStatus.STOPPED }) @@ -129,6 +133,7 @@ inline fun <reified E : Event> CoroutineScope.subscribeOnce(crossinline listener * * @see subscribe 获取更多说明 */ +@UseExperimental(MiraiInternalAPI::class) inline fun <reified E : Event, T> CoroutineScope.subscribeUntil(valueIfStop: T, crossinline listener: suspend E.(E) -> T): Listener<E> = E::class.subscribeInternal(Handler { if (it.listener(it) == valueIfStop) ListeningStatus.STOPPED else ListeningStatus.LISTENING }) @@ -141,6 +146,7 @@ inline fun <reified E : Event, T> CoroutineScope.subscribeUntil(valueIfStop: T, * * @see subscribe 获取更多说明 */ +@UseExperimental(MiraiInternalAPI::class) inline fun <reified E : Event, T> CoroutineScope.subscribeWhile(valueIfContinue: T, crossinline listener: suspend E.(E) -> T): Listener<E> = E::class.subscribeInternal(Handler { if (it.listener(it) != valueIfContinue) ListeningStatus.STOPPED else ListeningStatus.LISTENING }) diff --git a/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/event/internal/EventInternalJvm.kt b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/event/internal/EventInternalJvm.kt new file mode 100644 index 000000000..ccc2a718f --- /dev/null +++ b/mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/event/internal/EventInternalJvm.kt @@ -0,0 +1,30 @@ +/* + * 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.event.internal + +import kotlinx.coroutines.CoroutineScope +import net.mamoe.mirai.event.Event +import net.mamoe.mirai.event.Listener +import net.mamoe.mirai.event.ListeningStatus +import net.mamoe.mirai.utils.MiraiInternalAPI +import java.util.function.Consumer +import java.util.function.Function + +@MiraiInternalAPI +@Suppress("FunctionName") +fun <E : Event> Class<E>._subscribeEventForJaptOnly(scope: CoroutineScope, onEvent: Function<E, ListeningStatus>): Listener<E> { + return this.kotlin.subscribeInternal(scope.Handler { onEvent.apply(it) }) +} + +@MiraiInternalAPI +@Suppress("FunctionName") +fun <E : Event> Class<E>._subscribeEventForJaptOnly(scope: CoroutineScope, onEvent: Consumer<E>): Listener<E> { + return this.kotlin.subscribeInternal(scope.Handler { onEvent.accept(it); ListeningStatus.LISTENING; }) +} \ No newline at end of file