1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-24 19:18:44 +00:00

improvements and fixes in web apps api

This commit is contained in:
InsanusMokrassar 2023-09-25 23:21:48 +06:00
parent 821bb5b45c
commit efe286c181
2 changed files with 13 additions and 9 deletions

View File

@ -9,7 +9,7 @@ sealed interface Color {
@Serializable
value class Hex(override val value: String) : Color {
constructor(r: UByte, g: UByte, b: UByte) : this("#${r.toString(16)}${g.toString(16)}${b.toString(16)}")
constructor(r: UByte, g: UByte, b: UByte) : this("#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}")
}
companion object {

View File

@ -1,34 +1,36 @@
package dev.inmo.tgbotapi.webapps.cloud
import kotlin.js.Json
external interface CloudStorage {
fun setItem(
key: CloudStorageKey,
value: CloudStorageValue,
callback: (e: Error?, success: Boolean?) -> Unit = definedExternally
callback: (e: Any?, success: Boolean?) -> Unit = definedExternally
): CloudStorage
fun getItem(
key: CloudStorageKey,
callback: (e: Error?, value: CloudStorageValue?) -> Unit
callback: (e: Any?, value: CloudStorageValue?) -> Unit
): CloudStorage
fun getItems(
key: Array<CloudStorageKey>,
callback: (e: Error?, values: Array<CloudStorageValue>?) -> Unit
callback: (e: Any?, values: Array<CloudStorageValue>?) -> Unit
): CloudStorage
fun removeItem(
key: CloudStorageKey,
callback: (e: Error?, success: Boolean?) -> Unit
callback: (e: Any?, success: Boolean?) -> Unit
): CloudStorage
fun removeItems(
key: Array<CloudStorageKey>,
callback: (e: Error?, success: Boolean?) -> Unit
callback: (e: Any?, success: Boolean?) -> Unit
): CloudStorage
fun getKeys(
callback: (e: Error?, success: Array<CloudStorageKey>?) -> Unit
callback: (e: Any?, success: Array<CloudStorageKey>?) -> Unit
): CloudStorage
}
private fun <T> resultsToResult(e: Error?, v: T?): Result<T> = when {
e != null -> Result.failure(e)
private fun <T> resultsToResult(e: Any?, v: T?): Result<T> = when {
e != null -> Result.failure(IllegalStateException(JSON.stringify(e)))
v != null -> Result.success(v)
else -> Result.failure(IllegalStateException("Both value and e"))
}
@ -119,8 +121,10 @@ fun CloudStorage.keys(
fun CloudStorage.getAll(callback: (result: Result<Map<CloudStorageKey, CloudStorageValue>>) -> Unit) = keys {
it.onSuccess { keys ->
console.log(keys)
get(keys) {
it.onSuccess { values ->
console.log(values)
val resultMap = keys.withIndex().mapNotNull { (i, it) ->
it to (values.getOrNull(i) ?: return@mapNotNull null)
}.toMap()