mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-12-22 14:15:45 +00:00
add micro_utils and deprecate old coroutine methods
This commit is contained in:
@@ -1,16 +1,18 @@
|
||||
package dev.inmo.tgbotapi.extensions.utils
|
||||
|
||||
import dev.inmo.micro_utils.coroutines.ExceptionHandler
|
||||
import dev.inmo.tgbotapi.utils.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
|
||||
/**
|
||||
* Shortcut for [handleSafely]. It was created for more comfortable way of handling different things
|
||||
* Shortcut for [dev.inmo.micro_utils.coroutines.safely]. It was created for more comfortable way of handling different things
|
||||
*/
|
||||
@PreviewFeature
|
||||
@Deprecated("In future will be used typealias from micro_utils", ReplaceWith("safely", "dev.inmo.micro_utils.coroutines.safely"))
|
||||
suspend inline fun <T> safely(
|
||||
noinline onException: ExceptionHandler<T> = { throw it },
|
||||
noinline block: suspend CoroutineScope.() -> T
|
||||
): T = handleSafely(
|
||||
): T = dev.inmo.micro_utils.coroutines.safely(
|
||||
onException,
|
||||
block
|
||||
)
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package dev.inmo.tgbotapi.extensions.utils.shortcuts
|
||||
|
||||
import dev.inmo.micro_utils.coroutines.safely
|
||||
import dev.inmo.tgbotapi.bot.RequestsExecutor
|
||||
import dev.inmo.tgbotapi.requests.abstracts.Request
|
||||
import dev.inmo.tgbotapi.utils.handleSafely
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
fun <T: Any> RequestsExecutor.executeAsync(
|
||||
request: Request<T>,
|
||||
scope: CoroutineScope
|
||||
): Deferred<T> = scope.async {
|
||||
handleSafely {
|
||||
safely {
|
||||
execute(request)
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ suspend fun <T: Any> RequestsExecutor.executeUnsafe(
|
||||
var leftRetries = retries
|
||||
val exceptions = onAllFailed ?.let { mutableListOf<Throwable>() }
|
||||
do {
|
||||
return handleSafely(
|
||||
return safely (
|
||||
{
|
||||
leftRetries--
|
||||
delay(retriesDelay)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package dev.inmo.tgbotapi.extensions.utils.updates.retrieving
|
||||
|
||||
import dev.inmo.micro_utils.coroutines.ExceptionHandler
|
||||
import dev.inmo.micro_utils.coroutines.safely
|
||||
import dev.inmo.tgbotapi.bot.RequestsExecutor
|
||||
import dev.inmo.tgbotapi.bot.exceptions.RequestException
|
||||
import dev.inmo.tgbotapi.extensions.utils.updates.convertWithMediaGroupUpdates
|
||||
@@ -23,7 +25,7 @@ fun RequestsExecutor.startGettingOfUpdatesByLongPolling(
|
||||
var lastUpdateIdentifier: UpdateIdentifier? = null
|
||||
|
||||
while (isActive) {
|
||||
handleSafely(
|
||||
safely(
|
||||
{ e ->
|
||||
exceptionsHandler ?.invoke(e)
|
||||
if (e is RequestException) {
|
||||
@@ -52,7 +54,7 @@ fun RequestsExecutor.startGettingOfUpdatesByLongPolling(
|
||||
}
|
||||
}
|
||||
|
||||
handleSafely {
|
||||
safely {
|
||||
for (update in updates) {
|
||||
updatesReceiver(update)
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package dev.inmo.tgbotapi.extensions.utils.updates.retrieving
|
||||
|
||||
import dev.inmo.micro_utils.coroutines.ExceptionHandler
|
||||
import dev.inmo.micro_utils.coroutines.safely
|
||||
import dev.inmo.tgbotapi.bot.RequestsExecutor
|
||||
import dev.inmo.tgbotapi.extensions.utils.nonstrictJsonFormat
|
||||
import dev.inmo.tgbotapi.extensions.utils.updates.flowsUpdatesFilter
|
||||
@@ -11,8 +13,6 @@ import dev.inmo.tgbotapi.types.update.abstracts.Update
|
||||
import dev.inmo.tgbotapi.types.update.abstracts.UpdateDeserializationStrategy
|
||||
import dev.inmo.tgbotapi.updateshandlers.*
|
||||
import dev.inmo.tgbotapi.updateshandlers.webhook.WebhookPrivateKeyConfig
|
||||
import dev.inmo.tgbotapi.utils.ExceptionHandler
|
||||
import dev.inmo.tgbotapi.utils.handleSafely
|
||||
import io.ktor.application.call
|
||||
import io.ktor.request.receiveText
|
||||
import io.ktor.response.respond
|
||||
@@ -41,7 +41,7 @@ fun Route.includeWebhookHandlingInRoute(
|
||||
) {
|
||||
val transformer = scope.updateHandlerWithMediaGroupsAdaptation(block)
|
||||
post {
|
||||
handleSafely(
|
||||
safely(
|
||||
exceptionsHandler ?: {}
|
||||
) {
|
||||
val asJson =
|
||||
|
||||
Reference in New Issue
Block a user