diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ea300a604..333c7236b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## 0.32.7 +* `Behaviour Builder`: + * Now `doInSubContextWithUpdatesFilter` and `doInSubContext` will automatically subscribe on updates of parent + `BehaviourContext` + * `doInSubContextWithFlowsUpdatesFilterSetup`, `doInSubContextWithUpdatesFilter` and `doInSubContext` got new + parameter `stopOnCompletion` to be able to disable stopping of behaviour context on finishing + ## 0.32.6 * `Common`: 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 d881a5f1ba..4ff49827ce 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 @@ -4,6 +4,7 @@ import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter +import dev.inmo.tgbotapi.utils.RiskFeature import kotlinx.coroutines.* import kotlinx.coroutines.flow.filter @@ -25,12 +26,15 @@ data class BehaviourContext( /** * Creates new one [BehaviourContext], adding subsequent [FlowsUpdatesFilter] in case [newFlowsUpdatesFilterSetUp] is provided and - * [CoroutineScope] as new [BehaviourContext.scope] + * [CoroutineScope] as new [BehaviourContext.scope]. You must do all subscription/running of longPolling manually. * * @param newFlowsUpdatesFilterSetUp As a parameter receives [FlowsUpdatesFilter] from old [this] [BehaviourContext.flowsUpdatesFilter] */ +@RiskFeature("It is recommended to use doInSubContextWithUpdatesFilter instead. " + + "This method is low level and should not be used in case you are not pretty sure you need it.") suspend fun BehaviourContext.doInSubContextWithFlowsUpdatesFilterSetup( newFlowsUpdatesFilterSetUp: BehaviourContextAndTypeReceiver?, + stopOnCompletion: Boolean = true, behaviourContextReceiver: BehaviourContextReceiver ) = copy( flowsUpdatesFilter = FlowsUpdatesFilter(), @@ -39,7 +43,7 @@ suspend fun BehaviourContext.doInSubContextWithFlowsUpdatesFilterSetup( newFlowsUpdatesFilterSetUp ?.let { it.apply { invoke(this@run, this@doInSubContextWithFlowsUpdatesFilterSetup.flowsUpdatesFilter) } } - behaviourContextReceiver().also { stop() } + behaviourContextReceiver().also { if (stopOnCompletion) stop() } } /** @@ -48,19 +52,24 @@ suspend fun BehaviourContext.doInSubContextWithFlowsUpdatesFilterSetup( */ suspend fun BehaviourContext.doInSubContextWithUpdatesFilter( updatesFilter: BehaviourContextAndTypeReceiver?, + stopOnCompletion: Boolean = true, behaviourContextReceiver: BehaviourContextReceiver ) = doInSubContextWithFlowsUpdatesFilterSetup( newFlowsUpdatesFilterSetUp = updatesFilter ?.let { { oldOne -> oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(scope, asUpdateReceiver) } + } ?: { oldOne -> + oldOne.allUpdatesFlow.subscribeSafelyWithoutExceptions(scope, asUpdateReceiver) }, + stopOnCompletion, behaviourContextReceiver ) suspend fun BehaviourContext.doInSubContext( + stopOnCompletion: Boolean = true, behaviourContextReceiver: BehaviourContextReceiver -) = doInSubContextWithFlowsUpdatesFilterSetup(newFlowsUpdatesFilterSetUp = null, behaviourContextReceiver) +) = doInSubContextWithUpdatesFilter(updatesFilter = null, stopOnCompletion, behaviourContextReceiver) /** * This method will cancel ALL subsequent contexts, expectations and waiters