mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-23 09:07:14 +00:00
commit
551d8ec480
21
CHANGELOG.md
21
CHANGELOG.md
@ -1,5 +1,26 @@
|
||||
# Changelog
|
||||
|
||||
## 0.16.7
|
||||
|
||||
* `Common`:
|
||||
* New extensions `ifTrue`/`ifFalse`/`alsoIfTrue`/`alsoIfFalse`/`letIfTrue`/`letIfFalse`
|
||||
* `Diff` now is serializable
|
||||
* Add `IndexedValue` serializer
|
||||
* `repeatOnFailure` extending: now you may pass any lambda to check if continue to try/do something
|
||||
* `Compose`:
|
||||
* New extension `MutableState.asState`
|
||||
* `Coroutines`:
|
||||
* `Compose`:
|
||||
* All the `Flow` conversations to compose `State`/`MutableState`/`SnapshotStateList`/`List` got several new
|
||||
parameters
|
||||
* `Flow.toMutableState` now is deprecated in favor to `asMutableComposeState`
|
||||
* `Repos`:
|
||||
* `Cache`:
|
||||
* New type `FullCacheRepo`
|
||||
* New type `CommonCacheRepo`
|
||||
* `CacheRepo` got `invalidate` method. It will fully reload `FullCacheRepo` and just clear `CommonCacheRepo`
|
||||
* New extensions `KVCache.actualizeAll`
|
||||
|
||||
## 0.16.6
|
||||
|
||||
* `Startup`:
|
||||
|
@ -0,0 +1,10 @@
|
||||
package dev.inmo.micro_utils.common.compose
|
||||
|
||||
import androidx.compose.runtime.MutableState
|
||||
import androidx.compose.runtime.State
|
||||
import androidx.compose.runtime.derivedStateOf
|
||||
|
||||
/**
|
||||
* Converts current [MutableState] to immutable [State] using [derivedStateOf]
|
||||
*/
|
||||
fun <T> MutableState<T>.asState(): State<T> = derivedStateOf { this.value }
|
@ -2,6 +2,8 @@
|
||||
|
||||
package dev.inmo.micro_utils.common
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
private inline fun <T> getObject(
|
||||
additional: MutableList<T>,
|
||||
iterator: Iterator<T>
|
||||
@ -24,13 +26,14 @@ private inline fun <T> getObject(
|
||||
*
|
||||
* @see calculateDiff
|
||||
*/
|
||||
@Serializable
|
||||
data class Diff<T> internal constructor(
|
||||
val removed: List<IndexedValue<T>>,
|
||||
val removed: List<@Serializable(IndexedValueSerializer::class) IndexedValue<T>>,
|
||||
/**
|
||||
* Old-New values pairs
|
||||
*/
|
||||
val replaced: List<Pair<IndexedValue<T>, IndexedValue<T>>>,
|
||||
val added: List<IndexedValue<T>>
|
||||
val replaced: List<Pair<@Serializable(IndexedValueSerializer::class) IndexedValue<T>, @Serializable(IndexedValueSerializer::class) IndexedValue<T>>>,
|
||||
val added: List<@Serializable(IndexedValueSerializer::class) IndexedValue<T>>
|
||||
)
|
||||
|
||||
private inline fun <T> performChanges(
|
||||
|
@ -0,0 +1,43 @@
|
||||
package dev.inmo.micro_utils.common
|
||||
|
||||
inline fun <T> Boolean.letIfTrue(block: () -> T): T? {
|
||||
return if (this) {
|
||||
block()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T> Boolean.letIfFalse(block: () -> T): T? {
|
||||
return if (this) {
|
||||
null
|
||||
} else {
|
||||
block()
|
||||
}
|
||||
}
|
||||
|
||||
inline fun Boolean.alsoIfTrue(block: () -> Unit): Boolean {
|
||||
letIfTrue(block)
|
||||
return this
|
||||
}
|
||||
|
||||
inline fun Boolean.alsoIfFalse(block: () -> Unit): Boolean {
|
||||
letIfFalse(block)
|
||||
return this
|
||||
}
|
||||
|
||||
inline fun <T> Boolean.ifTrue(block: () -> T): T? {
|
||||
return if (this) {
|
||||
block()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T> Boolean.ifFalse(block: () -> T): T? {
|
||||
return if (this) {
|
||||
null
|
||||
} else {
|
||||
block()
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package dev.inmo.micro_utils.common
|
||||
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.Serializer
|
||||
import kotlinx.serialization.builtins.PairSerializer
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
import kotlinx.serialization.descriptors.SerialDescriptor
|
||||
import kotlinx.serialization.encoding.Decoder
|
||||
import kotlinx.serialization.encoding.Encoder
|
||||
|
||||
class IndexedValueSerializer<T>(private val subSerializer: KSerializer<T>) : KSerializer<IndexedValue<T>> {
|
||||
private val originalSerializer = PairSerializer(Int.serializer(), subSerializer)
|
||||
override val descriptor: SerialDescriptor
|
||||
get() = originalSerializer.descriptor
|
||||
|
||||
override fun deserialize(decoder: Decoder): IndexedValue<T> {
|
||||
val pair = originalSerializer.deserialize(decoder)
|
||||
return IndexedValue(
|
||||
pair.first,
|
||||
pair.second
|
||||
)
|
||||
}
|
||||
|
||||
override fun serialize(encoder: Encoder, value: IndexedValue<T>) {
|
||||
originalSerializer.serialize(
|
||||
encoder,
|
||||
Pair(value.index, value.value)
|
||||
)
|
||||
}
|
||||
}
|
@ -1,5 +1,27 @@
|
||||
package dev.inmo.micro_utils.common
|
||||
|
||||
/**
|
||||
* Executes the given [action] until getting of successful result specified number of [times].
|
||||
*
|
||||
* A zero-based index of current iteration is passed as a parameter to [action].
|
||||
*/
|
||||
inline fun <R> repeatOnFailure(
|
||||
onFailure: (Throwable) -> Boolean,
|
||||
action: () -> R
|
||||
): Result<R> {
|
||||
do {
|
||||
runCatching {
|
||||
action()
|
||||
}.onFailure {
|
||||
if (!onFailure(it)) {
|
||||
return Result.failure(it)
|
||||
}
|
||||
}.onSuccess {
|
||||
return Result.success(it)
|
||||
}
|
||||
} while (true)
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the given [action] until getting of successful result specified number of [times].
|
||||
*
|
||||
@ -10,12 +32,23 @@ inline fun <R> repeatOnFailure(
|
||||
onEachFailure: (Throwable) -> Unit = {},
|
||||
action: (Int) -> R
|
||||
): Optional<R> {
|
||||
repeat(times) {
|
||||
runCatching {
|
||||
action(it)
|
||||
}.onFailure(onEachFailure).onSuccess {
|
||||
return Optional.presented(it)
|
||||
var i = 0
|
||||
val result = repeatOnFailure(
|
||||
{
|
||||
onEachFailure(it)
|
||||
if (i < times) {
|
||||
i++
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
) {
|
||||
action(i)
|
||||
}
|
||||
return if (result.isSuccess) {
|
||||
Optional.presented(result.getOrThrow())
|
||||
} else {
|
||||
Optional.absent()
|
||||
}
|
||||
return Optional.absent()
|
||||
}
|
||||
|
@ -3,24 +3,58 @@ package dev.inmo.micro_utils.coroutines.compose
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.runtime.snapshots.SnapshotStateList
|
||||
import dev.inmo.micro_utils.common.applyDiff
|
||||
import dev.inmo.micro_utils.coroutines.ExceptionHandler
|
||||
import dev.inmo.micro_utils.coroutines.defaultSafelyWithoutExceptionHandlerWithNull
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
/**
|
||||
* Each value of [this] [Flow] will trigger [applyDiff] to the result [SnapshotStateList]
|
||||
*
|
||||
* @param scope Will be used to [subscribeSafelyWithoutExceptions] on [this] to update returned [SnapshotStateList]
|
||||
* @param useContextOnChange Will be used to change context inside of [subscribeSafelyWithoutExceptions] to ensure that
|
||||
* change will happen in the required [CoroutineContext]. [Dispatchers.Main] by default
|
||||
* @param onException Will be passed to the [subscribeSafelyWithoutExceptions] as uncaught exceptions handler
|
||||
*/
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun <reified T> Flow<List<T>>.asMutableComposeListState(
|
||||
scope: CoroutineScope
|
||||
scope: CoroutineScope,
|
||||
useContextOnChange: CoroutineContext? = Dispatchers.Main,
|
||||
noinline onException: ExceptionHandler<List<T>?> = defaultSafelyWithoutExceptionHandlerWithNull,
|
||||
): SnapshotStateList<T> {
|
||||
val state = mutableStateListOf<T>()
|
||||
subscribeSafelyWithoutExceptions(scope) {
|
||||
val changeBlock: suspend (List<T>) -> Unit = useContextOnChange ?.let {
|
||||
{
|
||||
withContext(useContextOnChange) {
|
||||
state.applyDiff(it)
|
||||
}
|
||||
}
|
||||
} ?: {
|
||||
state.applyDiff(it)
|
||||
}
|
||||
subscribeSafelyWithoutExceptions(scope, onException, changeBlock)
|
||||
return state
|
||||
}
|
||||
|
||||
/**
|
||||
* In fact, it is just classcast of [asMutableComposeListState] to [List]
|
||||
*
|
||||
* @param scope Will be used to [subscribeSafelyWithoutExceptions] on [this] to update returned [List]
|
||||
* @param useContextOnChange Will be used to change context inside of [subscribeSafelyWithoutExceptions] to ensure that
|
||||
* change will happen in the required [CoroutineContext]. [Dispatchers.Main] by default
|
||||
* @param onException Will be passed to the [subscribeSafelyWithoutExceptions] as uncaught exceptions handler
|
||||
*
|
||||
* @return Changing in time [List] which follow [Flow] values
|
||||
*/
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun <reified T> Flow<List<T>>.asComposeList(
|
||||
scope: CoroutineScope
|
||||
): List<T> = asMutableComposeListState(scope)
|
||||
scope: CoroutineScope,
|
||||
useContextOnChange: CoroutineContext? = Dispatchers.Main,
|
||||
noinline onException: ExceptionHandler<List<T>?> = defaultSafelyWithoutExceptionHandlerWithNull,
|
||||
): List<T> = asMutableComposeListState(scope, useContextOnChange, onException)
|
||||
|
||||
|
@ -1,35 +1,94 @@
|
||||
package dev.inmo.micro_utils.coroutines.compose
|
||||
|
||||
import androidx.compose.runtime.*
|
||||
import dev.inmo.micro_utils.common.compose.asState
|
||||
import dev.inmo.micro_utils.coroutines.ExceptionHandler
|
||||
import dev.inmo.micro_utils.coroutines.defaultSafelyWithoutExceptionHandlerWithNull
|
||||
import dev.inmo.micro_utils.coroutines.doInUI
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
/**
|
||||
* Will map [this] [Flow] as [MutableState]. Returned [MutableState] WILL NOT change source [Flow]
|
||||
*
|
||||
* @param initial First value which will be passed to the result [MutableState]
|
||||
* @param scope Will be used to [subscribeSafelyWithoutExceptions] on [this] to update returned [MutableState]
|
||||
* @param useContextOnChange Will be used to change context inside of [subscribeSafelyWithoutExceptions] to ensure that
|
||||
* change will happen in the required [CoroutineContext]. [Dispatchers.Main] by default
|
||||
* @param onException Will be passed to the [subscribeSafelyWithoutExceptions] as uncaught exceptions handler
|
||||
*/
|
||||
fun <T> Flow<T>.asMutableComposeState(
|
||||
initial: T,
|
||||
scope: CoroutineScope
|
||||
scope: CoroutineScope,
|
||||
useContextOnChange: CoroutineContext? = Dispatchers.Main,
|
||||
onException: ExceptionHandler<T?> = defaultSafelyWithoutExceptionHandlerWithNull,
|
||||
): MutableState<T> {
|
||||
val state = mutableStateOf(initial)
|
||||
subscribeSafelyWithoutExceptions(scope) { state.value = it }
|
||||
val changeBlock: suspend (T) -> Unit = useContextOnChange ?.let {
|
||||
{
|
||||
withContext(useContextOnChange) {
|
||||
state.value = it
|
||||
}
|
||||
}
|
||||
} ?: {
|
||||
state.value = it
|
||||
}
|
||||
subscribeSafelyWithoutExceptions(scope, onException, block = changeBlock)
|
||||
return state
|
||||
}
|
||||
|
||||
/**
|
||||
* Will map [this] [StateFlow] as [MutableState]. Returned [MutableState] WILL NOT change source [StateFlow].
|
||||
* This conversation will pass its [StateFlow.value] as the first value
|
||||
*
|
||||
* @param scope Will be used to [subscribeSafelyWithoutExceptions] on [this] to update returned [MutableState]
|
||||
* @param useContextOnChange Will be used to change context inside of [subscribeSafelyWithoutExceptions] to ensure that
|
||||
* change will happen in the required [CoroutineContext]. [Dispatchers.Main] by default
|
||||
* @param onException Will be passed to the [subscribeSafelyWithoutExceptions] as uncaught exceptions handler
|
||||
*/
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun <T> StateFlow<T>.asMutableComposeState(
|
||||
scope: CoroutineScope
|
||||
): MutableState<T> = asMutableComposeState(value, scope)
|
||||
scope: CoroutineScope,
|
||||
useContextOnChange: CoroutineContext? = Dispatchers.Main,
|
||||
noinline onException: ExceptionHandler<T?> = defaultSafelyWithoutExceptionHandlerWithNull,
|
||||
): MutableState<T> = asMutableComposeState(value, scope, useContextOnChange, onException)
|
||||
|
||||
/**
|
||||
* Will create [MutableState] using [asMutableComposeState] and use [asState] to convert it as immutable state
|
||||
*
|
||||
* @param initial First value which will be passed to the result [State]
|
||||
* @param scope Will be used to [subscribeSafelyWithoutExceptions] on [this] to update returned [State]
|
||||
* @param useContextOnChange Will be used to change context inside of [subscribeSafelyWithoutExceptions] to ensure that
|
||||
* change will happen in the required [CoroutineContext]. [Dispatchers.Main] by default
|
||||
* @param onException Will be passed to the [subscribeSafelyWithoutExceptions] as uncaught exceptions handler
|
||||
*/
|
||||
fun <T> Flow<T>.asComposeState(
|
||||
initial: T,
|
||||
scope: CoroutineScope
|
||||
scope: CoroutineScope,
|
||||
useContextOnChange: CoroutineContext? = Dispatchers.Main,
|
||||
onException: ExceptionHandler<T?> = defaultSafelyWithoutExceptionHandlerWithNull,
|
||||
): State<T> {
|
||||
val state = asMutableComposeState(initial, scope)
|
||||
return derivedStateOf { state.value }
|
||||
val state = asMutableComposeState(initial, scope, useContextOnChange, onException)
|
||||
return state.asState()
|
||||
}
|
||||
|
||||
/**
|
||||
* Will map [this] [StateFlow] as [State]. This conversation will pass its [StateFlow.value] as the first value
|
||||
*
|
||||
* @param scope Will be used to [subscribeSafelyWithoutExceptions] on [this] to update returned [State]
|
||||
* @param useContextOnChange Will be used to change context inside of [subscribeSafelyWithoutExceptions] to ensure that
|
||||
* change will happen in the required [CoroutineContext]. [Dispatchers.Main] by default
|
||||
* @param onException Will be passed to the [subscribeSafelyWithoutExceptions] as uncaught exceptions handler
|
||||
*/
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun <T> StateFlow<T>.asComposeState(
|
||||
scope: CoroutineScope
|
||||
): State<T> = asComposeState(value, scope)
|
||||
scope: CoroutineScope,
|
||||
useContextOnChange: CoroutineContext? = Dispatchers.Main,
|
||||
noinline onException: ExceptionHandler<T?> = defaultSafelyWithoutExceptionHandlerWithNull,
|
||||
): State<T> = asComposeState(value, scope, useContextOnChange, onException)
|
||||
|
||||
|
@ -7,17 +7,15 @@ import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
|
||||
@Deprecated("Duplicated functionality", ReplaceWith("asMutableComposeState(initial, scope)", "dev.inmo.micro_utils.coroutines.compose.asMutableComposeState"))
|
||||
fun <T> Flow<T>.toMutableState(
|
||||
initial: T,
|
||||
scope: CoroutineScope
|
||||
): MutableState<T> {
|
||||
val state = mutableStateOf(initial)
|
||||
subscribeSafelyWithoutExceptions(scope) { state.value = it }
|
||||
return state
|
||||
}
|
||||
): MutableState<T> = asMutableComposeState(initial, scope)
|
||||
|
||||
@Deprecated("Duplicated functionality", ReplaceWith("asMutableComposeState(scope)", "dev.inmo.micro_utils.coroutines.compose.asMutableComposeState"))
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
inline fun <T> StateFlow<T>.toMutableState(
|
||||
scope: CoroutineScope
|
||||
): MutableState<T> = toMutableState(value, scope)
|
||||
): MutableState<T> = asMutableComposeState(scope)
|
||||
|
||||
|
@ -14,5 +14,5 @@ crypto_js_version=4.1.1
|
||||
# Project data
|
||||
|
||||
group=dev.inmo
|
||||
version=0.16.6
|
||||
android_code_version=174
|
||||
version=0.16.7
|
||||
android_code_version=175
|
||||
|
@ -10,12 +10,14 @@ open class ReadCRUDCacheRepo<ObjectType, IdType>(
|
||||
protected open val parentRepo: ReadCRUDRepo<ObjectType, IdType>,
|
||||
protected open val kvCache: KVCache<IdType, ObjectType>,
|
||||
protected open val idGetter: (ObjectType) -> IdType
|
||||
) : ReadCRUDRepo<ObjectType, IdType> by parentRepo, CacheRepo {
|
||||
) : ReadCRUDRepo<ObjectType, IdType> by parentRepo, CommonCacheRepo {
|
||||
override suspend fun getById(id: IdType): ObjectType? = kvCache.get(id) ?: (parentRepo.getById(id) ?.also {
|
||||
kvCache.set(id, it)
|
||||
})
|
||||
|
||||
override suspend fun contains(id: IdType): Boolean = kvCache.contains(id) || parentRepo.contains(id)
|
||||
|
||||
override suspend fun invalidate() = kvCache.clear()
|
||||
}
|
||||
|
||||
fun <ObjectType, IdType> ReadCRUDRepo<ObjectType, IdType>.cached(
|
||||
@ -28,7 +30,7 @@ open class WriteCRUDCacheRepo<ObjectType, IdType, InputValueType>(
|
||||
protected open val kvCache: KVCache<IdType, ObjectType>,
|
||||
protected open val scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
|
||||
protected open val idGetter: (ObjectType) -> IdType
|
||||
) : WriteCRUDRepo<ObjectType, IdType, InputValueType>, CacheRepo {
|
||||
) : WriteCRUDRepo<ObjectType, IdType, InputValueType>, CommonCacheRepo {
|
||||
override val newObjectsFlow: Flow<ObjectType> by parentRepo::newObjectsFlow
|
||||
override val updatedObjectsFlow: Flow<ObjectType> by parentRepo::updatedObjectsFlow
|
||||
override val deletedObjectsIdsFlow: Flow<IdType> by parentRepo::deletedObjectsIdsFlow
|
||||
@ -72,6 +74,8 @@ open class WriteCRUDCacheRepo<ObjectType, IdType, InputValueType>(
|
||||
|
||||
return created
|
||||
}
|
||||
|
||||
override suspend fun invalidate() = kvCache.clear()
|
||||
}
|
||||
|
||||
fun <ObjectType, IdType, InputType> WriteCRUDRepo<ObjectType, IdType, InputType>.caching(
|
||||
|
@ -1,3 +1,5 @@
|
||||
package dev.inmo.micro_utils.repos.cache
|
||||
|
||||
interface CacheRepo
|
||||
interface CacheRepo {
|
||||
suspend fun invalidate()
|
||||
}
|
||||
|
7
repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CommonCacheRepo.kt
vendored
Normal file
7
repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CommonCacheRepo.kt
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
package dev.inmo.micro_utils.repos.cache
|
||||
|
||||
/**
|
||||
* Any inheritor of this should work with next logic: try to take data from some [dev.inmo.micro_utils.repos.cache.cache.KVCache] and,
|
||||
* if not exists, take from origin and save to the cache for future reuse
|
||||
*/
|
||||
interface CommonCacheRepo : CacheRepo
|
@ -10,7 +10,7 @@ import kotlinx.coroutines.flow.*
|
||||
open class ReadKeyValueCacheRepo<Key,Value>(
|
||||
protected open val parentRepo: ReadKeyValueRepo<Key, Value>,
|
||||
protected open val kvCache: KVCache<Key, Value>,
|
||||
) : ReadKeyValueRepo<Key,Value> by parentRepo, CacheRepo {
|
||||
) : ReadKeyValueRepo<Key,Value> by parentRepo, CommonCacheRepo {
|
||||
override suspend fun get(k: Key): Value? = kvCache.get(k) ?: parentRepo.get(k) ?.also { kvCache.set(k, it) }
|
||||
override suspend fun contains(key: Key): Boolean = kvCache.contains(key) || parentRepo.contains(key)
|
||||
|
||||
@ -23,6 +23,8 @@ open class ReadKeyValueCacheRepo<Key,Value>(
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun invalidate() = kvCache.clear()
|
||||
}
|
||||
|
||||
fun <Key, Value> ReadKeyValueRepo<Key, Value>.cached(
|
||||
@ -33,9 +35,11 @@ open class KeyValueCacheRepo<Key,Value>(
|
||||
parentRepo: KeyValueRepo<Key, Value>,
|
||||
kvCache: KVCache<Key, Value>,
|
||||
scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
||||
) : ReadKeyValueCacheRepo<Key,Value>(parentRepo, kvCache), KeyValueRepo<Key,Value>, WriteKeyValueRepo<Key, Value> by parentRepo, CacheRepo {
|
||||
) : ReadKeyValueCacheRepo<Key,Value>(parentRepo, kvCache), KeyValueRepo<Key,Value>, WriteKeyValueRepo<Key, Value> by parentRepo, CommonCacheRepo {
|
||||
protected val onNewJob = parentRepo.onNewValue.onEach { kvCache.set(it.first, it.second) }.launchIn(scope)
|
||||
protected val onRemoveJob = parentRepo.onValueRemoved.onEach { kvCache.unset(it) }.launchIn(scope)
|
||||
|
||||
override suspend fun invalidate() = kvCache.clear()
|
||||
}
|
||||
|
||||
fun <Key, Value> KeyValueRepo<Key, Value>.cached(
|
||||
|
@ -11,7 +11,7 @@ import kotlinx.coroutines.flow.*
|
||||
open class ReadKeyValuesCacheRepo<Key,Value>(
|
||||
protected open val parentRepo: ReadKeyValuesRepo<Key, Value>,
|
||||
protected open val kvCache: KVCache<Key, List<Value>>
|
||||
) : ReadKeyValuesRepo<Key,Value> by parentRepo, CacheRepo {
|
||||
) : ReadKeyValuesRepo<Key,Value> by parentRepo, CommonCacheRepo {
|
||||
override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult<Value> {
|
||||
return getAll(k, reversed).paginate(
|
||||
pagination
|
||||
@ -30,6 +30,8 @@ open class ReadKeyValuesCacheRepo<Key,Value>(
|
||||
}
|
||||
})
|
||||
override suspend fun contains(k: Key): Boolean = kvCache.contains(k) || parentRepo.contains(k)
|
||||
|
||||
override suspend fun invalidate() = kvCache.clear()
|
||||
}
|
||||
|
||||
fun <Key, Value> ReadKeyValuesRepo<Key, Value>.cached(
|
||||
@ -40,7 +42,7 @@ open class KeyValuesCacheRepo<Key,Value>(
|
||||
parentRepo: KeyValuesRepo<Key, Value>,
|
||||
kvCache: KVCache<Key, List<Value>>,
|
||||
scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
||||
) : ReadKeyValuesCacheRepo<Key,Value>(parentRepo, kvCache), KeyValuesRepo<Key,Value>, WriteKeyValuesRepo<Key,Value> by parentRepo, CacheRepo {
|
||||
) : ReadKeyValuesCacheRepo<Key,Value>(parentRepo, kvCache), KeyValuesRepo<Key,Value>, WriteKeyValuesRepo<Key,Value> by parentRepo, CommonCacheRepo {
|
||||
protected val onNewJob = parentRepo.onNewValue.onEach { (k, v) ->
|
||||
kvCache.set(
|
||||
k,
|
||||
@ -56,6 +58,8 @@ open class KeyValuesCacheRepo<Key,Value>(
|
||||
protected val onDataClearedJob = parentRepo.onDataCleared.onEach {
|
||||
kvCache.unset(it)
|
||||
}.launchIn(scope)
|
||||
|
||||
override suspend fun invalidate() = kvCache.clear()
|
||||
}
|
||||
|
||||
fun <Key, Value> KeyValuesRepo<Key, Value>.cached(
|
||||
|
@ -8,6 +8,7 @@ import dev.inmo.micro_utils.repos.*
|
||||
import dev.inmo.micro_utils.repos.cache.*
|
||||
import dev.inmo.micro_utils.repos.cache.cache.FullKVCache
|
||||
import dev.inmo.micro_utils.repos.cache.cache.KVCache
|
||||
import dev.inmo.micro_utils.repos.cache.util.actualizeAll
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
||||
@ -32,12 +33,7 @@ open class FullReadCRUDCacheRepo<ObjectType, IdType>(
|
||||
}
|
||||
|
||||
protected open suspend fun actualizeAll() {
|
||||
kvCache.clear()
|
||||
doForAllWithNextPaging {
|
||||
parentRepo.getByPagination(it).also {
|
||||
kvCache.set(it.results.associateBy { idGetter(it) })
|
||||
}
|
||||
}
|
||||
kvCache.actualizeAll(parentRepo)
|
||||
}
|
||||
|
||||
override suspend fun getByPagination(pagination: Pagination): PaginationResult<ObjectType> = doOrTakeAndActualize(
|
||||
@ -69,6 +65,10 @@ open class FullReadCRUDCacheRepo<ObjectType, IdType>(
|
||||
{ getById(id) },
|
||||
{ it ?.let { set(idGetter(it), it) } }
|
||||
)
|
||||
|
||||
override suspend fun invalidate() {
|
||||
actualizeAll()
|
||||
}
|
||||
}
|
||||
|
||||
fun <ObjectType, IdType> ReadCRUDRepo<ObjectType, IdType>.cached(
|
||||
@ -92,7 +92,11 @@ open class FullCRUDCacheRepo<ObjectType, IdType, InputValueType>(
|
||||
scope,
|
||||
idGetter
|
||||
),
|
||||
CRUDRepo<ObjectType, IdType, InputValueType>
|
||||
CRUDRepo<ObjectType, IdType, InputValueType> {
|
||||
override suspend fun invalidate() {
|
||||
actualizeAll()
|
||||
}
|
||||
}
|
||||
|
||||
fun <ObjectType, IdType, InputType> CRUDRepo<ObjectType, IdType, InputType>.cached(
|
||||
kvCache: FullKVCache<IdType, ObjectType>,
|
||||
|
@ -5,6 +5,7 @@ import dev.inmo.micro_utils.pagination.Pagination
|
||||
import dev.inmo.micro_utils.pagination.PaginationResult
|
||||
import dev.inmo.micro_utils.repos.*
|
||||
import dev.inmo.micro_utils.repos.cache.cache.FullKVCache
|
||||
import dev.inmo.micro_utils.repos.cache.util.actualizeAll
|
||||
import dev.inmo.micro_utils.repos.pagination.getAll
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@ -68,6 +69,10 @@ open class FullReadKeyValueCacheRepo<Key,Value>(
|
||||
{ parentRepo.keys(v, pagination, reversed) },
|
||||
{ if (it.results.isNotEmpty()) actualizeAll() }
|
||||
)
|
||||
|
||||
override suspend fun invalidate() {
|
||||
actualizeAll()
|
||||
}
|
||||
}
|
||||
|
||||
fun <Key, Value> ReadKeyValueRepo<Key, Value>.cached(
|
||||
@ -81,6 +86,10 @@ open class FullWriteKeyValueCacheRepo<Key,Value>(
|
||||
) : WriteKeyValueRepo<Key, Value> by parentRepo, FullCacheRepo {
|
||||
protected val onNewJob = parentRepo.onNewValue.onEach { kvCache.set(it.first, it.second) }.launchIn(scope)
|
||||
protected val onRemoveJob = parentRepo.onValueRemoved.onEach { kvCache.unset(it) }.launchIn(scope)
|
||||
|
||||
override suspend fun invalidate() {
|
||||
kvCache.clear()
|
||||
}
|
||||
}
|
||||
|
||||
fun <Key, Value> WriteKeyValueRepo<Key, Value>.caching(
|
||||
@ -89,13 +98,17 @@ fun <Key, Value> WriteKeyValueRepo<Key, Value>.caching(
|
||||
) = FullWriteKeyValueCacheRepo(this, kvCache, scope)
|
||||
|
||||
open class FullKeyValueCacheRepo<Key,Value>(
|
||||
parentRepo: KeyValueRepo<Key, Value>,
|
||||
override val parentRepo: KeyValueRepo<Key, Value>,
|
||||
kvCache: FullKVCache<Key, Value>,
|
||||
scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
||||
) : FullWriteKeyValueCacheRepo<Key,Value>(parentRepo, kvCache, scope),
|
||||
KeyValueRepo<Key,Value>,
|
||||
ReadKeyValueRepo<Key, Value> by FullReadKeyValueCacheRepo(parentRepo, kvCache) {
|
||||
override suspend fun unsetWithValues(toUnset: List<Value>) = parentRepo.unsetWithValues(toUnset)
|
||||
|
||||
override suspend fun invalidate() {
|
||||
kvCache.actualizeAll(parentRepo)
|
||||
}
|
||||
}
|
||||
|
||||
fun <Key, Value> KeyValueRepo<Key, Value>.cached(
|
||||
|
@ -5,6 +5,7 @@ import dev.inmo.micro_utils.pagination.*
|
||||
import dev.inmo.micro_utils.pagination.utils.*
|
||||
import dev.inmo.micro_utils.repos.*
|
||||
import dev.inmo.micro_utils.repos.cache.cache.FullKVCache
|
||||
import dev.inmo.micro_utils.repos.cache.util.actualizeAll
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.*
|
||||
@ -33,8 +34,7 @@ open class FullReadKeyValuesCacheRepo<Key,Value>(
|
||||
}
|
||||
|
||||
protected open suspend fun actualizeAll() {
|
||||
doAllWithCurrentPaging { kvCache.keys(it).also { kvCache.unset(it.results) } }
|
||||
kvCache.set(parentRepo.getAll())
|
||||
kvCache.actualizeAll(parentRepo)
|
||||
}
|
||||
|
||||
override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult<Value> {
|
||||
@ -102,6 +102,9 @@ open class FullReadKeyValuesCacheRepo<Key,Value>(
|
||||
{ if (it.results.isNotEmpty()) actualizeAll() }
|
||||
)
|
||||
|
||||
override suspend fun invalidate() {
|
||||
actualizeAll()
|
||||
}
|
||||
}
|
||||
|
||||
fun <Key, Value> ReadKeyValuesRepo<Key, Value>.cached(
|
||||
@ -125,6 +128,10 @@ open class FullWriteKeyValuesCacheRepo<Key,Value>(
|
||||
kvCache.get(it.first) ?.minus(it.second) ?: return@onEach
|
||||
)
|
||||
}.launchIn(scope)
|
||||
|
||||
override suspend fun invalidate() {
|
||||
kvCache.clear()
|
||||
}
|
||||
}
|
||||
|
||||
fun <Key, Value> WriteKeyValuesRepo<Key, Value>.caching(
|
||||
@ -133,7 +140,7 @@ fun <Key, Value> WriteKeyValuesRepo<Key, Value>.caching(
|
||||
) = FullWriteKeyValuesCacheRepo(this, kvCache, scope)
|
||||
|
||||
open class FullKeyValuesCacheRepo<Key,Value>(
|
||||
parentRepo: KeyValuesRepo<Key, Value>,
|
||||
override val parentRepo: KeyValuesRepo<Key, Value>,
|
||||
kvCache: FullKVCache<Key, List<Value>>,
|
||||
scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
||||
) : FullWriteKeyValuesCacheRepo<Key, Value>(parentRepo, kvCache, scope),
|
||||
@ -146,6 +153,10 @@ open class FullKeyValuesCacheRepo<Key,Value>(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun invalidate() {
|
||||
kvCache.actualizeAll(parentRepo)
|
||||
}
|
||||
}
|
||||
|
||||
fun <Key, Value> KeyValuesRepo<Key, Value>.caching(
|
||||
|
61
repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/util/ActualizeAll.kt
vendored
Normal file
61
repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/util/ActualizeAll.kt
vendored
Normal file
@ -0,0 +1,61 @@
|
||||
package dev.inmo.micro_utils.repos.cache.util
|
||||
|
||||
import dev.inmo.micro_utils.pagination.FirstPagePagination
|
||||
import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging
|
||||
import dev.inmo.micro_utils.repos.ReadCRUDRepo
|
||||
import dev.inmo.micro_utils.repos.ReadKeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.ReadKeyValuesRepo
|
||||
import dev.inmo.micro_utils.repos.cache.cache.KVCache
|
||||
import dev.inmo.micro_utils.repos.set
|
||||
|
||||
suspend inline fun <K, V> KVCache<K, V>.actualizeAll(
|
||||
getAll: () -> Map<K, V>
|
||||
) {
|
||||
clear()
|
||||
set(getAll())
|
||||
}
|
||||
|
||||
suspend inline fun <K, V> KVCache<K, V>.actualizeAll(
|
||||
repo: ReadKeyValueRepo<K, V>
|
||||
) {
|
||||
clear()
|
||||
val count = repo.count().takeIf { it < Int.MAX_VALUE } ?.toInt() ?: Int.MAX_VALUE
|
||||
val initPagination = FirstPagePagination(count)
|
||||
doForAllWithNextPaging(initPagination) {
|
||||
keys(it).also {
|
||||
set(
|
||||
it.results.mapNotNull { k -> repo.get(k) ?.let { k to it } }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend inline fun <K, V> KVCache<K, List<V>>.actualizeAll(
|
||||
repo: ReadKeyValuesRepo<K, V>
|
||||
) {
|
||||
clear()
|
||||
val count = repo.count().takeIf { it < Int.MAX_VALUE } ?.toInt() ?: Int.MAX_VALUE
|
||||
val initPagination = FirstPagePagination(count)
|
||||
doForAllWithNextPaging(initPagination) {
|
||||
keys(it).also {
|
||||
set(
|
||||
it.results.associateWith { k -> repo.getAll(k) }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend inline fun <K, V> KVCache<K, V>.actualizeAll(
|
||||
repo: ReadCRUDRepo<V, K>
|
||||
) {
|
||||
clear()
|
||||
val count = repo.count().takeIf { it < Int.MAX_VALUE } ?.toInt() ?: Int.MAX_VALUE
|
||||
val initPagination = FirstPagePagination(count)
|
||||
doForAllWithNextPaging(initPagination) {
|
||||
keys(it).also {
|
||||
set(
|
||||
it.results.mapNotNull { k -> repo.getById(k) ?.let { k to it } }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
@ -93,6 +93,10 @@ suspend inline fun <Key, Value> WriteKeyValueRepo<Key, Value>.set(
|
||||
vararg toSet: Pair<Key, Value>
|
||||
) = set(toSet.toMap())
|
||||
|
||||
suspend inline fun <Key, Value> WriteKeyValueRepo<Key, Value>.set(
|
||||
toSet: List<Pair<Key, Value>>
|
||||
) = set(toSet.toMap())
|
||||
|
||||
suspend inline fun <Key, Value> WriteKeyValueRepo<Key, Value>.set(
|
||||
k: Key, v: Value
|
||||
) = set(k to v)
|
||||
@ -125,7 +129,11 @@ interface KeyValueRepo<Key, Value> : ReadKeyValueRepo<Key, Value>, WriteKeyValue
|
||||
* By default, will remove all the data of current repo using [doAllWithCurrentPaging], [keys] and [unset]
|
||||
*/
|
||||
suspend fun clear() {
|
||||
doAllWithCurrentPaging { keys(it).also { unset(it.results) } }
|
||||
var count: Int
|
||||
do {
|
||||
count = count().takeIf { it < Int.MAX_VALUE } ?.toInt() ?: Int.MAX_VALUE
|
||||
keys(FirstPagePagination(count)).also { unset(it.results) }
|
||||
} while(count > 0)
|
||||
}
|
||||
}
|
||||
typealias StandardKeyValueRepo<Key,Value> = KeyValueRepo<Key, Value>
|
||||
|
Loading…
Reference in New Issue
Block a user