This commit is contained in:
InsanusMokrassar 2020-11-09 00:51:08 +06:00
parent abfda3627c
commit c914f8c44a
2 changed files with 14 additions and 23 deletions

View File

@ -1,8 +1,7 @@
package dev.inmo.micro_utils.repos
import dev.inmo.micro_utils.coroutines.BroadcastFlow
import dev.inmo.micro_utils.pagination.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.*
class ReadMapCRUDRepo<ObjectType, IdType>(
private val map: Map<IdType, ObjectType> = emptyMap()
@ -26,15 +25,12 @@ class ReadMapCRUDRepo<ObjectType, IdType>(
abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(
private val map: MutableMap<IdType, ObjectType> = mutableMapOf()
) : WriteStandardCRUDRepo<ObjectType, IdType, InputValueType> {
private val _newObjectsFlow: BroadcastFlow<ObjectType> = BroadcastFlow()
override val newObjectsFlow: Flow<ObjectType>
get() = _newObjectsFlow
private val _updatedObjectsFlow: BroadcastFlow<ObjectType> = BroadcastFlow()
override val updatedObjectsFlow: Flow<ObjectType>
get() = _updatedObjectsFlow
private val _deletedObjectsIdsFlow: BroadcastFlow<IdType> = BroadcastFlow()
override val deletedObjectsIdsFlow: Flow<IdType>
get() = _deletedObjectsIdsFlow
private val _newObjectsFlow: MutableSharedFlow<ObjectType> = MutableSharedFlow()
override val newObjectsFlow: Flow<ObjectType> = _newObjectsFlow.asSharedFlow()
private val _updatedObjectsFlow: MutableSharedFlow<ObjectType> = MutableSharedFlow()
override val updatedObjectsFlow: Flow<ObjectType> = _updatedObjectsFlow.asSharedFlow()
private val _deletedObjectsIdsFlow: MutableSharedFlow<IdType> = MutableSharedFlow()
override val deletedObjectsIdsFlow: Flow<IdType> = _deletedObjectsIdsFlow.asSharedFlow()
protected abstract suspend fun updateObject(newValue: InputValueType, id: IdType, old: ObjectType): ObjectType
protected abstract suspend fun createObject(newValue: InputValueType): Pair<IdType, ObjectType>
@ -44,7 +40,7 @@ abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(
val (id, newObject) = createObject(it)
map[id] = newObject
newObject.also { _ ->
_newObjectsFlow.send(newObject)
_newObjectsFlow.emit(newObject)
}
}
}
@ -54,7 +50,7 @@ abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(
return newValue.also {
map[id] = it
_updatedObjectsFlow.send(it)
_updatedObjectsFlow.emit(it)
}
}
@ -64,7 +60,7 @@ abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(
override suspend fun deleteById(ids: List<IdType>) {
ids.forEach {
map.remove(it) ?.also { _ -> _deletedObjectsIdsFlow.send(it) }
map.remove(it) ?.also { _ -> _deletedObjectsIdsFlow.emit(it) }
}
}

View File

@ -1,11 +1,9 @@
package dev.inmo.micro_utils.repos
import dev.inmo.micro_utils.coroutines.BroadcastFlow
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.pagination.utils.paginate
import dev.inmo.micro_utils.pagination.utils.reverse
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.*
class MapReadOneToManyKeyValueRepo<Key, Value>(
private val map: Map<Key, List<Value>> = emptyMap()
@ -47,14 +45,11 @@ class MapWriteOneToManyKeyValueRepo<Key, Value>(
private val map: MutableMap<Key, MutableList<Value>> = mutableMapOf()
) : WriteOneToManyKeyValueRepo<Key, Value> {
private val _onNewValue: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
override val onNewValue: Flow<Pair<Key, Value>>
get() = _onNewValue
override val onNewValue: Flow<Pair<Key, Value>> = _onNewValue.asSharedFlow()
private val _onValueRemoved: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
override val onValueRemoved: Flow<Pair<Key, Value>>
get() = _onValueRemoved
override val onValueRemoved: Flow<Pair<Key, Value>> = _onValueRemoved.asSharedFlow()
private val _onDataCleared: MutableSharedFlow<Key> = MutableSharedFlow()
override val onDataCleared: Flow<Key>
get() = _onDataCleared
override val onDataCleared: Flow<Key> = _onDataCleared.asSharedFlow()
override suspend fun add(toAdd: Map<Key, List<Value>>) {
toAdd.keys.forEach { k ->