defaultExceptionsHandler in behaviour builder

This commit is contained in:
InsanusMokrassar 2021-06-26 17:05:34 +06:00
parent b9de002517
commit ee8cc2aa46
4 changed files with 21 additions and 3 deletions

View File

@ -10,6 +10,7 @@
* New extensions `telegramBotWithBehaviour`
* All triggers (`on*` extensions) have been modified to work in parallel by default (new parameter
`performInParallel`, by default `true`)
* All behaviour builder extensions got new parameter `defaultExceptionsHandler`
* `API`:
* All `reply` and subsequent extension have been replaced in send package
* `Bot API 5.3`:

View File

@ -1,11 +1,14 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder
import dev.inmo.micro_utils.coroutines.ContextSafelyExceptionHandler
import dev.inmo.micro_utils.coroutines.ExceptionHandler
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.longPolling
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingOfUpdatesByLongPolling
import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
import dev.inmo.tgbotapi.utils.PreviewFeature
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.plus
/**
* Use this method in case you wish to make some additional actions with [flowUpdatesFilter].
@ -20,11 +23,18 @@ import kotlinx.coroutines.CoroutineScope
suspend fun TelegramBot.buildBehaviour(
scope: CoroutineScope,
flowUpdatesFilter: FlowsUpdatesFilter,
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
block: BehaviourContextReceiver<Unit>
) {
BehaviourContext(
this,
scope,
scope.let {
if (defaultExceptionsHandler == null) {
it
} else {
it + ContextSafelyExceptionHandler(defaultExceptionsHandler)
}
},
flowUpdatesFilter
).block()
}
@ -40,11 +50,13 @@ suspend fun TelegramBot.buildBehaviour(
@PreviewFeature
suspend fun TelegramBot.buildBehaviour(
scope: CoroutineScope,
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
block: BehaviourContextReceiver<Unit>
) = FlowsUpdatesFilter().let {
buildBehaviour(
scope,
it,
defaultExceptionsHandler,
block
)
longPolling(

View File

@ -60,12 +60,12 @@ suspend fun <T> BehaviourContext.doInSubContextWithUpdatesFilter(
newFlowsUpdatesFilterSetUp = updatesFilter ?.let {
{ oldOne ->
weakLaunch {
oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(this, asUpdateReceiver)
oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(this, block = asUpdateReceiver)
}
}
} ?: { oldOne ->
weakLaunch {
oldOne.allUpdatesFlow.subscribeSafelyWithoutExceptions(this, asUpdateReceiver)
oldOne.allUpdatesFlow.subscribeSafelyWithoutExceptions(this, block = asUpdateReceiver)
}
},
stopOnCompletion,

View File

@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder
import dev.inmo.micro_utils.coroutines.ExceptionHandler
import dev.inmo.tgbotapi.bot.Ktor.KtorRequestsExecutorBuilder
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
import dev.inmo.tgbotapi.bot.TelegramBot
@ -28,6 +29,7 @@ suspend fun telegramBotWithBehaviour(
scope: CoroutineScope? = null,
apiUrl: String = telegramBotAPIDefaultUrl,
builder: KtorRequestsExecutorBuilder.() -> Unit = {},
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
block: BehaviourContextReceiver<Unit>
): TelegramBot = telegramBot(
token,
@ -37,6 +39,7 @@ suspend fun telegramBotWithBehaviour(
buildBehaviour(
scope ?: CoroutineScope(coroutineContext),
flowsUpdatesFilter,
defaultExceptionsHandler,
block
)
}
@ -60,6 +63,7 @@ suspend fun telegramBotWithBehaviour(
scope: CoroutineScope? = null,
apiUrl: String = telegramBotAPIDefaultUrl,
builder: KtorRequestsExecutorBuilder.() -> Unit = {},
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
block: BehaviourContextReceiver<Unit>
): Pair<TelegramBot, Job> {
return telegramBot(
@ -69,6 +73,7 @@ suspend fun telegramBotWithBehaviour(
).let {
it to it.buildBehaviour(
scope ?: CoroutineScope(coroutineContext),
defaultExceptionsHandler,
block
)
}