diff --git a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt index 45e426ac6e..30ebf58bc3 100644 --- a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt +++ b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt @@ -94,7 +94,7 @@ class DefaultBehaviourContextWithFSM( private val additionalHandlers = mutableListOf>() private var actualHandlersList = additionalHandlers + handlers - private suspend fun getSubContext(context: Any) = updatesFlows.getOrPut(context) { + private fun getSubContext(context: Any) = updatesFlows.getOrPut(context) { createSubContext() } 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 3d3aec4693..00929f64b4 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 @@ -113,7 +113,7 @@ inline fun BehaviourContext( crossinline block: BehaviourContext.() -> T ) = DefaultBehaviourContext(bot, scope, upstreamUpdatesFlow = flowsUpdatesFilter.allUpdatesFlow, triggersHolder = triggersHolder).run(block) -suspend fun BC.createSubContext( +fun BC.createSubContext( scope: CoroutineScope = LinkedSupervisorScope(), triggersHolder: TriggersHolder = this.triggersHolder, updatesUpstreamFlow: Flow = allUpdatesFlow.accumulatorFlow(scope), @@ -132,20 +132,51 @@ suspend fun BC.createSubContext( ) as BC /** - * Creates new one [BehaviourContext], adding subsequent [FlowsUpdatesFilter] in case [updatesFilter] is provided and - * [CoroutineScope] as new [BehaviourContext.scope] + * Launch [behaviourContextReceiver] in context of [this] as [BehaviourContext] and as [kotlin.coroutines.CoroutineContext] + * + * @param stopOnCompletion ___FALSE BY DEFAULT___. Will stop [this] in case if passed true */ suspend fun BC.doInContext( - stopOnCompletion: Boolean = true, + stopOnCompletion: Boolean = false, behaviourContextReceiver: CustomBehaviourContextReceiver ): T { - return behaviourContextReceiver().also { if (stopOnCompletion) stop() } + return withContext(coroutineContext) { + behaviourContextReceiver().also { if (stopOnCompletion) stop() } + } } /** - * Creates new one [BehaviourContext], adding subsequent [FlowsUpdatesFilter] in case [updatesFilter] is provided and - * [CoroutineScope] as new [BehaviourContext.scope] + * Creates new one [BehaviourContext] using [createSubContext] and launches [behaviourContextReceiver] in a new context + * using [doInContext] + * + * @param stopOnCompletion ___TRUE BY DEFAULT___ */ +suspend fun BC.createSubContextAndDoWithUpdatesFilter( + scope: CoroutineScope = LinkedSupervisorScope(), + triggersHolder: TriggersHolder = this.triggersHolder, + updatesUpstreamFlow: Flow = allUpdatesFlow.accumulatorFlow(scope), + updatesFilter: CustomBehaviourContextAndTypeReceiver? = null, + stopOnCompletion: Boolean = true, + behaviourContextReceiver: CustomBehaviourContextReceiver +): T { + return createSubContext( + scope, + triggersHolder, + updatesUpstreamFlow, + updatesFilter + ).doInContext( + stopOnCompletion, + behaviourContextReceiver + ) +} + +/** + * Creates new one [BehaviourContext] using [createSubContext] and launches [behaviourContextReceiver] in a new context + * using [doInContext] + * + * @param stopOnCompletion ___TRUE BY DEFAULT___ + */ +@Deprecated("Renamed", ReplaceWith("createSubContextAndDoWithUpdatesFilter", "dev.inmo.tgbotapi.extensions.behaviour_builder.createSubContextAndDoWithUpdatesFilter")) suspend fun BC.doInSubContextWithUpdatesFilter( updatesFilter: CustomBehaviourContextAndTypeReceiver?, stopOnCompletion: Boolean = true, @@ -165,13 +196,21 @@ suspend fun BC.doInSubContextWithUpdatesFilter( ) } +@Deprecated("Redundant", ReplaceWith("createSubContextAndDoWithUpdatesFilter", "dev.inmo.tgbotapi.extensions.behaviour_builder.createSubContextAndDoWithUpdatesFilter")) suspend fun BehaviourContext.doInSubContext( stopOnCompletion: Boolean = true, updatesUpstreamFlow: Flow = allUpdatesFlow, scope: CoroutineScope = LinkedSupervisorScope(), triggersHolder: TriggersHolder = this.triggersHolder, behaviourContextReceiver: BehaviourContextReceiver -) = doInSubContextWithUpdatesFilter(updatesFilter = null, stopOnCompletion, updatesUpstreamFlow, scope, triggersHolder, behaviourContextReceiver) +) = createSubContextAndDoWithUpdatesFilter( + scope, + triggersHolder, + updatesUpstreamFlow, + updatesFilter = null, + stopOnCompletion, + behaviourContextReceiver +) /** * This method will cancel ALL subsequent contexts, expectations and waiters diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt index 2f181eb3fa..766972a57b 100644 --- a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/triggers_handling/MainTrigger.kt @@ -21,12 +21,9 @@ internal suspend inline fun BC.on( scope, markerFactory::invoke ) { triggerData -> - val scope = LinkedSupervisorScope() - doInSubContextWithUpdatesFilter( + createSubContextAndDoWithUpdatesFilter( updatesFilter = subcontextUpdatesFilter ?.toOneType(triggerData), - stopOnCompletion = false, - updatesUpstreamFlow = allUpdatesFlow.accumulatorFlow(scope), - scope = scope + stopOnCompletion = false ) { scenarioReceiver(triggerData) }