From a9a3f55c8d54c40ad01b48adaa8dec0bf5d2ac20 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 27 Jun 2020 09:43:24 +0600 Subject: [PATCH] refactoring of FlowsUpdatesFilter --- CHANGELOG.md | 4 + .../TelegramBotAPI/types/update/RawUpdate.kt | 7 +- .../types/update/abstracts/Update.kt | 4 +- .../updateshandlers/FlowsUpdatesFilter.kt | 88 ++++++------------- .../updateshandlers/UpdatesFilter.kt | 9 +- 5 files changed, 41 insertions(+), 71 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1c153ad99..599c7d0ffc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,10 @@ ### 0.27.8 +* `TelegramBotAPI`: + * `UnknownUpdateType` was renamed to `UnknownUpdate` + * Refactoring and optimization of `FlowsUpdatesFilter` + ### 0.27.7 * `TelegramBotAPI`: diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/RawUpdate.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/RawUpdate.kt index 809c0ea145..f37b9239aa 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/RawUpdate.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/RawUpdate.kt @@ -9,8 +9,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.payments.PreCheckoutQuer import com.github.insanusmokrassar.TelegramBotAPI.types.payments.ShippingQuery import com.github.insanusmokrassar.TelegramBotAPI.types.polls.Poll import com.github.insanusmokrassar.TelegramBotAPI.types.polls.PollAnswer -import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UnknownUpdateType -import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.updateIdField import kotlinx.serialization.* import kotlinx.serialization.json.JsonElement @@ -57,7 +56,7 @@ internal data class RawUpdate constructor( pre_checkout_query != null -> PreCheckoutQueryUpdate(updateId, pre_checkout_query) poll != null -> PollUpdate(updateId, poll) poll_answer != null -> PollAnswerUpdate(updateId, poll_answer) - else -> UnknownUpdateType( + else -> UnknownUpdate( updateId, raw.toString(), raw @@ -66,7 +65,7 @@ internal data class RawUpdate constructor( } catch (e: Error) { when (e) { is SerializationException, - is NotImplementedError -> UnknownUpdateType( + is NotImplementedError -> UnknownUpdate( updateId, raw.toString(), raw diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt index 874d3860fc..24be238958 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/update/abstracts/Update.kt @@ -12,11 +12,13 @@ interface Update { val data: Any } -data class UnknownUpdateType( +data class UnknownUpdate( override val updateId: UpdateIdentifier, override val data: String, val rawJson: JsonElement ) : Update +@Deprecated("Renamed", ReplaceWith("UnknownUpdate")) +typealias UnknownUpdateType = UnknownUpdate internal object UpdateSerializerWithoutSerialization : KSerializer { override val descriptor: SerialDescriptor = JsonElementSerializer.descriptor diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt index 052e1a8f03..203aa819fb 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/FlowsUpdatesFilter.kt @@ -1,74 +1,40 @@ package com.github.insanusmokrassar.TelegramBotAPI.updateshandlers +import com.github.insanusmokrassar.TelegramBotAPI.types.ALL_UPDATES_LIST import com.github.insanusmokrassar.TelegramBotAPI.types.update.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.* -import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.* import kotlinx.coroutines.channels.BroadcastChannel -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.asFlow - -@Suppress("EXPERIMENTAL_API_USAGE") -private fun BroadcastChannel.createUpdateReceiver(): UpdateReceiver = ::send +import kotlinx.coroutines.flow.* @Suppress("EXPERIMENTAL_API_USAGE", "unused") class FlowsUpdatesFilter( - broadcastChannelsSize: Int = 64 + broadcastChannelsSize: Int = 100 ): UpdatesFilter { - private val messageChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) - private val messageMediaGroupChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) - private val editedMessageChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) - private val editedMessageMediaGroupChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) - private val channelPostChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) - private val channelPostMediaGroupChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) - private val editedChannelPostChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) - private val editedChannelPostMediaGroupChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) - private val chosenInlineResultChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) - private val inlineQueryChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) - private val callbackQueryChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) - private val shippingQueryChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) - private val preCheckoutQueryChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) - private val pollChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) - private val pollAnswerChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) - private val unknownUpdateChannel: BroadcastChannel = BroadcastChannel(broadcastChannelsSize) + private val updatesReceivingChannel = BroadcastChannel(broadcastChannelsSize) + @Suppress("MemberVisibilityCanBePrivate") + val allUpdatesFlow: Flow = updatesReceivingChannel.asFlow() override val allowedUpdates: List - get() = filter.allowedUpdates - override val asUpdateReceiver: UpdateReceiver - get() = filter.asUpdateReceiver + get() = ALL_UPDATES_LIST + override val asUpdateReceiver: UpdateReceiver = { + updatesReceivingChannel.send(it) + } - val filter = SimpleUpdatesFilter( - messageChannel.createUpdateReceiver(), - messageMediaGroupChannel.createUpdateReceiver(), - editedMessageChannel.createUpdateReceiver(), - editedMessageMediaGroupChannel.createUpdateReceiver(), - channelPostChannel.createUpdateReceiver(), - channelPostMediaGroupChannel.createUpdateReceiver(), - editedChannelPostChannel.createUpdateReceiver(), - editedChannelPostMediaGroupChannel.createUpdateReceiver(), - chosenInlineResultChannel.createUpdateReceiver(), - inlineQueryChannel.createUpdateReceiver(), - callbackQueryChannel.createUpdateReceiver(), - shippingQueryChannel.createUpdateReceiver(), - preCheckoutQueryChannel.createUpdateReceiver(), - pollChannel.createUpdateReceiver(), - pollAnswerChannel.createUpdateReceiver(), - unknownUpdateChannel.createUpdateReceiver() - ) - - val messageFlow: Flow = messageChannel.asFlow() - val messageMediaGroupFlow: Flow = messageMediaGroupChannel.asFlow() - val editedMessageFlow: Flow = editedMessageChannel.asFlow() - val editedMessageMediaGroupFlow: Flow = editedMessageMediaGroupChannel.asFlow() - val channelPostFlow: Flow = channelPostChannel.asFlow() - val channelPostMediaGroupFlow: Flow = channelPostMediaGroupChannel.asFlow() - val editedChannelPostFlow: Flow = editedChannelPostChannel.asFlow() - val editedChannelPostMediaGroupFlow: Flow = editedChannelPostMediaGroupChannel.asFlow() - val chosenInlineResultFlow: Flow = chosenInlineResultChannel.asFlow() - val inlineQueryFlow: Flow = inlineQueryChannel.asFlow() - val callbackQueryFlow: Flow = callbackQueryChannel.asFlow() - val shippingQueryFlow: Flow = shippingQueryChannel.asFlow() - val preCheckoutQueryFlow: Flow = preCheckoutQueryChannel.asFlow() - val pollFlow: Flow = pollChannel.asFlow() - val pollAnswerFlow: Flow = pollAnswerChannel.asFlow() - val unknownUpdateTypeFlow: Flow = unknownUpdateChannel.asFlow() + val messageFlow: Flow = allUpdatesFlow.filterIsInstance() + val messageMediaGroupFlow: Flow = allUpdatesFlow.filterIsInstance() + val editedMessageFlow: Flow = allUpdatesFlow.filterIsInstance() + val editedMessageMediaGroupFlow: Flow = allUpdatesFlow.filterIsInstance() + val channelPostFlow: Flow = allUpdatesFlow.filterIsInstance() + val channelPostMediaGroupFlow: Flow = allUpdatesFlow.filterIsInstance() + val editedChannelPostFlow: Flow = allUpdatesFlow.filterIsInstance() + val editedChannelPostMediaGroupFlow: Flow = allUpdatesFlow.filterIsInstance() + val chosenInlineResultFlow: Flow = allUpdatesFlow.filterIsInstance() + val inlineQueryFlow: Flow = allUpdatesFlow.filterIsInstance() + val callbackQueryFlow: Flow = allUpdatesFlow.filterIsInstance() + val shippingQueryFlow: Flow = allUpdatesFlow.filterIsInstance() + val preCheckoutQueryFlow: Flow = allUpdatesFlow.filterIsInstance() + val pollFlow: Flow = allUpdatesFlow.filterIsInstance() + val pollAnswerFlow: Flow = allUpdatesFlow.filterIsInstance() + val unknownUpdateTypeFlow: Flow = allUpdatesFlow.filterIsInstance() } \ No newline at end of file diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt index 868484ccb1..91fedfc184 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/updateshandlers/UpdatesFilter.kt @@ -3,8 +3,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.updateshandlers import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.* -import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UnknownUpdateType -import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.* typealias UpdateReceiver = suspend (T) -> Unit @@ -29,7 +28,7 @@ data class SimpleUpdatesFilter( private val preCheckoutQueryCallback: UpdateReceiver? = null, private val pollUpdateCallback: UpdateReceiver? = null, private val pollAnswerUpdateCallback: UpdateReceiver? = null, - private val unknownUpdateTypeCallback: UpdateReceiver? = null + private val unknownUpdateTypeCallback: UpdateReceiver? = null ) : UpdatesFilter { override val asUpdateReceiver: UpdateReceiver = this::invoke override val allowedUpdates = listOfNotNull( @@ -83,7 +82,7 @@ data class SimpleUpdatesFilter( is PreCheckoutQueryUpdate -> preCheckoutQueryCallback ?.invoke(update) is PollUpdate -> pollUpdateCallback ?.invoke(update) is PollAnswerUpdate -> pollAnswerUpdateCallback ?.invoke(update) - is UnknownUpdateType -> unknownUpdateTypeCallback ?.invoke(update) + is UnknownUpdate -> unknownUpdateTypeCallback ?.invoke(update) } } } @@ -101,7 +100,7 @@ fun createSimpleUpdateFilter( preCheckoutQueryCallback: UpdateReceiver? = null, pollCallback: UpdateReceiver? = null, pollAnswerCallback: UpdateReceiver? = null, - unknownCallback: UpdateReceiver? = null + unknownCallback: UpdateReceiver? = null ): UpdatesFilter = SimpleUpdatesFilter( messageCallback = messageCallback, messageMediaGroupCallback = mediaGroupCallback,