1
0
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:
2025-02-18 23:09:28 +06:00
parent 974c2e883f
commit 6cb9950b36
2 changed files with 28 additions and 6 deletions

View File

@@ -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`:

View File

@@ -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)
}
}