diff --git a/CHANGELOG.md b/CHANGELOG.md index 06383db851..c33d4a67d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## 24.0.0 +**THIS UPDATE CONTAINS BREAKING CHANGES IN `subcontextUpdatesFilter` WORK. TAKE CARE IN MIGRATION** + +* `BehaviourBuilder`: + * Fix of overall `subcontextUpdatesFilter` behaviour. In fact, this update will fix its affection on scenaries + ## 23.2.1 * `Core`: diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt index 8e76465ffa..c04acadf9f 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt @@ -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.on( markerFactory: MarkerFactory?, @@ -40,11 +47,21 @@ internal suspend fun BC.on( } val handler: suspend (Pair) -> Unit = subcontextUpdatesFilter ?.let { { (update, triggerData) -> - createSubContextAndDoSynchronouslyWithUpdatesFilter { - if (subcontextUpdatesFilter(this, triggerData, update)) { - localSubcontextInitialAction(update, triggerData) - scenarioReceiver(triggerData) + val contextStateFlow = SpecialMutableStateFlow(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.on( { markerFactory(it.second) }, block = handler ) - } ?: subscribeSafelyWithoutExceptions(scope) { - scope.launchSafelyWithoutExceptions { + } ?: subscribeLoggingDropExceptions(scope) { + scope.launchLoggingDropExceptions { handler(it) } }