From ad9bc36f8c7b8ec526535be4bcb928aeacc16622 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 14 Oct 2021 23:05:11 +0600 Subject: [PATCH] all triggers in behaviour builder now use accumulator flow --- .../extensions/behaviour_builder/BehaviourContext.kt | 11 ++++++++--- .../triggers_handling/MainTrigger.kt | 7 +++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt index c81e7a5981..11fdc2142b 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt @@ -144,16 +144,19 @@ suspend fun BC.doInSubContextWithFlowsUpdatesFilterSe suspend fun BC.doInSubContextWithUpdatesFilter( updatesFilter: CustomBehaviourContextAndTypeReceiver?, stopOnCompletion: Boolean = true, + updatesUpstreamFlow: Flow = allUpdatesFlow, + scope: CoroutineScope = LinkedSupervisorScope(), behaviourContextReceiver: CustomBehaviourContextReceiver ): T = copy( - scope = LinkedSupervisorScope(), + scope = scope, updatesFilter = updatesFilter ?.let { _ -> { (this as? BC) ?.run { updatesFilter(it) } ?: true } - } + }, + upstreamUpdatesFlow = updatesUpstreamFlow ).run { withContext(coroutineContext) { behaviourContextReceiver().also { if (stopOnCompletion) stop() } @@ -162,8 +165,10 @@ suspend fun BC.doInSubContextWithUpdatesFilter( suspend fun BehaviourContext.doInSubContext( stopOnCompletion: Boolean = true, + updatesUpstreamFlow: Flow = allUpdatesFlow, + scope: CoroutineScope = LinkedSupervisorScope(), behaviourContextReceiver: BehaviourContextReceiver -) = doInSubContextWithUpdatesFilter(updatesFilter = null, stopOnCompletion, behaviourContextReceiver) +) = doInSubContextWithUpdatesFilter(updatesFilter = null, stopOnCompletion, updatesUpstreamFlow, scope, behaviourContextReceiver) /** * This method will cancel ALL subsequent contexts, expectations and waiters diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt index fc561aa605..6618479bec 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt @@ -1,6 +1,6 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling -import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptionsAsync +import dev.inmo.micro_utils.coroutines.* 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 @@ -21,9 +21,12 @@ internal suspend inline fun BC.on( scope, markerFactory::invoke ) { triggerData -> + val scope = LinkedSupervisorScope() doInSubContextWithUpdatesFilter( updatesFilter = subcontextUpdatesFilter ?.toOneType(triggerData), - stopOnCompletion = false + stopOnCompletion = false, + updatesUpstreamFlow = allUpdatesFlow.accumulatorFlow(scope), + scope = scope ) { scenarioReceiver(triggerData) }