diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e0252ca34..6806ed75fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourBuilders.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourBuilders.kt index e4473c5431..6ec5c5a862 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourBuilders.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourBuilders.kt @@ -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? = null, + block: BehaviourContextReceiver +) { + 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? = null, block: BehaviourContextReceiver ) { @@ -49,7 +85,7 @@ suspend fun TelegramBot.buildBehaviour( */ @PreviewFeature suspend fun TelegramBot.buildBehaviour( - scope: CoroutineScope, + scope: CoroutineScope = defaultCoroutineScopeProvider(), defaultExceptionsHandler: ExceptionHandler? = null, block: BehaviourContextReceiver ) = FlowsUpdatesFilter().let { diff --git a/tgbotapi.extensions.behaviour_builder/src/jsMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/DefaultCoroutineScopeProvider.kt b/tgbotapi.extensions.behaviour_builder/src/jsMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/DefaultCoroutineScopeProvider.kt new file mode 100644 index 0000000000..93b2831568 --- /dev/null +++ b/tgbotapi.extensions.behaviour_builder/src/jsMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/DefaultCoroutineScopeProvider.kt @@ -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) +} diff --git a/tgbotapi.extensions.behaviour_builder/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/DefaultCoroutineScopeProvider.kt b/tgbotapi.extensions.behaviour_builder/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/DefaultCoroutineScopeProvider.kt new file mode 100644 index 0000000000..3106aef3ca --- /dev/null +++ b/tgbotapi.extensions.behaviour_builder/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/DefaultCoroutineScopeProvider.kt @@ -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) +}