mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 16:23:48 +00:00
refactoring of FlowsUpdatesFilter
This commit is contained in:
parent
58943f2504
commit
a9a3f55c8d
@ -51,6 +51,10 @@
|
|||||||
|
|
||||||
### 0.27.8
|
### 0.27.8
|
||||||
|
|
||||||
|
* `TelegramBotAPI`:
|
||||||
|
* `UnknownUpdateType` was renamed to `UnknownUpdate`
|
||||||
|
* Refactoring and optimization of `FlowsUpdatesFilter`
|
||||||
|
|
||||||
### 0.27.7
|
### 0.27.7
|
||||||
|
|
||||||
* `TelegramBotAPI`:
|
* `TelegramBotAPI`:
|
||||||
|
@ -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.payments.ShippingQuery
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.polls.Poll
|
import com.github.insanusmokrassar.TelegramBotAPI.types.polls.Poll
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.polls.PollAnswer
|
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.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.updateIdField
|
import com.github.insanusmokrassar.TelegramBotAPI.types.updateIdField
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.*
|
||||||
import kotlinx.serialization.json.JsonElement
|
import kotlinx.serialization.json.JsonElement
|
||||||
@ -57,7 +56,7 @@ internal data class RawUpdate constructor(
|
|||||||
pre_checkout_query != null -> PreCheckoutQueryUpdate(updateId, pre_checkout_query)
|
pre_checkout_query != null -> PreCheckoutQueryUpdate(updateId, pre_checkout_query)
|
||||||
poll != null -> PollUpdate(updateId, poll)
|
poll != null -> PollUpdate(updateId, poll)
|
||||||
poll_answer != null -> PollAnswerUpdate(updateId, poll_answer)
|
poll_answer != null -> PollAnswerUpdate(updateId, poll_answer)
|
||||||
else -> UnknownUpdateType(
|
else -> UnknownUpdate(
|
||||||
updateId,
|
updateId,
|
||||||
raw.toString(),
|
raw.toString(),
|
||||||
raw
|
raw
|
||||||
@ -66,7 +65,7 @@ internal data class RawUpdate constructor(
|
|||||||
} catch (e: Error) {
|
} catch (e: Error) {
|
||||||
when (e) {
|
when (e) {
|
||||||
is SerializationException,
|
is SerializationException,
|
||||||
is NotImplementedError -> UnknownUpdateType(
|
is NotImplementedError -> UnknownUpdate(
|
||||||
updateId,
|
updateId,
|
||||||
raw.toString(),
|
raw.toString(),
|
||||||
raw
|
raw
|
||||||
|
@ -12,11 +12,13 @@ interface Update {
|
|||||||
val data: Any
|
val data: Any
|
||||||
}
|
}
|
||||||
|
|
||||||
data class UnknownUpdateType(
|
data class UnknownUpdate(
|
||||||
override val updateId: UpdateIdentifier,
|
override val updateId: UpdateIdentifier,
|
||||||
override val data: String,
|
override val data: String,
|
||||||
val rawJson: JsonElement
|
val rawJson: JsonElement
|
||||||
) : Update
|
) : Update
|
||||||
|
@Deprecated("Renamed", ReplaceWith("UnknownUpdate"))
|
||||||
|
typealias UnknownUpdateType = UnknownUpdate
|
||||||
|
|
||||||
internal object UpdateSerializerWithoutSerialization : KSerializer<Update> {
|
internal object UpdateSerializerWithoutSerialization : KSerializer<Update> {
|
||||||
override val descriptor: SerialDescriptor = JsonElementSerializer.descriptor
|
override val descriptor: SerialDescriptor = JsonElementSerializer.descriptor
|
||||||
|
@ -1,74 +1,40 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.updateshandlers
|
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.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
|
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.channels.BroadcastChannel
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.*
|
||||||
import kotlinx.coroutines.flow.asFlow
|
|
||||||
|
|
||||||
@Suppress("EXPERIMENTAL_API_USAGE")
|
|
||||||
private fun <T> BroadcastChannel<T>.createUpdateReceiver(): UpdateReceiver<T> = ::send
|
|
||||||
|
|
||||||
@Suppress("EXPERIMENTAL_API_USAGE", "unused")
|
@Suppress("EXPERIMENTAL_API_USAGE", "unused")
|
||||||
class FlowsUpdatesFilter(
|
class FlowsUpdatesFilter(
|
||||||
broadcastChannelsSize: Int = 64
|
broadcastChannelsSize: Int = 100
|
||||||
): UpdatesFilter {
|
): UpdatesFilter {
|
||||||
private val messageChannel: BroadcastChannel<MessageUpdate> = BroadcastChannel(broadcastChannelsSize)
|
private val updatesReceivingChannel = BroadcastChannel<Update>(broadcastChannelsSize)
|
||||||
private val messageMediaGroupChannel: BroadcastChannel<MessageMediaGroupUpdate> = BroadcastChannel(broadcastChannelsSize)
|
@Suppress("MemberVisibilityCanBePrivate")
|
||||||
private val editedMessageChannel: BroadcastChannel<EditMessageUpdate> = BroadcastChannel(broadcastChannelsSize)
|
val allUpdatesFlow: Flow<Update> = updatesReceivingChannel.asFlow()
|
||||||
private val editedMessageMediaGroupChannel: BroadcastChannel<EditMessageMediaGroupUpdate> = BroadcastChannel(broadcastChannelsSize)
|
|
||||||
private val channelPostChannel: BroadcastChannel<ChannelPostUpdate> = BroadcastChannel(broadcastChannelsSize)
|
|
||||||
private val channelPostMediaGroupChannel: BroadcastChannel<ChannelPostMediaGroupUpdate> = BroadcastChannel(broadcastChannelsSize)
|
|
||||||
private val editedChannelPostChannel: BroadcastChannel<EditChannelPostUpdate> = BroadcastChannel(broadcastChannelsSize)
|
|
||||||
private val editedChannelPostMediaGroupChannel: BroadcastChannel<EditChannelPostMediaGroupUpdate> = BroadcastChannel(broadcastChannelsSize)
|
|
||||||
private val chosenInlineResultChannel: BroadcastChannel<ChosenInlineResultUpdate> = BroadcastChannel(broadcastChannelsSize)
|
|
||||||
private val inlineQueryChannel: BroadcastChannel<InlineQueryUpdate> = BroadcastChannel(broadcastChannelsSize)
|
|
||||||
private val callbackQueryChannel: BroadcastChannel<CallbackQueryUpdate> = BroadcastChannel(broadcastChannelsSize)
|
|
||||||
private val shippingQueryChannel: BroadcastChannel<ShippingQueryUpdate> = BroadcastChannel(broadcastChannelsSize)
|
|
||||||
private val preCheckoutQueryChannel: BroadcastChannel<PreCheckoutQueryUpdate> = BroadcastChannel(broadcastChannelsSize)
|
|
||||||
private val pollChannel: BroadcastChannel<PollUpdate> = BroadcastChannel(broadcastChannelsSize)
|
|
||||||
private val pollAnswerChannel: BroadcastChannel<PollAnswerUpdate> = BroadcastChannel(broadcastChannelsSize)
|
|
||||||
private val unknownUpdateChannel: BroadcastChannel<Update> = BroadcastChannel(broadcastChannelsSize)
|
|
||||||
|
|
||||||
override val allowedUpdates: List<String>
|
override val allowedUpdates: List<String>
|
||||||
get() = filter.allowedUpdates
|
get() = ALL_UPDATES_LIST
|
||||||
override val asUpdateReceiver: UpdateReceiver<Update>
|
override val asUpdateReceiver: UpdateReceiver<Update> = {
|
||||||
get() = filter.asUpdateReceiver
|
updatesReceivingChannel.send(it)
|
||||||
|
}
|
||||||
val filter = SimpleUpdatesFilter(
|
|
||||||
messageChannel.createUpdateReceiver(),
|
val messageFlow: Flow<MessageUpdate> = allUpdatesFlow.filterIsInstance()
|
||||||
messageMediaGroupChannel.createUpdateReceiver(),
|
val messageMediaGroupFlow: Flow<MessageMediaGroupUpdate> = allUpdatesFlow.filterIsInstance()
|
||||||
editedMessageChannel.createUpdateReceiver(),
|
val editedMessageFlow: Flow<EditMessageUpdate> = allUpdatesFlow.filterIsInstance()
|
||||||
editedMessageMediaGroupChannel.createUpdateReceiver(),
|
val editedMessageMediaGroupFlow: Flow<EditMessageMediaGroupUpdate> = allUpdatesFlow.filterIsInstance()
|
||||||
channelPostChannel.createUpdateReceiver(),
|
val channelPostFlow: Flow<ChannelPostUpdate> = allUpdatesFlow.filterIsInstance()
|
||||||
channelPostMediaGroupChannel.createUpdateReceiver(),
|
val channelPostMediaGroupFlow: Flow<ChannelPostMediaGroupUpdate> = allUpdatesFlow.filterIsInstance()
|
||||||
editedChannelPostChannel.createUpdateReceiver(),
|
val editedChannelPostFlow: Flow<EditChannelPostUpdate> = allUpdatesFlow.filterIsInstance()
|
||||||
editedChannelPostMediaGroupChannel.createUpdateReceiver(),
|
val editedChannelPostMediaGroupFlow: Flow<EditChannelPostMediaGroupUpdate> = allUpdatesFlow.filterIsInstance()
|
||||||
chosenInlineResultChannel.createUpdateReceiver(),
|
val chosenInlineResultFlow: Flow<ChosenInlineResultUpdate> = allUpdatesFlow.filterIsInstance()
|
||||||
inlineQueryChannel.createUpdateReceiver(),
|
val inlineQueryFlow: Flow<InlineQueryUpdate> = allUpdatesFlow.filterIsInstance()
|
||||||
callbackQueryChannel.createUpdateReceiver(),
|
val callbackQueryFlow: Flow<CallbackQueryUpdate> = allUpdatesFlow.filterIsInstance()
|
||||||
shippingQueryChannel.createUpdateReceiver(),
|
val shippingQueryFlow: Flow<ShippingQueryUpdate> = allUpdatesFlow.filterIsInstance()
|
||||||
preCheckoutQueryChannel.createUpdateReceiver(),
|
val preCheckoutQueryFlow: Flow<PreCheckoutQueryUpdate> = allUpdatesFlow.filterIsInstance()
|
||||||
pollChannel.createUpdateReceiver(),
|
val pollFlow: Flow<PollUpdate> = allUpdatesFlow.filterIsInstance()
|
||||||
pollAnswerChannel.createUpdateReceiver(),
|
val pollAnswerFlow: Flow<PollAnswerUpdate> = allUpdatesFlow.filterIsInstance()
|
||||||
unknownUpdateChannel.createUpdateReceiver()
|
val unknownUpdateTypeFlow: Flow<UnknownUpdate> = allUpdatesFlow.filterIsInstance()
|
||||||
)
|
|
||||||
|
|
||||||
val messageFlow: Flow<MessageUpdate> = messageChannel.asFlow()
|
|
||||||
val messageMediaGroupFlow: Flow<MessageMediaGroupUpdate> = messageMediaGroupChannel.asFlow()
|
|
||||||
val editedMessageFlow: Flow<EditMessageUpdate> = editedMessageChannel.asFlow()
|
|
||||||
val editedMessageMediaGroupFlow: Flow<EditMessageMediaGroupUpdate> = editedMessageMediaGroupChannel.asFlow()
|
|
||||||
val channelPostFlow: Flow<ChannelPostUpdate> = channelPostChannel.asFlow()
|
|
||||||
val channelPostMediaGroupFlow: Flow<ChannelPostMediaGroupUpdate> = channelPostMediaGroupChannel.asFlow()
|
|
||||||
val editedChannelPostFlow: Flow<EditChannelPostUpdate> = editedChannelPostChannel.asFlow()
|
|
||||||
val editedChannelPostMediaGroupFlow: Flow<EditChannelPostMediaGroupUpdate> = editedChannelPostMediaGroupChannel.asFlow()
|
|
||||||
val chosenInlineResultFlow: Flow<ChosenInlineResultUpdate> = chosenInlineResultChannel.asFlow()
|
|
||||||
val inlineQueryFlow: Flow<InlineQueryUpdate> = inlineQueryChannel.asFlow()
|
|
||||||
val callbackQueryFlow: Flow<CallbackQueryUpdate> = callbackQueryChannel.asFlow()
|
|
||||||
val shippingQueryFlow: Flow<ShippingQueryUpdate> = shippingQueryChannel.asFlow()
|
|
||||||
val preCheckoutQueryFlow: Flow<PreCheckoutQueryUpdate> = preCheckoutQueryChannel.asFlow()
|
|
||||||
val pollFlow: Flow<PollUpdate> = pollChannel.asFlow()
|
|
||||||
val pollAnswerFlow: Flow<PollAnswerUpdate> = pollAnswerChannel.asFlow()
|
|
||||||
val unknownUpdateTypeFlow: Flow<Update> = unknownUpdateChannel.asFlow()
|
|
||||||
}
|
}
|
@ -3,8 +3,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.updateshandlers
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
|
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.*
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
|
||||||
|
|
||||||
typealias UpdateReceiver<T> = suspend (T) -> Unit
|
typealias UpdateReceiver<T> = suspend (T) -> Unit
|
||||||
|
|
||||||
@ -29,7 +28,7 @@ data class SimpleUpdatesFilter(
|
|||||||
private val preCheckoutQueryCallback: UpdateReceiver<PreCheckoutQueryUpdate>? = null,
|
private val preCheckoutQueryCallback: UpdateReceiver<PreCheckoutQueryUpdate>? = null,
|
||||||
private val pollUpdateCallback: UpdateReceiver<PollUpdate>? = null,
|
private val pollUpdateCallback: UpdateReceiver<PollUpdate>? = null,
|
||||||
private val pollAnswerUpdateCallback: UpdateReceiver<PollAnswerUpdate>? = null,
|
private val pollAnswerUpdateCallback: UpdateReceiver<PollAnswerUpdate>? = null,
|
||||||
private val unknownUpdateTypeCallback: UpdateReceiver<UnknownUpdateType>? = null
|
private val unknownUpdateTypeCallback: UpdateReceiver<UnknownUpdate>? = null
|
||||||
) : UpdatesFilter {
|
) : UpdatesFilter {
|
||||||
override val asUpdateReceiver: UpdateReceiver<Update> = this::invoke
|
override val asUpdateReceiver: UpdateReceiver<Update> = this::invoke
|
||||||
override val allowedUpdates = listOfNotNull(
|
override val allowedUpdates = listOfNotNull(
|
||||||
@ -83,7 +82,7 @@ data class SimpleUpdatesFilter(
|
|||||||
is PreCheckoutQueryUpdate -> preCheckoutQueryCallback ?.invoke(update)
|
is PreCheckoutQueryUpdate -> preCheckoutQueryCallback ?.invoke(update)
|
||||||
is PollUpdate -> pollUpdateCallback ?.invoke(update)
|
is PollUpdate -> pollUpdateCallback ?.invoke(update)
|
||||||
is PollAnswerUpdate -> pollAnswerUpdateCallback ?.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<PreCheckoutQueryUpdate>? = null,
|
preCheckoutQueryCallback: UpdateReceiver<PreCheckoutQueryUpdate>? = null,
|
||||||
pollCallback: UpdateReceiver<PollUpdate>? = null,
|
pollCallback: UpdateReceiver<PollUpdate>? = null,
|
||||||
pollAnswerCallback: UpdateReceiver<PollAnswerUpdate>? = null,
|
pollAnswerCallback: UpdateReceiver<PollAnswerUpdate>? = null,
|
||||||
unknownCallback: UpdateReceiver<UnknownUpdateType>? = null
|
unknownCallback: UpdateReceiver<UnknownUpdate>? = null
|
||||||
): UpdatesFilter = SimpleUpdatesFilter(
|
): UpdatesFilter = SimpleUpdatesFilter(
|
||||||
messageCallback = messageCallback,
|
messageCallback = messageCallback,
|
||||||
messageMediaGroupCallback = mediaGroupCallback,
|
messageMediaGroupCallback = mediaGroupCallback,
|
||||||
|
Loading…
Reference in New Issue
Block a user