mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-09-03 07:09:23 +00:00
fix of subcontextUpdatesFilter
This commit is contained in:
@@ -1,13 +1,20 @@
|
||||
package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling
|
||||
|
||||
import dev.inmo.micro_utils.coroutines.SpecialMutableStateFlow
|
||||
import dev.inmo.micro_utils.coroutines.launchLoggingDropExceptions
|
||||
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
|
||||
import dev.inmo.micro_utils.coroutines.subscribeLoggingDropExceptions
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptionsAsync
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
|
||||
import dev.inmo.tgbotapi.extensions.utils.flatMap
|
||||
import dev.inmo.tgbotapi.types.update.abstracts.Update
|
||||
import kotlinx.coroutines.flow.emptyFlow
|
||||
import kotlinx.coroutines.flow.filter
|
||||
import kotlinx.coroutines.flow.flatMapLatest
|
||||
|
||||
internal suspend fun <BC : BehaviourContext, T> BC.on(
|
||||
markerFactory: MarkerFactory<in T, Any>?,
|
||||
@@ -40,11 +47,21 @@ internal suspend fun <BC : BehaviourContext, T> BC.on(
|
||||
}
|
||||
val handler: suspend (Pair<Update, T>) -> Unit = subcontextUpdatesFilter ?.let {
|
||||
{ (update, triggerData) ->
|
||||
createSubContextAndDoSynchronouslyWithUpdatesFilter {
|
||||
if (subcontextUpdatesFilter(this, triggerData, update)) {
|
||||
localSubcontextInitialAction(update, triggerData)
|
||||
scenarioReceiver(triggerData)
|
||||
val contextStateFlow = SpecialMutableStateFlow<BC?>(null)
|
||||
createSubContextAndDoSynchronouslyWithUpdatesFilter(
|
||||
updatesUpstreamFlow = contextStateFlow.flatMapLatest { context ->
|
||||
if (context == null) {
|
||||
emptyFlow()
|
||||
} else {
|
||||
allUpdatesFlow.filter {
|
||||
context.subcontextUpdatesFilter(triggerData, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
) {
|
||||
contextStateFlow.value = this
|
||||
localSubcontextInitialAction(update, triggerData)
|
||||
scenarioReceiver(triggerData)
|
||||
}
|
||||
}
|
||||
} ?: { (update, triggerData) ->
|
||||
@@ -57,8 +74,8 @@ internal suspend fun <BC : BehaviourContext, T> BC.on(
|
||||
{ markerFactory(it.second) },
|
||||
block = handler
|
||||
)
|
||||
} ?: subscribeSafelyWithoutExceptions(scope) {
|
||||
scope.launchSafelyWithoutExceptions {
|
||||
} ?: subscribeLoggingDropExceptions(scope) {
|
||||
scope.launchLoggingDropExceptions {
|
||||
handler(it)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user