1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 08:13:47 +00:00

refactor of contentConverter

This commit is contained in:
InsanusMokrassar 2021-10-14 22:57:04 +06:00
parent 2a33b22afb
commit 5a192b00d4
2 changed files with 10 additions and 11 deletions

View File

@ -7,7 +7,7 @@
* `Common`:
* `Version`:
* `Serialization`: `1.2.2` -> `1.3.0`
* `MicroUtils`: `0.5.28` -> `0.7.0`
* `MicroUtils`: `0.5.28` -> `0.7.1`
* `Klock`: `2.4.3` -> `2.4.5`
* `Core`:
* `PayInlineKeyboardButton#pay` now is deprecated

View File

@ -16,6 +16,7 @@ import dev.inmo.tgbotapi.types.update.MediaGroupUpdates.SentMediaGroupUpdate
import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.toList
import kotlin.reflect.KClass
typealias CommonMessageToContentMapper<T> = suspend CommonMessage<T>.() -> T?
@ -54,19 +55,17 @@ private suspend fun <O> BehaviourContext.waitCommonMessage(
internal inline fun <reified T : MessageContent> contentConverter(
noinline mapper: CommonMessageToContentMapper<T>? = null
): suspend CommonMessage<MessageContent>.() -> T? = {
if (content is T) {
@Suppress("UNCHECKED_CAST")
val message = (this as CommonMessage<T>)
if (mapper == null) {
message.content
} else {
): suspend CommonMessage<MessageContent>.() -> T? = mapper ?.let {
{
if (content is T) {
@Suppress("UNCHECKED_CAST")
val message = (this as CommonMessage<T>)
safelyWithoutExceptions { mapper(message) }
} else {
null
}
} else {
null
}
}
} ?: { content as? T }
private suspend inline fun <reified T : MessageContent> BehaviourContext.waitContent(
count: Int = 1,