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

make small improvement in send action dsl

This commit is contained in:
InsanusMokrassar 2024-08-15 00:36:11 +06:00
parent 77158ffecf
commit a56ad7d671

View File

@ -1,5 +1,7 @@
package dev.inmo.tgbotapi.extensions.api.send package dev.inmo.tgbotapi.extensions.api.send
import dev.inmo.micro_utils.coroutines.LinkedSupervisorScope
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.micro_utils.coroutines.safelyWithResult import dev.inmo.micro_utils.coroutines.safelyWithResult
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.bot.TelegramBot
@ -22,7 +24,8 @@ public suspend fun <T> TelegramBot.withAction(
contract { contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE) callsInPlace(block, InvocationKind.EXACTLY_ONCE)
} }
val botActionJob = CoroutineScope(currentCoroutineContext()).launch { val actionScope = currentCoroutineContext().LinkedSupervisorScope(Job())
actionScope.launch {
while (isActive) { while (isActive) {
safelyWithoutExceptions { safelyWithoutExceptions {
execute(actionRequest) execute(actionRequest)
@ -30,8 +33,8 @@ public suspend fun <T> TelegramBot.withAction(
delay(refreshTime) delay(refreshTime)
} }
} }
val result = safelyWithResult { block() } val result = runCatchingSafely { block() }
botActionJob.cancel() actionScope.cancel()
return result.getOrThrow() return result.getOrThrow()
} }