1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-09-15 05:09:30 +00:00

BehaviourBuilder FSM extension

This commit is contained in:
2021-10-13 14:22:01 +06:00
parent 22e71354dd
commit 2a33b22afb
41 changed files with 905 additions and 464 deletions

View File

@@ -57,7 +57,7 @@ Dependency config presented here:
To use last versions you will need to add one line in repositories block of your `build.gradle`:
`jcenter()` or `mavenCentral()`
`mavenCentral()`
And add next line to your dependencies block:

View File

@@ -1,11 +1,12 @@
package dev.inmo.tgbotapi.updateshandlers
import dev.inmo.micro_utils.coroutines.plus
import dev.inmo.tgbotapi.types.ALL_UPDATES_LIST
import dev.inmo.tgbotapi.types.update.*
import dev.inmo.tgbotapi.types.update.MediaGroupUpdates.*
import dev.inmo.tgbotapi.types.update.abstracts.UnknownUpdate
import dev.inmo.tgbotapi.types.update.abstracts.Update
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.flow.*
interface FlowsUpdatesFilter : UpdatesFilter {
@@ -34,22 +35,61 @@ interface FlowsUpdatesFilter : UpdatesFilter {
val unknownUpdatesFlow: Flow<UnknownUpdate>
}
abstract class AbstractFlowsUpdatesFilter : FlowsUpdatesFilter {
override val allUpdatesWithoutMediaGroupsGroupingFlow: Flow<Update>
get() = allUpdatesFlow.flatMapConcat {
when (it) {
is SentMediaGroupUpdate -> it.origins.asFlow()
is EditMediaGroupUpdate -> flowOf(it.origin)
else -> flowOf(it)
}
}
override val messagesFlow: Flow<MessageUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val messageMediaGroupsFlow: Flow<MessageMediaGroupUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val editedMessagesFlow: Flow<EditMessageUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val editedMessageMediaGroupsFlow: Flow<EditMessageMediaGroupUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val channelPostsFlow: Flow<ChannelPostUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val channelPostMediaGroupsFlow: Flow<ChannelPostMediaGroupUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val editedChannelPostsFlow: Flow<EditChannelPostUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val editedChannelPostMediaGroupsFlow: Flow<EditChannelPostMediaGroupUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val chosenInlineResultsFlow: Flow<ChosenInlineResultUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val inlineQueriesFlow: Flow<InlineQueryUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val callbackQueriesFlow: Flow<CallbackQueryUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val shippingQueriesFlow: Flow<ShippingQueryUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val preCheckoutQueriesFlow: Flow<PreCheckoutQueryUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val pollsFlow: Flow<PollUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val pollAnswersFlow: Flow<PollAnswerUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val chatMemberUpdatesFlow: Flow<CommonChatMemberUpdatedUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val myChatMemberUpdatesFlow: Flow<MyChatMemberUpdatedUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
override val unknownUpdatesFlow: Flow<UnknownUpdate> by lazy { allUpdatesFlow.filterIsInstance() }
}
/**
* Creates [DefaultFlowsUpdatesFilter]
*/
@Suppress("FunctionName")
fun FlowsUpdatesFilter(
broadcastChannelsSize: Int = 100
) = DefaultFlowsUpdatesFilter(broadcastChannelsSize)
broadcastChannelsSize: Int = 100,
onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND,
upstreamUpdatesFlow: Flow<Update>? = null
) = DefaultFlowsUpdatesFilter(broadcastChannelsSize, onBufferOverflow, upstreamUpdatesFlow)
@Suppress("unused")
class DefaultFlowsUpdatesFilter(
broadcastChannelsSize: Int = 100,
onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND
): FlowsUpdatesFilter {
private val updatesSharedFlow = MutableSharedFlow<Update>(extraBufferCapacity = broadcastChannelsSize, onBufferOverflow = onBufferOverflow)
onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND,
upstreamUpdatesFlow: Flow<Update>? = null
): AbstractFlowsUpdatesFilter() {
private val additionalUpdatesSharedFlow = MutableSharedFlow<Update>(0, broadcastChannelsSize, onBufferOverflow)
@Suppress("MemberVisibilityCanBePrivate")
override val allUpdatesFlow: Flow<Update> = updatesSharedFlow.asSharedFlow()
override val allUpdatesFlow: Flow<Update> = (additionalUpdatesSharedFlow.asSharedFlow()).let {
if (upstreamUpdatesFlow != null) {
(it + upstreamUpdatesFlow).distinctUntilChanged { old, new -> old.updateId == new.updateId }
} else {
it
}
}
@Suppress("MemberVisibilityCanBePrivate")
override val allUpdatesWithoutMediaGroupsGroupingFlow: Flow<Update> = allUpdatesFlow.flatMapConcat {
when (it) {
@@ -59,26 +99,5 @@ class DefaultFlowsUpdatesFilter(
}
}
override val asUpdateReceiver: UpdateReceiver<Update> = {
updatesSharedFlow.emit(it)
}
override val messagesFlow: Flow<MessageUpdate> = allUpdatesFlow.filterIsInstance()
override val messageMediaGroupsFlow: Flow<MessageMediaGroupUpdate> = allUpdatesFlow.filterIsInstance()
override val editedMessagesFlow: Flow<EditMessageUpdate> = allUpdatesFlow.filterIsInstance()
override val editedMessageMediaGroupsFlow: Flow<EditMessageMediaGroupUpdate> = allUpdatesFlow.filterIsInstance()
override val channelPostsFlow: Flow<ChannelPostUpdate> = allUpdatesFlow.filterIsInstance()
override val channelPostMediaGroupsFlow: Flow<ChannelPostMediaGroupUpdate> = allUpdatesFlow.filterIsInstance()
override val editedChannelPostsFlow: Flow<EditChannelPostUpdate> = allUpdatesFlow.filterIsInstance()
override val editedChannelPostMediaGroupsFlow: Flow<EditChannelPostMediaGroupUpdate> = allUpdatesFlow.filterIsInstance()
override val chosenInlineResultsFlow: Flow<ChosenInlineResultUpdate> = allUpdatesFlow.filterIsInstance()
override val inlineQueriesFlow: Flow<InlineQueryUpdate> = allUpdatesFlow.filterIsInstance()
override val callbackQueriesFlow: Flow<CallbackQueryUpdate> = allUpdatesFlow.filterIsInstance()
override val shippingQueriesFlow: Flow<ShippingQueryUpdate> = allUpdatesFlow.filterIsInstance()
override val preCheckoutQueriesFlow: Flow<PreCheckoutQueryUpdate> = allUpdatesFlow.filterIsInstance()
override val pollsFlow: Flow<PollUpdate> = allUpdatesFlow.filterIsInstance()
override val pollAnswersFlow: Flow<PollAnswerUpdate> = allUpdatesFlow.filterIsInstance()
override val chatMemberUpdatesFlow: Flow<CommonChatMemberUpdatedUpdate> = allUpdatesFlow.filterIsInstance()
override val myChatMemberUpdatesFlow: Flow<MyChatMemberUpdatedUpdate> = allUpdatesFlow.filterIsInstance()
override val unknownUpdatesFlow: Flow<UnknownUpdate> = allUpdatesFlow.filterIsInstance()
override val asUpdateReceiver: UpdateReceiver<Update> = additionalUpdatesSharedFlow::emit
}