1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 12:08:43 +00:00

include weakLaunch in behaviour builder

This commit is contained in:
InsanusMokrassar 2021-04-05 18:15:10 +06:00
parent 019b260888
commit f90ae2f918

View File

@ -1,6 +1,7 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder package dev.inmo.tgbotapi.extensions.behaviour_builder
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.coroutines.weakLaunch
import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
@ -55,23 +56,21 @@ suspend fun <T> BehaviourContext.doInSubContextWithUpdatesFilter(
updatesFilter: BehaviourContextAndTypeReceiver<Boolean, Update>?, updatesFilter: BehaviourContextAndTypeReceiver<Boolean, Update>?,
stopOnCompletion: Boolean = true, stopOnCompletion: Boolean = true,
behaviourContextReceiver: BehaviourContextReceiver<T> behaviourContextReceiver: BehaviourContextReceiver<T>
): T { ): T = doInSubContextWithFlowsUpdatesFilterSetup(
val updatesScope = CoroutineScope(coroutineContext + SupervisorJob()) newFlowsUpdatesFilterSetUp = updatesFilter ?.let {
{ oldOne ->
return doInSubContextWithFlowsUpdatesFilterSetup( weakLaunch {
newFlowsUpdatesFilterSetUp = updatesFilter ?.let { oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(this, asUpdateReceiver)
{ oldOne ->
oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(updatesScope, asUpdateReceiver)
} }
} ?: { oldOne -> }
oldOne.allUpdatesFlow.subscribeSafelyWithoutExceptions(updatesScope, asUpdateReceiver) } ?: { oldOne ->
}, weakLaunch {
stopOnCompletion oldOne.allUpdatesFlow.subscribeSafelyWithoutExceptions(this, asUpdateReceiver)
) { }
coroutineContext.job.invokeOnCompletion { updatesScope.cancel() } },
behaviourContextReceiver() stopOnCompletion,
} behaviourContextReceiver
} )
suspend fun <T> BehaviourContext.doInSubContext( suspend fun <T> BehaviourContext.doInSubContext(
stopOnCompletion: Boolean = true, stopOnCompletion: Boolean = true,