1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2026-03-03 17:32:23 +00:00

improve drafts flows

This commit is contained in:
2026-02-16 18:48:31 +06:00
parent b734757062
commit 14402f9283
4 changed files with 71 additions and 8 deletions

View File

@@ -32718,6 +32718,14 @@ public final class dev/inmo/tgbotapi/utils/DeserializeWithUnknownKt {
public static final fun deserializeWithRaw (Lkotlinx/serialization/encoding/Decoder;Lkotlinx/serialization/KSerializer;)Lkotlin/Pair;
}
public final class dev/inmo/tgbotapi/utils/DraftIdAllocator {
public fun <init> ()V
public final fun allocate-2sDQ-Rg (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun free-NZs9fbA (JLkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun getAllocated ()Ljava/util/Set;
public final fun getMutex ()Lkotlinx/coroutines/sync/Mutex;
}
public final class dev/inmo/tgbotapi/utils/EntitiesBuilder {
public fun <init> ()V
public fun <init> (Ldev/inmo/tgbotapi/types/message/textsources/TextSource;)V

View File

@@ -0,0 +1,25 @@
package dev.inmo.tgbotapi.utils
import dev.inmo.tgbotapi.types.DraftId
import kotlinx.coroutines.NonCancellable.isActive
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlin.random.Random
class DraftIdAllocator {
val allocated = mutableSetOf<DraftId>()
val mutex = Mutex()
suspend fun allocate(): DraftId = mutex.withLock {
while (isActive) {
val draftId = DraftId(Random.nextLong())
if (allocated.add(draftId)) {
return draftId
}
}
error("Unable to allocate a unique draft ID")
}
suspend fun free(draftId: DraftId) = mutex.withLock {
allocated.remove(draftId)
}
}