From 484e09374d7125ef3a4657ed16bd62ff10bb998a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 2 May 2022 08:49:58 +0600 Subject: [PATCH] small refactor --- .../behaviour_builder/BehaviourContext.kt | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) 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 ccc6ccbbd5..a5d9a0e73d 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 @@ -90,7 +90,7 @@ class DefaultBehaviourContext( onBufferOverflow: BufferOverflow, upstreamUpdatesFlow: Flow?, updatesFilter: BehaviourContextAndTypeReceiver? - ): BehaviourContext = DefaultBehaviourContext(bot, scope, broadcastChannelsSize, onBufferOverflow, upstreamUpdatesFlow, updatesFilter) + ): DefaultBehaviourContext = DefaultBehaviourContext(bot, scope, broadcastChannelsSize, onBufferOverflow, upstreamUpdatesFlow, updatesFilter) } fun BehaviourContext( @@ -116,19 +116,20 @@ suspend fun BC.doInSubContextWithUpdatesFilter( updatesUpstreamFlow: Flow = allUpdatesFlow, scope: CoroutineScope = LinkedSupervisorScope(), behaviourContextReceiver: CustomBehaviourContextReceiver -): T = copy( - scope = scope, - updatesFilter = updatesFilter ?.let { _ -> - { - (this as? BC) ?.run { - updatesFilter(it) - } ?: true - } - }, - upstreamUpdatesFlow = updatesUpstreamFlow -).run { - withContext(coroutineContext) { - (this@run as BC).behaviourContextReceiver().also { if (stopOnCompletion) stop() } +): T { + val newContext = copy( + scope = scope, + updatesFilter = updatesFilter ?.let { _ -> + { + (this as? BC) ?.run { + updatesFilter(it) + } ?: true + } + }, + upstreamUpdatesFlow = updatesUpstreamFlow + ) as BC + return withContext(currentCoroutineContext()) { + newContext.behaviourContextReceiver().also { if (stopOnCompletion) newContext.stop() } } }