From f90ae2f9182cd6541f23d489f0f80bae0a941550 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 5 Apr 2021 18:15:10 +0600 Subject: [PATCH] include weakLaunch in behaviour builder --- .../behaviour_builder/BehaviourContext.kt | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) 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 da1ce6739b..f4d91975fb 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 @@ -1,6 +1,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions +import dev.inmo.micro_utils.coroutines.weakLaunch import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter @@ -55,23 +56,21 @@ suspend fun BehaviourContext.doInSubContextWithUpdatesFilter( updatesFilter: BehaviourContextAndTypeReceiver?, stopOnCompletion: Boolean = true, behaviourContextReceiver: BehaviourContextReceiver -): T { - val updatesScope = CoroutineScope(coroutineContext + SupervisorJob()) - - return doInSubContextWithFlowsUpdatesFilterSetup( - newFlowsUpdatesFilterSetUp = updatesFilter ?.let { - { oldOne -> - oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(updatesScope, asUpdateReceiver) +): T = doInSubContextWithFlowsUpdatesFilterSetup( + newFlowsUpdatesFilterSetUp = updatesFilter ?.let { + { oldOne -> + weakLaunch { + oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(this, asUpdateReceiver) } - } ?: { oldOne -> - oldOne.allUpdatesFlow.subscribeSafelyWithoutExceptions(updatesScope, asUpdateReceiver) - }, - stopOnCompletion - ) { - coroutineContext.job.invokeOnCompletion { updatesScope.cancel() } - behaviourContextReceiver() - } -} + } + } ?: { oldOne -> + weakLaunch { + oldOne.allUpdatesFlow.subscribeSafelyWithoutExceptions(this, asUpdateReceiver) + } + }, + stopOnCompletion, + behaviourContextReceiver +) suspend fun BehaviourContext.doInSubContext( stopOnCompletion: Boolean = true,