1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-25 03:28:44 +00:00

buildBehaciour returns BehaviourContext

This commit is contained in:
InsanusMokrassar 2022-05-16 18:58:19 +06:00
parent f1be8bf16e
commit 9d40e598f1
2 changed files with 23 additions and 22 deletions

View File

@ -2,6 +2,9 @@
## 1.1.1
* `Behaviour Builder`:
* Extension `TelegramBot#buildBehaviour` now returns `BehaviourContext`
## 1.1.0
* `Utils`:

View File

@ -7,8 +7,7 @@ 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
import kotlinx.coroutines.*
/**
* This function is used in [buildBehaviour] extensions to provide default [CoroutineScope] and allow to avoid all
@ -30,18 +29,18 @@ suspend fun TelegramBot.buildBehaviour(
scope: CoroutineScope = defaultCoroutineScopeProvider(),
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
block: BehaviourContextReceiver<Unit>
) {
BehaviourContext(
this,
scope.let {
if (defaultExceptionsHandler == null) {
it
} else {
it + ContextSafelyExceptionHandler(defaultExceptionsHandler)
}
},
flowUpdatesFilter
).block()
): BehaviourContext = BehaviourContext(
this,
scope.let {
if (defaultExceptionsHandler == null) {
it
} else {
it + ContextSafelyExceptionHandler(defaultExceptionsHandler)
}
},
flowUpdatesFilter
).apply {
block()
}
/**
@ -56,15 +55,14 @@ suspend fun TelegramBot.buildBehaviourWithLongPolling(
scope: CoroutineScope = defaultCoroutineScopeProvider(),
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
block: BehaviourContextReceiver<Unit>
) = FlowsUpdatesFilter().let {
buildBehaviour(
it,
scope,
defaultExceptionsHandler,
block
): Job {
val behaviourContext = buildBehaviour(
scope = scope,
defaultExceptionsHandler = defaultExceptionsHandler,
block = block
)
longPolling(
it,
return longPolling(
behaviourContext,
scope = scope
)
}