From db14eee3b6c7d2b5d01a75b8aaecc9bdafaa0d8e Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 2 May 2022 01:16:24 +0300 Subject: [PATCH 1/2] fix: new BehaviourContext ignored --- .../tgbotapi/extensions/behaviour_builder/BehaviourContext.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 2426853a51..ccc6ccbbd5 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 @@ -128,7 +128,7 @@ suspend fun BC.doInSubContextWithUpdatesFilter( upstreamUpdatesFlow = updatesUpstreamFlow ).run { withContext(coroutineContext) { - behaviourContextReceiver().also { if (stopOnCompletion) stop() } + (this@run as BC).behaviourContextReceiver().also { if (stopOnCompletion) stop() } } } From 2ae105cb15381be72c06c948c11a465d08ca87c6 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 2 May 2022 08:49:58 +0600 Subject: [PATCH 2/2] 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() } } }