From a56ad7d67159f277b04a0958cf61b8dddbac4724 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 15 Aug 2024 00:36:11 +0600 Subject: [PATCH] make small improvement in send action dsl --- .../inmo/tgbotapi/extensions/api/send/SendActionDSL.kt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/SendActionDSL.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/SendActionDSL.kt index ee38522ff6..03550f4398 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/SendActionDSL.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/SendActionDSL.kt @@ -1,5 +1,7 @@ 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.safelyWithoutExceptions import dev.inmo.tgbotapi.bot.TelegramBot @@ -22,7 +24,8 @@ public suspend fun TelegramBot.withAction( contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - val botActionJob = CoroutineScope(currentCoroutineContext()).launch { + val actionScope = currentCoroutineContext().LinkedSupervisorScope(Job()) + actionScope.launch { while (isActive) { safelyWithoutExceptions { execute(actionRequest) @@ -30,8 +33,8 @@ public suspend fun TelegramBot.withAction( delay(refreshTime) } } - val result = safelyWithResult { block() } - botActionJob.cancel() + val result = runCatchingSafely { block() } + actionScope.cancel() return result.getOrThrow() }