From ecda34deee80b98104b4db9d9f7d6aa8f2d27cc3 Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 19 Mar 2020 10:25:57 +0800 Subject: [PATCH] Add async shortcuts for `nextMessage` --- .../net.mamoe.mirai/message/MessagePacket.kt | 56 ++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessagePacket.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessagePacket.kt index 951d4360c..de8b57cad 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessagePacket.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessagePacket.kt @@ -11,6 +11,8 @@ package net.mamoe.mirai.message +import kotlinx.coroutines.Deferred +import kotlinx.coroutines.async import kotlinx.coroutines.io.ByteReadChannel import net.mamoe.mirai.Bot import net.mamoe.mirai.contact.Contact @@ -254,6 +256,20 @@ suspend inline fun > P.nextMessage( }.message } +/** + * @see nextMessage + */ +inline fun > P.nextMessageAsync( + timeoutMillis: Long = -1, + coroutineContext: CoroutineContext = EmptyCoroutineContext +): Deferred { + return this.bot.async(coroutineContext) { + subscribingGet(timeoutMillis) { + takeIf { this.isContextIdenticalWith(this@nextMessageAsync) } + }.message + } +} + /** * 挂起当前协程, 等待下一条 [MessagePacket.sender] 和 [MessagePacket.subject] 与 [this] 相同的 [MessagePacket] * @@ -272,6 +288,20 @@ suspend inline fun > P.nextMessageOrNull( }?.message } +/** + * @see nextMessageOrNull + */ +inline fun > P.nextMessageOrNullAsync( + timeoutMillis: Long = -1, + coroutineContext: CoroutineContext = EmptyCoroutineContext +): Deferred { + return this.bot.async(coroutineContext) { + subscribingGetOrNull(timeoutMillis) { + takeIf { this.isContextIdenticalWith(this@nextMessageOrNullAsync) } + }?.message + } +} + /** * 挂起当前协程, 等待下一条 [MessagePacket.sender] 和 [MessagePacket.subject] 与 [this] 相同的 [MessagePacket] * @@ -289,6 +319,18 @@ suspend inline fun MessagePacket<*, *>.nextMessageContaini }.message.first() } +inline fun MessagePacket<*, *>.nextMessageContainingAsync( + timeoutMillis: Long = -1, + coroutineContext: CoroutineContext = EmptyCoroutineContext +): Deferred { + return this.bot.async(coroutineContext) { + @Suppress("RemoveExplicitTypeArguments") + subscribingGet, MessagePacket<*, *>>(timeoutMillis) { + takeIf { this.isContextIdenticalWith(this@nextMessageContainingAsync) } + }.message.first() + } +} + /** * 挂起当前协程, 等待下一条 [MessagePacket.sender] 和 [MessagePacket.subject] 与 [this] 相同并含有 [M] 类型的消息的 [MessagePacket] * @@ -305,4 +347,16 @@ suspend inline fun MessagePacket<*, *>.nextMessageContaini return subscribingGetOrNull, MessagePacket<*, *>>(timeoutMillis) { takeIf { this.isContextIdenticalWith(this@nextMessageContainingOrNull) } }?.message?.first() -} \ No newline at end of file +} + +inline fun MessagePacket<*, *>.nextMessageContainingOrNullAsync( + timeoutMillis: Long = -1, + coroutineContext: CoroutineContext = EmptyCoroutineContext +): Deferred { + return this.bot.async(coroutineContext) { + subscribingGetOrNull, MessagePacket<*, *>>(timeoutMillis) { + takeIf { this.isContextIdenticalWith(this@nextMessageContainingOrNullAsync) } + }?.message?.first() + } +} +