From f76e88c880b5d6d93cd302844b25097721cb40fa Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 5 Sep 2024 00:48:45 +0600 Subject: [PATCH] update createSubContextAndDoWithUpdatesFilter logic --- CHANGELOG.md | 4 ++ .../behaviour_builder/BehaviourContext.kt | 48 ++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b854397a4..7f3942074f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 18.1.0 +* `BehaviourBuilder`: + * Add `createSubContextAndDoSynchronouslyWithUpdatesFilter` as old logic of `createSubContextAndDoWithUpdatesFilter` + * `createSubContextAndDoWithUpdatesFilter` has been renamed to `createSubContextAndDoAsynchronouslyWithUpdatesFilter` + ## 18.0.0 **THIS UPDATE CONTAINS BREAKING CHANGES** diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt index 1f96aef8cd..e430e22578 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt @@ -11,6 +11,8 @@ import dev.inmo.tgbotapi.updateshandlers.* import kotlinx.coroutines.* import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.flow.* +import kotlin.coroutines.CoroutineContext +import kotlin.coroutines.EmptyCoroutineContext typealias CustomBehaviourContextReceiver = suspend BC.() -> T typealias BehaviourContextReceiver = CustomBehaviourContextReceiver @@ -145,9 +147,12 @@ suspend fun BC.doInContext( /** * Creates new one [BehaviourContext] using [createSubContext] and launches [behaviourContextReceiver] in a new context - * using [doInContext] + * using [doInContext]. + * + * This action will be executed in **synchronous** manner which means that until the context created with + * [createSubContext] will be done this function will not let execution of code continue */ -suspend fun BC.createSubContextAndDoWithUpdatesFilter( +suspend fun BC.createSubContextAndDoSynchronouslyWithUpdatesFilter( triggersHolder: TriggersHolder = this.triggersHolder, updatesUpstreamFlow: Flow = allUpdatesFlow, behaviourContextReceiver: CustomBehaviourContextReceiver @@ -161,6 +166,45 @@ suspend fun BC.createSubContextAndDoWithUpdatesFilter } } +/** + * Uses [createSubContextAndDoSynchronouslyWithUpdatesFilter], but wrapping it in [async]. That means, that + * execution of this function will be **asynchronous** and **will not** block execution of code by default + */ +suspend fun BC.createSubContextAndDoAsynchronouslyWithUpdatesFilter( + triggersHolder: TriggersHolder = this.triggersHolder, + updatesUpstreamFlow: Flow = allUpdatesFlow, + context: CoroutineContext = EmptyCoroutineContext, + start: CoroutineStart = CoroutineStart.DEFAULT, + behaviourContextReceiver: CustomBehaviourContextReceiver +): Deferred = async( + context, + start +) { + createSubContextAndDoSynchronouslyWithUpdatesFilter( + triggersHolder, + updatesUpstreamFlow, + behaviourContextReceiver + ) +} + +/** + * It is just backward compatibility function which will be removed in next updates. + * + * Uses [createSubContextAndDoAsynchronouslyWithUpdatesFilter] under the hood with passing of parameters as is + */ +@Deprecated( + "Renamed", + ReplaceWith( + "createSubContextAndDoAsynchronouslyWithUpdatesFilter(triggersHolder, updatesUpstreamFlow, behaviourContextReceiver = behaviourContextReceiver)", + "dev.inmo.tgbotapi.extensions.behaviour_builder.createSubContextAndDoAsynchronouslyWithUpdatesFilter" + ) +) +suspend fun BC.createSubContextAndDoWithUpdatesFilter( + triggersHolder: TriggersHolder = this.triggersHolder, + updatesUpstreamFlow: Flow = allUpdatesFlow, + behaviourContextReceiver: CustomBehaviourContextReceiver +): Deferred = createSubContextAndDoAsynchronouslyWithUpdatesFilter(triggersHolder, updatesUpstreamFlow, behaviourContextReceiver = behaviourContextReceiver) + /** * This method will cancel ALL subsequent contexts, expectations and waiters */