1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 12:08:43 +00:00

all triggers in behaviour builder now use accumulator flow

This commit is contained in:
InsanusMokrassar 2021-10-14 23:05:11 +06:00
parent 5a192b00d4
commit ad9bc36f8c
2 changed files with 13 additions and 5 deletions

View File

@ -144,16 +144,19 @@ suspend fun <T, BC : BehaviourContext> BC.doInSubContextWithFlowsUpdatesFilterSe
suspend fun <T, BC : BehaviourContext> BC.doInSubContextWithUpdatesFilter( suspend fun <T, BC : BehaviourContext> BC.doInSubContextWithUpdatesFilter(
updatesFilter: CustomBehaviourContextAndTypeReceiver<BC, Boolean, Update>?, updatesFilter: CustomBehaviourContextAndTypeReceiver<BC, Boolean, Update>?,
stopOnCompletion: Boolean = true, stopOnCompletion: Boolean = true,
updatesUpstreamFlow: Flow<Update> = allUpdatesFlow,
scope: CoroutineScope = LinkedSupervisorScope(),
behaviourContextReceiver: CustomBehaviourContextReceiver<BC, T> behaviourContextReceiver: CustomBehaviourContextReceiver<BC, T>
): T = copy( ): T = copy(
scope = LinkedSupervisorScope(), scope = scope,
updatesFilter = updatesFilter ?.let { _ -> updatesFilter = updatesFilter ?.let { _ ->
{ {
(this as? BC) ?.run { (this as? BC) ?.run {
updatesFilter(it) updatesFilter(it)
} ?: true } ?: true
} }
} },
upstreamUpdatesFlow = updatesUpstreamFlow
).run { ).run {
withContext(coroutineContext) { withContext(coroutineContext) {
behaviourContextReceiver().also { if (stopOnCompletion) stop() } behaviourContextReceiver().also { if (stopOnCompletion) stop() }
@ -162,8 +165,10 @@ suspend fun <T, BC : BehaviourContext> BC.doInSubContextWithUpdatesFilter(
suspend fun <T> BehaviourContext.doInSubContext( suspend fun <T> BehaviourContext.doInSubContext(
stopOnCompletion: Boolean = true, stopOnCompletion: Boolean = true,
updatesUpstreamFlow: Flow<Update> = allUpdatesFlow,
scope: CoroutineScope = LinkedSupervisorScope(),
behaviourContextReceiver: BehaviourContextReceiver<T> behaviourContextReceiver: BehaviourContextReceiver<T>
) = doInSubContextWithUpdatesFilter(updatesFilter = null, stopOnCompletion, behaviourContextReceiver) ) = doInSubContextWithUpdatesFilter(updatesFilter = null, stopOnCompletion, updatesUpstreamFlow, scope, behaviourContextReceiver)
/** /**
* This method will cancel ALL subsequent contexts, expectations and waiters * This method will cancel ALL subsequent contexts, expectations and waiters

View File

@ -1,6 +1,6 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling 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.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow 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.SimpleFilter
@ -21,9 +21,12 @@ internal suspend inline fun <BC : BehaviourContext, reified T> BC.on(
scope, scope,
markerFactory::invoke markerFactory::invoke
) { triggerData -> ) { triggerData ->
val scope = LinkedSupervisorScope()
doInSubContextWithUpdatesFilter( doInSubContextWithUpdatesFilter(
updatesFilter = subcontextUpdatesFilter ?.toOneType(triggerData), updatesFilter = subcontextUpdatesFilter ?.toOneType(triggerData),
stopOnCompletion = false stopOnCompletion = false,
updatesUpstreamFlow = allUpdatesFlow.accumulatorFlow(scope),
scope = scope
) { ) {
scenarioReceiver(triggerData) scenarioReceiver(triggerData)
} }