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:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user