From 821bb5b45c80d4ea837355ce76af8470444f953c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 25 Sep 2023 20:23:37 +0600 Subject: [PATCH] improve cloud storage --- .../tgbotapi/webapps/cloud/CloudStorage.kt | 90 ++++++++++++++----- 1 file changed, 70 insertions(+), 20 deletions(-) diff --git a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/cloud/CloudStorage.kt b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/cloud/CloudStorage.kt index cff6d26db8..27172a8ce6 100644 --- a/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/cloud/CloudStorage.kt +++ b/tgbotapi.webapps/src/jsMain/kotlin/dev/inmo/tgbotapi/webapps/cloud/CloudStorage.kt @@ -39,47 +39,97 @@ fun CloudStorage.set( callback: (result: Result) -> Unit = {} ) = setItem(CloudStorageKey(key), CloudStorageValue(value)) { e, v -> callback(resultsToResult(e, v)) } +fun CloudStorage.get( + key: CloudStorageKey, + callback: (result: Result) -> Unit +) = getItem(key) { e, v -> callback(resultsToResult(e, v)) } + fun CloudStorage.get( key: String, callback: (result: Result) -> Unit -) = getItem(CloudStorageKey(key)) { e, v -> callback(resultsToResult(e, v)) } +) = get(CloudStorageKey(key), callback) + +fun CloudStorage.get( + keys: Array, + callback: (result: Result>) -> Unit +) = getItems( + keys +) { e, v -> callback(resultsToResult(e, v)) } + +fun CloudStorage.get( + keys: Array, + callback: (result: Result>) -> Unit +) = get( + Array(keys.size) { + CloudStorageKey(keys[it]) + }, + callback +) fun CloudStorage.get( key: String, key2: String, vararg otherKeys: String, callback: (result: Result>) -> Unit -) = getItems( - Array(2 + otherKeys.size) { - when (it) { - 0 -> CloudStorageKey(key) - 1 -> CloudStorageKey(key2) - else -> CloudStorageKey(otherKeys[it - 2]) - } - } -) { e, v -> callback(resultsToResult(e, v)) } +) = get( + arrayOf(key, key2) + otherKeys, + callback +) + +fun CloudStorage.remove( + key: CloudStorageKey, + callback: (result: Result) -> Unit +) = removeItem(key) { e, v -> callback(resultsToResult(e, v)) } fun CloudStorage.remove( key: String, callback: (result: Result) -> Unit -) = removeItem(CloudStorageKey(key)) { e, v -> callback(resultsToResult(e, v)) } +) = remove(CloudStorageKey(key), callback) + +fun CloudStorage.remove( + keys: Array, + callback: (result: Result) -> Unit +) = removeItems( + keys +) { e, v -> callback(resultsToResult(e, v)) } + +fun CloudStorage.remove( + keys: Array, + callback: (result: Result) -> Unit +) = remove( + Array(keys.size) { + CloudStorageKey(keys[it]) + }, + callback +) fun CloudStorage.remove( key: String, key2: String, vararg otherKeys: String, callback: (result: Result) -> Unit -) = removeItems( - Array(2 + otherKeys.size) { - when (it) { - 0 -> CloudStorageKey(key) - 1 -> CloudStorageKey(key2) - else -> CloudStorageKey(otherKeys[it - 2]) - } - } -) { e, v -> callback(resultsToResult(e, v)) } +) = remove( + arrayOf(key, key2) + otherKeys, + callback +) fun CloudStorage.keys( callback: (result: Result>) -> Unit ) = getKeys { e, v -> callback(resultsToResult(e, v)) } +fun CloudStorage.getAll(callback: (result: Result>) -> Unit) = keys { + it.onSuccess { keys -> + get(keys) { + it.onSuccess { values -> + val resultMap = keys.withIndex().mapNotNull { (i, it) -> + it to (values.getOrNull(i) ?: return@mapNotNull null) + }.toMap() + callback(Result.success(resultMap)) + }.onFailure { + callback(Result.failure(it)) + } + } + }.onFailure { + callback(Result.failure(it)) + } +}