improvements in buildBehaviour

This commit is contained in:
InsanusMokrassar 2021-08-03 11:27:39 +06:00
parent e7a2dc51aa
commit adf5fd6288
4 changed files with 60 additions and 2 deletions

View File

@ -7,6 +7,12 @@
* `Klock`: `2.2.0` -> `2.3.1`
* `Ktor`: `1.6.1` -> `1.6.2`
* `MicroUtils`: `0.5.16` -> `0.5.17`
* `Behaviour Builder`:
* New provider `defaultCoroutineScopeProvider`
* Now it is not necessary to provide `CoroutineScope` to `TelegramBot#buildBehaviour`
extension
* New `TelegramBot#buildBehaviour` extension with `FlowUpdatesFilter` and `CoroutineScope` with
default `CoroutineScope`
## 0.35.2

View File

@ -10,6 +10,42 @@ import dev.inmo.tgbotapi.utils.PreviewFeature
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.plus
/**
* This function is used in [buildBehaviour] extensions to provide default [CoroutineScope] and allow to avoid all
* unnecessary parameters except of block
*/
expect var defaultCoroutineScopeProvider: () -> CoroutineScope
/**
* Use this method in case you wish to make some additional actions with [flowUpdatesFilter].
*
* **WARNING** This method WILL NOT launch any listening of updates. Use something like
* [startGettingOfUpdatesByLongPolling] or tools for work with webhooks
*
* @see [BehaviourContext]
* @see startGettingOfUpdatesByLongPolling
*/
@PreviewFeature
@Deprecated("Parameters has been reordered. Replace scope and flowUpdatesFilter for correct order")
suspend fun TelegramBot.buildBehaviour(
scope: CoroutineScope,
flowUpdatesFilter: FlowsUpdatesFilter,
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
block: BehaviourContextReceiver<Unit>
) {
BehaviourContext(
this,
scope.let {
if (defaultExceptionsHandler == null) {
it
} else {
it + ContextSafelyExceptionHandler(defaultExceptionsHandler)
}
},
flowUpdatesFilter
).block()
}
/**
* Use this method in case you wish to make some additional actions with [flowUpdatesFilter].
*
@ -21,8 +57,8 @@ import kotlinx.coroutines.plus
*/
@PreviewFeature
suspend fun TelegramBot.buildBehaviour(
scope: CoroutineScope,
flowUpdatesFilter: FlowsUpdatesFilter,
scope: CoroutineScope = defaultCoroutineScopeProvider(),
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
block: BehaviourContextReceiver<Unit>
) {
@ -49,7 +85,7 @@ suspend fun TelegramBot.buildBehaviour(
*/
@PreviewFeature
suspend fun TelegramBot.buildBehaviour(
scope: CoroutineScope,
scope: CoroutineScope = defaultCoroutineScopeProvider(),
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,
block: BehaviourContextReceiver<Unit>
) = FlowsUpdatesFilter().let {

View File

@ -0,0 +1,8 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
actual var defaultCoroutineScopeProvider = {
CoroutineScope(Dispatchers.Default)
}

View File

@ -0,0 +1,8 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
actual var defaultCoroutineScopeProvider = {
CoroutineScope(Dispatchers.IO)
}