Fixes in TelegramBot#withAction

This commit is contained in:
InsanusMokrassar 2021-09-08 22:19:08 +06:00
parent 0fec35f0dc
commit 7af5ab17b7
3 changed files with 14 additions and 17 deletions

View File

@ -10,6 +10,8 @@
* `MultipartRequestCallFactory` now will use file name as multipart `filename` parameter instead of generated
filename
* New extension `MPPFile#asMultipartFile`
* `API`
* Fixes in `TelegramBot#withAction`
* `Behaviour Builder`:
* New extensions `BehaviourContext#commandWithArgs` and `BehaviourContext#onCommandWithArgs`

View File

@ -1,13 +1,13 @@
package dev.inmo.tgbotapi.extensions.api.send
import dev.inmo.micro_utils.coroutines.safely
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
import dev.inmo.micro_utils.coroutines.*
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.send.SendAction
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.actions.*
import dev.inmo.tgbotapi.types.chat.abstracts.Chat
import kotlinx.coroutines.*
import kotlin.coroutines.coroutineContext
private const val refreshTime: MilliSeconds = (botActionActualityTime - 1) * 1000L
typealias TelegramBotActionCallback<T> = suspend TelegramBot.() -> T
@ -16,21 +16,17 @@ suspend fun <T> TelegramBot.withAction(
actionRequest: SendAction,
block: TelegramBotActionCallback<T>
): T {
val botActionJob = supervisorScope {
launch {
while (isActive) {
delay(refreshTime)
safelyWithoutExceptions {
execute(actionRequest)
}
val botActionJob = CoroutineScope(coroutineContext).launch {
while (isActive) {
delay(refreshTime)
safelyWithoutExceptions {
execute(actionRequest)
}
}
}
return try {
safely { block() }
} finally {
botActionJob.cancel()
}
val result = safelyWithResult { block() }
botActionJob.cancel()
return result.getOrThrow()
}
suspend fun <T> TelegramBot.withAction(

View File

@ -4,6 +4,7 @@ import dev.inmo.micro_utils.coroutines.safely
import dev.inmo.tgbotapi.bot.RequestsExecutor
import dev.inmo.tgbotapi.requests.abstracts.Request
import kotlinx.coroutines.*
import kotlin.coroutines.coroutineContext
fun <T: Any> RequestsExecutor.executeAsync(
request: Request<T>,
@ -16,9 +17,7 @@ fun <T: Any> RequestsExecutor.executeAsync(
suspend fun <T: Any> RequestsExecutor.executeAsync(
request: Request<T>
): Deferred<T> = coroutineScope {
executeAsync(request, this)
}
): Deferred<T> = executeAsync(request, CoroutineScope(coroutineContext))
suspend fun <T: Any> RequestsExecutor.executeUnsafe(
request: Request<T>,