1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 03:58:44 +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
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 <T> BehaviourContext.doInSubContextWithUpdatesFilter(
updatesFilter: BehaviourContextAndTypeReceiver<Boolean, Update>?,
stopOnCompletion: Boolean = true,
behaviourContextReceiver: BehaviourContextReceiver<T>
): T {
val updatesScope = CoroutineScope(coroutineContext + SupervisorJob())
return doInSubContextWithFlowsUpdatesFilterSetup(
): T = doInSubContextWithFlowsUpdatesFilterSetup(
newFlowsUpdatesFilterSetUp = updatesFilter ?.let {
{ oldOne ->
oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(updatesScope, asUpdateReceiver)
weakLaunch {
oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(this, asUpdateReceiver)
}
}
} ?: { oldOne ->
oldOne.allUpdatesFlow.subscribeSafelyWithoutExceptions(updatesScope, asUpdateReceiver)
},
stopOnCompletion
) {
coroutineContext.job.invokeOnCompletion { updatesScope.cancel() }
behaviourContextReceiver()
weakLaunch {
oldOne.allUpdatesFlow.subscribeSafelyWithoutExceptions(this, asUpdateReceiver)
}
}
},
stopOnCompletion,
behaviourContextReceiver
)
suspend fun <T> BehaviourContext.doInSubContext(
stopOnCompletion: Boolean = true,