mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-18 06:49:20 +00:00
Compare commits
32 Commits
Author | SHA1 | Date | |
---|---|---|---|
64cc42a23b | |||
325f178763 | |||
fc8d0e52ef | |||
e58348907e | |||
eaba9173ae | |||
2f42b30f87 | |||
35913b95be | |||
8023fa1b76 | |||
4cbe2d1d61 | |||
740036e8d9 | |||
273a7aa9a4 | |||
dccb479c3c | |||
6f5c6e5ebe | |||
8495cc6263 | |||
d242d8dcf4 | |||
864d576e70 | |||
f0127b018e | |||
5eb48a58bf | |||
b690f68c7f | |||
7f19b83828 | |||
98a1ec82db | |||
63313ff964 | |||
210e32bed4 | |||
5b325a8ff9 | |||
4582e0c817 | |||
e2d1c5d6a1 | |||
4019ad7d31 | |||
e7df21e91a | |||
b2e30c9f6d | |||
0b27b5cc06 | |||
347e9c32fe | |||
cc18f58e4c |
56
CHANGELOG.md
56
CHANGELOG.md
@@ -1,5 +1,61 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 0.2.7
|
||||||
|
|
||||||
|
* `Versions`:
|
||||||
|
* `Coroutines`: `1.4.0` -> `1.4.1`
|
||||||
|
* `Repos`:
|
||||||
|
* `WriteStandardKeyValueRepo` got new methods `set` and `unset` with collections
|
||||||
|
* All standard realizations of repos got collections methods realizations
|
||||||
|
* All old usages of `BroadcastFlow` and `BroadcastChannel` has been replaced with `MutableSharedFlow`
|
||||||
|
* `Ktor`:
|
||||||
|
* `Server`:
|
||||||
|
* Fixed incorrect answer for `keyvalue`
|
||||||
|
|
||||||
|
## 0.2.6
|
||||||
|
|
||||||
|
* `Pagination`
|
||||||
|
* Fixes in function `List#paginate`
|
||||||
|
* Extension property `Pagination#lastIndexExclusive`
|
||||||
|
|
||||||
|
## 0.2.5
|
||||||
|
|
||||||
|
* `Coroutines`
|
||||||
|
* Function `safelyWithoutExceptions`
|
||||||
|
* Extension `CoroutineScope#safeActor`
|
||||||
|
|
||||||
|
## 0.2.4
|
||||||
|
|
||||||
|
* `Versions`
|
||||||
|
* `Serialization`: `1.0.0` -> `1.0.1`
|
||||||
|
* `Common`
|
||||||
|
* Full rework of `DiffUtils`
|
||||||
|
* Data class `Diff` has been added
|
||||||
|
* Extension `Iterable#calculateDiff` has been added
|
||||||
|
* Extension `Iterable#calculateStrictDiff` as replacement for `Iterable#calculateDiff` with
|
||||||
|
`strictComparison` mode enabled
|
||||||
|
* Functions `Diff` (as analog of `Iterable#calculateDiff`) and `StrictDiff` (as analog of
|
||||||
|
`Iterable#calculateStrictDiff`)
|
||||||
|
* `Coroutines`
|
||||||
|
* `BroadcastFlow` now is deprecated
|
||||||
|
* `BroadcastStateFlow` now is deprecated
|
||||||
|
* New extensions for `Flow`s:
|
||||||
|
* `Flow#subscribe`
|
||||||
|
* `Flow#subscribeSafely`
|
||||||
|
* `Flow#subscribeSafelyWithoutExceptions`
|
||||||
|
|
||||||
|
## 0.2.3
|
||||||
|
|
||||||
|
* `Versions`
|
||||||
|
* `Coroutines`: `1.3.9` -> `1.4.0`
|
||||||
|
* `Exposed`: `0.27.1` -> `0.28.1`
|
||||||
|
* `Common`
|
||||||
|
* `K/JS`
|
||||||
|
* Add several extensions for `Element` objects to detect that object is on screen viewport
|
||||||
|
* Add several extensions for `Element` objects to detect object visibility
|
||||||
|
* `Coroutines`
|
||||||
|
* `BroadcastStateFlow` now use different strategy for getting of state and implements `replayCache`
|
||||||
|
|
||||||
## 0.2.2
|
## 0.2.2
|
||||||
|
|
||||||
* `Repos`
|
* `Repos`
|
||||||
|
@@ -1,10 +1,166 @@
|
|||||||
|
@file:Suppress("NOTHING_TO_INLINE")
|
||||||
|
|
||||||
package dev.inmo.micro_utils.common
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
fun <T> Iterable<T>.syncWith(
|
private inline fun <T> getObject(
|
||||||
other: Iterable<T>,
|
additional: MutableList<T>,
|
||||||
removed: (List<T>) -> Unit = {},
|
iterator: Iterator<T>
|
||||||
added: (List<T>) -> Unit = {}
|
): T? = when {
|
||||||
) {
|
additional.isNotEmpty() -> additional.removeFirst()
|
||||||
removed(filter { it !in other })
|
iterator.hasNext() -> iterator.next()
|
||||||
added(other.filter { it !in this })
|
else -> null
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Diff object which contains information about differences between two [Iterable]s
|
||||||
|
*
|
||||||
|
* @see calculateDiff
|
||||||
|
*/
|
||||||
|
data class Diff<T> internal constructor(
|
||||||
|
val removed: List<IndexedValue<T>>,
|
||||||
|
/**
|
||||||
|
* Old-New values pairs
|
||||||
|
*/
|
||||||
|
val replaced: List<Pair<IndexedValue<T>, IndexedValue<T>>>,
|
||||||
|
val added: List<IndexedValue<T>>
|
||||||
|
)
|
||||||
|
|
||||||
|
private inline fun <T> performChanges(
|
||||||
|
potentialChanges: MutableList<Pair<IndexedValue<T>?, IndexedValue<T>?>>,
|
||||||
|
additionalsInOld: MutableList<T>,
|
||||||
|
additionalsInNew: MutableList<T>,
|
||||||
|
changedList: MutableList<Pair<IndexedValue<T>, IndexedValue<T>>>,
|
||||||
|
removedList: MutableList<IndexedValue<T>>,
|
||||||
|
addedList: MutableList<IndexedValue<T>>,
|
||||||
|
strictComparison: Boolean
|
||||||
|
) {
|
||||||
|
var i = -1
|
||||||
|
val (oldObject, newObject) = potentialChanges.lastOrNull() ?: return
|
||||||
|
for ((old, new) in potentialChanges.take(potentialChanges.size - 1)) {
|
||||||
|
i++
|
||||||
|
val oldOneEqualToNewObject = old ?.value === newObject ?.value || (old ?.value == newObject ?.value && !strictComparison)
|
||||||
|
val newOneEqualToOldObject = new ?.value === oldObject ?.value || (new ?.value == oldObject ?.value && !strictComparison)
|
||||||
|
if (oldOneEqualToNewObject || newOneEqualToOldObject) {
|
||||||
|
changedList.addAll(
|
||||||
|
potentialChanges.take(i).mapNotNull {
|
||||||
|
if (it.first != null && it.second != null) it as Pair<IndexedValue<T>, IndexedValue<T>> else null
|
||||||
|
}
|
||||||
|
)
|
||||||
|
val newPotentials = potentialChanges.drop(i).take(potentialChanges.size - i)
|
||||||
|
when {
|
||||||
|
oldOneEqualToNewObject -> {
|
||||||
|
newPotentials.first().second ?.let { addedList.add(it) }
|
||||||
|
newPotentials.drop(1).take(newPotentials.size - 2).forEach { (oldOne, newOne) ->
|
||||||
|
addedList.add(newOne!!)
|
||||||
|
oldOne ?.let { additionalsInOld.add(oldOne.value) }
|
||||||
|
}
|
||||||
|
if (newPotentials.size > 1) {
|
||||||
|
newPotentials.last().first ?.value ?.let { additionalsInOld.add(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newOneEqualToOldObject -> {
|
||||||
|
newPotentials.first().first ?.let { removedList.add(it) }
|
||||||
|
newPotentials.drop(1).take(newPotentials.size - 2).forEach { (oldOne, newOne) ->
|
||||||
|
removedList.add(oldOne!!)
|
||||||
|
newOne ?.let { additionalsInNew.add(newOne.value) }
|
||||||
|
}
|
||||||
|
if (newPotentials.size > 1) {
|
||||||
|
newPotentials.last().second ?.value ?.let { additionalsInNew.add(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
potentialChanges.clear()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (potentialChanges.isNotEmpty() && potentialChanges.last().let { it.first == null && it.second == null }) {
|
||||||
|
potentialChanges.dropLast(1).forEach { (old, new) ->
|
||||||
|
when {
|
||||||
|
old != null && new != null -> changedList.add(old to new)
|
||||||
|
old != null -> removedList.add(old)
|
||||||
|
new != null -> addedList.add(new)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculating [Diff] object
|
||||||
|
*
|
||||||
|
* @param strictComparison If this parameter set to true, objects which are not equal by links will be used as different
|
||||||
|
* objects. For example, in case of two "Example" string they will be equal by value, but CAN be different by links
|
||||||
|
*/
|
||||||
|
fun <T> Iterable<T>.calculateDiff(
|
||||||
|
other: Iterable<T>,
|
||||||
|
strictComparison: Boolean = false
|
||||||
|
): Diff<T> {
|
||||||
|
var i = -1
|
||||||
|
var j = -1
|
||||||
|
|
||||||
|
val additionalInOld = mutableListOf<T>()
|
||||||
|
val additionalInNew = mutableListOf<T>()
|
||||||
|
|
||||||
|
val oldIterator = iterator()
|
||||||
|
val newIterator = other.iterator()
|
||||||
|
|
||||||
|
val potentiallyChangedObjects = mutableListOf<Pair<IndexedValue<T>?, IndexedValue<T>?>>()
|
||||||
|
val changedObjects = mutableListOf<Pair<IndexedValue<T>, IndexedValue<T>>>()
|
||||||
|
val addedObjects = mutableListOf<IndexedValue<T>>()
|
||||||
|
val removedObjects = mutableListOf<IndexedValue<T>>()
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
i++
|
||||||
|
j++
|
||||||
|
|
||||||
|
val oldObject = getObject(additionalInOld, oldIterator)
|
||||||
|
val newObject = getObject(additionalInNew, newIterator)
|
||||||
|
|
||||||
|
if (oldObject == null && newObject == null) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
when {
|
||||||
|
oldObject === newObject || (oldObject == newObject && !strictComparison) -> {
|
||||||
|
changedObjects.addAll(potentiallyChangedObjects.map { it as Pair<IndexedValue<T>, IndexedValue<T>> })
|
||||||
|
potentiallyChangedObjects.clear()
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
potentiallyChangedObjects.add(oldObject ?.let { IndexedValue(i, oldObject) } to newObject ?.let { IndexedValue(j, newObject) })
|
||||||
|
val previousOldsAdditionsSize = additionalInOld.size
|
||||||
|
val previousNewsAdditionsSize = additionalInNew.size
|
||||||
|
performChanges(potentiallyChangedObjects, additionalInOld, additionalInNew, changedObjects, removedObjects, addedObjects, strictComparison)
|
||||||
|
i -= (additionalInOld.size - previousOldsAdditionsSize)
|
||||||
|
j -= (additionalInNew.size - previousNewsAdditionsSize)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
potentiallyChangedObjects.add(null to null)
|
||||||
|
performChanges(potentiallyChangedObjects, additionalInOld, additionalInNew, changedObjects, removedObjects, addedObjects, strictComparison)
|
||||||
|
|
||||||
|
return Diff(removedObjects.toList(), changedObjects.toList(), addedObjects.toList())
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <T> Diff(old: Iterable<T>, new: Iterable<T>) = old.calculateDiff(new)
|
||||||
|
inline fun <T> StrictDiff(old: Iterable<T>, new: Iterable<T>) = old.calculateDiff(new, true)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method call [calculateDiff] with strict mode enabled
|
||||||
|
*/
|
||||||
|
inline fun <T> Iterable<T>.calculateStrictDiff(
|
||||||
|
other: Iterable<T>
|
||||||
|
) = calculateDiff(other, strictComparison = true)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compare one-to-one
|
||||||
|
*/
|
||||||
|
@Deprecated("Will be removed or replaced with some new function. Use calculateDiff instead")
|
||||||
|
inline fun <T> Iterable<T>.syncWith(
|
||||||
|
other: Iterable<T>,
|
||||||
|
noinline removed: (List<T>) -> Unit = {},
|
||||||
|
noinline added: (List<T>) -> Unit = {}
|
||||||
|
) {
|
||||||
|
calculateDiff(other).also {
|
||||||
|
removed(it.removed.map { it.value })
|
||||||
|
added(it.added.map { it.value })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,76 @@
|
|||||||
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
import kotlin.math.floor
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
class DiffUtilsTests {
|
||||||
|
@Test
|
||||||
|
fun testThatSimpleRemoveWorks() {
|
||||||
|
val oldList = (0 until 10).toList()
|
||||||
|
val withIndex = oldList.withIndex()
|
||||||
|
|
||||||
|
for (count in 1 .. (floor(oldList.size.toFloat() / 2).toInt())) {
|
||||||
|
for ((i, v) in withIndex) {
|
||||||
|
if (i + count > oldList.lastIndex) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val removedSublist = oldList.subList(i, i + count)
|
||||||
|
oldList.calculateDiff(oldList - removedSublist).apply {
|
||||||
|
assertEquals(
|
||||||
|
removedSublist.mapIndexed { j, o -> IndexedValue(i + j, o) },
|
||||||
|
removed
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testThatSimpleAddWorks() {
|
||||||
|
val oldList = (0 until 10).map { it.toString() }
|
||||||
|
val withIndex = oldList.withIndex()
|
||||||
|
|
||||||
|
for (count in 1 .. (floor(oldList.size.toFloat() / 2).toInt())) {
|
||||||
|
for ((i, v) in withIndex) {
|
||||||
|
if (i + count > oldList.lastIndex) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val addedSublist = oldList.subList(i, i + count).map { "added$it" }
|
||||||
|
val mutable = oldList.toMutableList()
|
||||||
|
mutable.addAll(i, addedSublist)
|
||||||
|
oldList.calculateDiff(mutable).apply {
|
||||||
|
assertEquals(
|
||||||
|
addedSublist.mapIndexed { j, o -> IndexedValue(i + j, o) },
|
||||||
|
added
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testThatSimpleChangesWorks() {
|
||||||
|
val oldList = (0 until 10).map { it.toString() }
|
||||||
|
val withIndex = oldList.withIndex()
|
||||||
|
|
||||||
|
for (step in 0 until oldList.size) {
|
||||||
|
for ((i, v) in withIndex) {
|
||||||
|
val mutable = oldList.toMutableList()
|
||||||
|
val changes = (
|
||||||
|
if (step == 0) i until oldList.size else (i until oldList.size step step)
|
||||||
|
).map { index ->
|
||||||
|
IndexedValue(index, mutable[index]) to IndexedValue(index, "changed$index").also {
|
||||||
|
mutable[index] = it.value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
oldList.calculateDiff(mutable).apply {
|
||||||
|
assertEquals(
|
||||||
|
changes,
|
||||||
|
replaced
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,43 @@
|
|||||||
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
import kotlinx.browser.window
|
||||||
|
import org.w3c.dom.DOMRect
|
||||||
|
import org.w3c.dom.Element
|
||||||
|
|
||||||
|
val DOMRect.isOnScreenByLeftEdge: Boolean
|
||||||
|
get() = left >= 0 && left <= window.innerWidth
|
||||||
|
inline val Element.isOnScreenByLeftEdge
|
||||||
|
get() = getBoundingClientRect().isOnScreenByLeftEdge
|
||||||
|
|
||||||
|
val DOMRect.isOnScreenByRightEdge: Boolean
|
||||||
|
get() = right >= 0 && right <= window.innerWidth
|
||||||
|
inline val Element.isOnScreenByRightEdge
|
||||||
|
get() = getBoundingClientRect().isOnScreenByRightEdge
|
||||||
|
|
||||||
|
internal val DOMRect.isOnScreenHorizontally: Boolean
|
||||||
|
get() = isOnScreenByLeftEdge || isOnScreenByRightEdge
|
||||||
|
|
||||||
|
|
||||||
|
val DOMRect.isOnScreenByTopEdge: Boolean
|
||||||
|
get() = top >= 0 && top <= window.innerHeight
|
||||||
|
inline val Element.isOnScreenByTopEdge
|
||||||
|
get() = getBoundingClientRect().isOnScreenByTopEdge
|
||||||
|
|
||||||
|
val DOMRect.isOnScreenByBottomEdge: Boolean
|
||||||
|
get() = bottom >= 0 && bottom <= window.innerHeight
|
||||||
|
inline val Element.isOnScreenByBottomEdge
|
||||||
|
get() = getBoundingClientRect().isOnScreenByBottomEdge
|
||||||
|
|
||||||
|
internal val DOMRect.isOnScreenVertically: Boolean
|
||||||
|
get() = isOnScreenByLeftEdge || isOnScreenByRightEdge
|
||||||
|
|
||||||
|
|
||||||
|
val DOMRect.isOnScreenFully: Boolean
|
||||||
|
get() = isOnScreenByLeftEdge && isOnScreenByTopEdge && isOnScreenByRightEdge && isOnScreenByBottomEdge
|
||||||
|
val Element.isOnScreenFully: Boolean
|
||||||
|
get() = getBoundingClientRect().isOnScreenFully
|
||||||
|
|
||||||
|
val DOMRect.isOnScreen: Boolean
|
||||||
|
get() = isOnScreenFully || (isOnScreenHorizontally && isOnScreenVertically)
|
||||||
|
inline val Element.isOnScreen: Boolean
|
||||||
|
get() = getBoundingClientRect().isOnScreen
|
@@ -0,0 +1,48 @@
|
|||||||
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
import kotlinx.browser.window
|
||||||
|
import org.w3c.dom.Element
|
||||||
|
import org.w3c.dom.css.CSSStyleDeclaration
|
||||||
|
|
||||||
|
sealed class Visibility
|
||||||
|
object Visible : Visibility()
|
||||||
|
object Invisible : Visibility()
|
||||||
|
object Gone : Visibility()
|
||||||
|
|
||||||
|
var CSSStyleDeclaration.visibilityState: Visibility
|
||||||
|
get() = when {
|
||||||
|
display == "none" -> Gone
|
||||||
|
visibility == "hidden" -> Invisible
|
||||||
|
else -> Visible
|
||||||
|
}
|
||||||
|
set(value) {
|
||||||
|
when (value) {
|
||||||
|
Visible -> {
|
||||||
|
if (display == "none") {
|
||||||
|
display = "initial"
|
||||||
|
}
|
||||||
|
visibility = "visible"
|
||||||
|
}
|
||||||
|
Invisible -> {
|
||||||
|
if (display == "none") {
|
||||||
|
display = "initial"
|
||||||
|
}
|
||||||
|
visibility = "hidden"
|
||||||
|
}
|
||||||
|
Gone -> {
|
||||||
|
display = "none"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
inline var Element.visibilityState: Visibility
|
||||||
|
get() = window.getComputedStyle(this).visibilityState
|
||||||
|
set(value) {
|
||||||
|
window.getComputedStyle(this).visibilityState = value
|
||||||
|
}
|
||||||
|
|
||||||
|
inline val Element.isVisible: Boolean
|
||||||
|
get() = visibilityState == Visible
|
||||||
|
inline val Element.isInvisible: Boolean
|
||||||
|
get() = visibilityState == Invisible
|
||||||
|
inline val Element.isGone: Boolean
|
||||||
|
get() = visibilityState == Gone
|
@@ -17,3 +17,15 @@ fun <T> CoroutineScope.actor(
|
|||||||
return channel
|
return channel
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline fun <T> CoroutineScope.safeActor(
|
||||||
|
channelCapacity: Int = Channel.UNLIMITED,
|
||||||
|
noinline onException: ExceptionHandler<Unit> = {},
|
||||||
|
crossinline block: suspend (T) -> Unit
|
||||||
|
): Channel<T> = actor(
|
||||||
|
channelCapacity
|
||||||
|
) {
|
||||||
|
safely(onException) {
|
||||||
|
block(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@@ -1,9 +1,11 @@
|
|||||||
package dev.inmo.micro_utils.coroutines
|
package dev.inmo.micro_utils.coroutines
|
||||||
|
|
||||||
import kotlinx.coroutines.channels.*
|
import kotlinx.coroutines.channels.*
|
||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.asFlow
|
||||||
|
|
||||||
@Suppress("FunctionName")
|
@Suppress("FunctionName")
|
||||||
|
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
|
||||||
fun <T> BroadcastFlow(
|
fun <T> BroadcastFlow(
|
||||||
internalChannelSize: Int = Channel.BUFFERED
|
internalChannelSize: Int = Channel.BUFFERED
|
||||||
): BroadcastFlow<T> {
|
): BroadcastFlow<T> {
|
||||||
@@ -15,6 +17,7 @@ fun <T> BroadcastFlow(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
|
||||||
class BroadcastFlow<T> internal constructor(
|
class BroadcastFlow<T> internal constructor(
|
||||||
private val channel: BroadcastChannel<T>,
|
private val channel: BroadcastChannel<T>,
|
||||||
private val flow: Flow<T>
|
private val flow: Flow<T>
|
||||||
|
@@ -5,29 +5,64 @@ import kotlinx.coroutines.channels.BroadcastChannel
|
|||||||
import kotlinx.coroutines.channels.Channel
|
import kotlinx.coroutines.channels.Channel
|
||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.flow.*
|
||||||
|
|
||||||
|
const val defaultBroadcastStateFlowReplayCacheSize = 1
|
||||||
|
|
||||||
|
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
|
||||||
class BroadcastStateFlow<T> internal constructor(
|
class BroadcastStateFlow<T> internal constructor(
|
||||||
parentFlow: Flow<T>,
|
parentFlow: Flow<T>,
|
||||||
private val stateGetter: () -> T
|
initial: T,
|
||||||
|
replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize,
|
||||||
|
replayScope: CoroutineScope
|
||||||
) : StateFlow<T>, Flow<T> by parentFlow {
|
) : StateFlow<T>, Flow<T> by parentFlow {
|
||||||
|
private val deque = ArrayDeque<T>(1).also {
|
||||||
|
it.add(initial)
|
||||||
|
}
|
||||||
|
override val replayCache: List<T>
|
||||||
|
get() = deque.toList()
|
||||||
override val value: T
|
override val value: T
|
||||||
get() = stateGetter()
|
get() = deque.last()
|
||||||
}
|
|
||||||
|
|
||||||
fun <T> BroadcastChannel<T>.asStateFlow(value: T, scope: CoroutineScope): StateFlow<T> = asFlow().let {
|
init {
|
||||||
var state: T = value
|
if (replayCacheSize < 1) {
|
||||||
it.onEach { state = it }.launchIn(scope)
|
error("Replay cache size can't be less than 1, but was $replayCacheSize")
|
||||||
BroadcastStateFlow(it) {
|
}
|
||||||
state
|
parentFlow.onEach {
|
||||||
|
deque.addLast(it)
|
||||||
|
if (deque.size > replayCacheSize) {
|
||||||
|
deque.removeFirst()
|
||||||
|
}
|
||||||
|
}.launchIn(replayScope)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> BroadcastChannel<T?>.asStateFlow(scope: CoroutineScope): StateFlow<T?> = asStateFlow(null, scope)
|
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
|
||||||
|
fun <T> BroadcastChannel<T>.asStateFlow(
|
||||||
|
value: T,
|
||||||
|
scope: CoroutineScope,
|
||||||
|
replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize
|
||||||
|
): StateFlow<T> = BroadcastStateFlow(asFlow(), value, replayCacheSize, scope)
|
||||||
|
|
||||||
fun <T> broadcastStateFlow(initial: T, scope: CoroutineScope, channelSize: Int = Channel.BUFFERED) = BroadcastChannel<T>(
|
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
|
||||||
|
fun <T> BroadcastChannel<T?>.asStateFlow(
|
||||||
|
scope: CoroutineScope,
|
||||||
|
replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize
|
||||||
|
): StateFlow<T?> = asStateFlow(null, scope, replayCacheSize)
|
||||||
|
|
||||||
|
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
|
||||||
|
fun <T> broadcastStateFlow(
|
||||||
|
initial: T, scope: CoroutineScope,
|
||||||
|
channelSize: Int = Channel.BUFFERED,
|
||||||
|
replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize
|
||||||
|
) = BroadcastChannel<T>(
|
||||||
channelSize
|
channelSize
|
||||||
).let {
|
).let {
|
||||||
it to it.asStateFlow(initial, scope)
|
it to it.asStateFlow(initial, scope, replayCacheSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> broadcastStateFlow(scope: CoroutineScope, channelSize: Int = Channel.BUFFERED) = broadcastStateFlow<T?>(null, scope, channelSize)
|
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
|
||||||
|
fun <T> broadcastStateFlow(
|
||||||
|
scope: CoroutineScope,
|
||||||
|
channelSize: Int = Channel.BUFFERED,
|
||||||
|
replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize
|
||||||
|
) = broadcastStateFlow<T?>(null, scope, channelSize, replayCacheSize)
|
||||||
|
|
||||||
|
@@ -0,0 +1,37 @@
|
|||||||
|
@file:Suppress("NOTHING_TO_INLINE")
|
||||||
|
|
||||||
|
package dev.inmo.micro_utils.coroutines
|
||||||
|
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.flow.*
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shortcut for chain if [Flow.onEach] and [Flow.launchIn]
|
||||||
|
*/
|
||||||
|
inline fun <T> Flow<T>.subscribe(scope: CoroutineScope, noinline block: suspend (T) -> Unit) = onEach(block).launchIn(scope)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use [subscribe], but all [block]s will be called inside of [safely] function.
|
||||||
|
* Use [onException] to set up your reaction for [Throwable]s
|
||||||
|
*/
|
||||||
|
inline fun <T> Flow<T>.subscribeSafely(
|
||||||
|
scope: CoroutineScope,
|
||||||
|
noinline onException: ExceptionHandler<Unit> = { throw it },
|
||||||
|
noinline block: suspend (T) -> Unit
|
||||||
|
) = subscribe(scope) {
|
||||||
|
safely(onException) {
|
||||||
|
block(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use [subscribeSafelyWithoutExceptions], but all exceptions inside of [safely] will be skipped
|
||||||
|
*/
|
||||||
|
inline fun <T> Flow<T>.subscribeSafelyWithoutExceptions(
|
||||||
|
scope: CoroutineScope,
|
||||||
|
noinline block: suspend (T) -> Unit
|
||||||
|
) = subscribeSafely(
|
||||||
|
scope,
|
||||||
|
{},
|
||||||
|
block
|
||||||
|
)
|
@@ -21,3 +21,10 @@ suspend inline fun <T> safely(
|
|||||||
onException(e)
|
onException(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shortcut for [safely] without exception handler (instead of this you will receive null as a result)
|
||||||
|
*/
|
||||||
|
suspend inline fun <T> safelyWithoutExceptions(
|
||||||
|
noinline block: suspend CoroutineScope.() -> T
|
||||||
|
): T? = safely({ null }, block)
|
||||||
|
88
dokka/build.gradle
Normal file
88
dokka/build.gradle
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
mavenLocal()
|
||||||
|
jcenter()
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
|
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
|
||||||
|
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
id "org.jetbrains.dokka" version "$dokka_version"
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenLocal()
|
||||||
|
jcenter()
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
jvm()
|
||||||
|
js(BOTH) {
|
||||||
|
browser()
|
||||||
|
nodejs()
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceSets {
|
||||||
|
commonMain {
|
||||||
|
dependencies {
|
||||||
|
implementation kotlin('stdlib')
|
||||||
|
|
||||||
|
project.parent.subprojects.forEach {
|
||||||
|
if (
|
||||||
|
it != project
|
||||||
|
&& it.hasProperty("kotlin")
|
||||||
|
&& it.kotlin.sourceSets.any { it.name.contains("commonMain") }
|
||||||
|
) {
|
||||||
|
api it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<SourceDirectorySet> findSourcesWithName(String... approximateNames) {
|
||||||
|
return parent.subprojects
|
||||||
|
.findAll { it != project && it.hasProperty("kotlin") }
|
||||||
|
.collectMany { it.kotlin.sourceSets }
|
||||||
|
.findAll { sourceSet ->
|
||||||
|
approximateNames.any { nameToFilter ->
|
||||||
|
sourceSet.name.contains(nameToFilter)
|
||||||
|
}
|
||||||
|
}.collect { it.kotlin }
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.dokkaHtml {
|
||||||
|
dokkaSourceSets {
|
||||||
|
configureEach {
|
||||||
|
skipDeprecated.set(true)
|
||||||
|
|
||||||
|
sourceLink {
|
||||||
|
localDirectory.set(file("./"))
|
||||||
|
remoteUrl.set(new URL("https://github.com/InsanusMokrassar/MicroUtils/blob/master/"))
|
||||||
|
remoteLineSuffix.set("#L")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
named("commonMain") {
|
||||||
|
sourceRoots.setFrom(findSourcesWithName("commonMain"))
|
||||||
|
}
|
||||||
|
|
||||||
|
named("jsMain") {
|
||||||
|
sourceRoots.setFrom(findSourcesWithName("jsMain", "commonMain"))
|
||||||
|
}
|
||||||
|
|
||||||
|
named("jvmMain") {
|
||||||
|
sourceRoots.setFrom(findSourcesWithName("jvmMain", "commonMain"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
dokka/gradle.properties
Normal file
3
dokka/gradle.properties
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
dokka_version=1.4.0
|
||||||
|
|
||||||
|
org.gradle.jvmargs=-Xmx1024m
|
@@ -5,9 +5,9 @@ kotlin.incremental=true
|
|||||||
kotlin.incremental.js=true
|
kotlin.incremental.js=true
|
||||||
|
|
||||||
kotlin_version=1.4.10
|
kotlin_version=1.4.10
|
||||||
kotlin_coroutines_version=1.3.9
|
kotlin_coroutines_version=1.4.1
|
||||||
kotlin_serialisation_core_version=1.0.0
|
kotlin_serialisation_core_version=1.0.1
|
||||||
kotlin_exposed_version=0.27.1
|
kotlin_exposed_version=0.28.1
|
||||||
|
|
||||||
ktor_version=1.4.1
|
ktor_version=1.4.1
|
||||||
|
|
||||||
@@ -19,4 +19,4 @@ github_release_plugin_version=2.2.12
|
|||||||
uuidVersion=0.2.2
|
uuidVersion=0.2.2
|
||||||
|
|
||||||
group=dev.inmo
|
group=dev.inmo
|
||||||
version=0.2.2
|
version=0.2.7
|
||||||
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,5 +1,5 @@
|
|||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
|
@@ -4,7 +4,8 @@ import dev.inmo.micro_utils.coroutines.safely
|
|||||||
import dev.inmo.micro_utils.ktor.common.*
|
import dev.inmo.micro_utils.ktor.common.*
|
||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import io.ktor.client.features.websocket.ws
|
import io.ktor.client.features.websocket.ws
|
||||||
import io.ktor.http.cio.websocket.*
|
import io.ktor.http.cio.websocket.Frame
|
||||||
|
import io.ktor.http.cio.websocket.readBytes
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.channelFlow
|
import kotlinx.coroutines.flow.channelFlow
|
||||||
import kotlinx.serialization.DeserializationStrategy
|
import kotlinx.serialization.DeserializationStrategy
|
||||||
|
@@ -4,7 +4,8 @@ import dev.inmo.micro_utils.ktor.common.*
|
|||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import io.ktor.client.request.get
|
import io.ktor.client.request.get
|
||||||
import io.ktor.client.request.post
|
import io.ktor.client.request.post
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.DeserializationStrategy
|
||||||
|
import kotlinx.serialization.SerializationStrategy
|
||||||
|
|
||||||
typealias BodyPair<T> = Pair<SerializationStrategy<T>, T>
|
typealias BodyPair<T> = Pair<SerializationStrategy<T>, T>
|
||||||
|
|
||||||
|
@@ -22,13 +22,11 @@ fun <T> Route.includeWebsocketHandling(
|
|||||||
converter: (T) -> StandardKtorSerialInputData
|
converter: (T) -> StandardKtorSerialInputData
|
||||||
) {
|
) {
|
||||||
webSocket(suburl) {
|
webSocket(suburl) {
|
||||||
// println("connected")
|
|
||||||
safely {
|
safely {
|
||||||
flow.collect {
|
flow.collect {
|
||||||
send(converter(it))
|
send(converter(it))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// println("disconnected")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -7,8 +7,8 @@ import io.ktor.http.HttpStatusCode
|
|||||||
import io.ktor.request.receive
|
import io.ktor.request.receive
|
||||||
import io.ktor.response.respond
|
import io.ktor.response.respond
|
||||||
import io.ktor.response.respondBytes
|
import io.ktor.response.respondBytes
|
||||||
import io.ktor.util.toByteArray
|
import kotlinx.serialization.DeserializationStrategy
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.SerializationStrategy
|
||||||
|
|
||||||
suspend fun <T> ApplicationCall.unianswer(
|
suspend fun <T> ApplicationCall.unianswer(
|
||||||
answerSerializer: SerializationStrategy<T>,
|
answerSerializer: SerializationStrategy<T>,
|
||||||
|
@@ -28,6 +28,15 @@ interface Pagination {
|
|||||||
val Pagination.firstIndex: Int
|
val Pagination.firstIndex: Int
|
||||||
get() = page * size
|
get() = page * size
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Last number in index of objects. In fact, one [Pagination] object represent data in next range:
|
||||||
|
*
|
||||||
|
* [[firstIndex], [lastIndex]]; That means, that for [Pagination] with [Pagination.size] == 10 and [Pagination.page] == 1
|
||||||
|
* you will retrieve [Pagination.firstIndex] == 10 and [Pagination.lastIndex] == 19. Here [Pagination.lastIndexExclusive] == 20
|
||||||
|
*/
|
||||||
|
val Pagination.lastIndexExclusive: Int
|
||||||
|
get() = firstIndex + size
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Last number in index of objects. In fact, one [Pagination] object represent data in next range:
|
* Last number in index of objects. In fact, one [Pagination] object represent data in next range:
|
||||||
*
|
*
|
||||||
@@ -35,7 +44,7 @@ val Pagination.firstIndex: Int
|
|||||||
* you will retrieve [Pagination.firstIndex] == 10 and [Pagination.lastIndex] == 19.
|
* you will retrieve [Pagination.firstIndex] == 10 and [Pagination.lastIndex] == 19.
|
||||||
*/
|
*/
|
||||||
val Pagination.lastIndex: Int
|
val Pagination.lastIndex: Int
|
||||||
get() = firstIndex + size - 1
|
get() = lastIndexExclusive - 1
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates pages count for given [datasetSize]
|
* Calculates pages count for given [datasetSize]
|
||||||
|
@@ -21,7 +21,12 @@ fun <T> Iterable<T>.paginate(with: Pagination): PaginationResult<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun <T> List<T>.paginate(with: Pagination): PaginationResult<T> {
|
fun <T> List<T>.paginate(with: Pagination): PaginationResult<T> {
|
||||||
return subList(with.firstIndex, with.lastIndex + 1).createPaginationResult(
|
val firstIndex = maxOf(with.firstIndex, 0)
|
||||||
|
val lastIndex = minOf(with.lastIndexExclusive, size)
|
||||||
|
if (firstIndex > lastIndex) {
|
||||||
|
return emptyPaginationResult()
|
||||||
|
}
|
||||||
|
return subList(firstIndex, lastIndex).createPaginationResult(
|
||||||
with,
|
with,
|
||||||
size.toLong()
|
size.toLong()
|
||||||
)
|
)
|
||||||
|
@@ -43,21 +43,13 @@ interface WriteOneToManyKeyValueRepo<Key, Value> : Repo {
|
|||||||
val onValueRemoved: Flow<Pair<Key, Value>>
|
val onValueRemoved: Flow<Pair<Key, Value>>
|
||||||
val onDataCleared: Flow<Key>
|
val onDataCleared: Flow<Key>
|
||||||
|
|
||||||
suspend fun add(toAdd: Map<Key, List<Value>>) = toAdd.forEach { (k, values) ->
|
suspend fun add(toAdd: Map<Key, List<Value>>)
|
||||||
values.forEach { v ->
|
|
||||||
add(k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@Deprecated("Will be extracted as extension for other add method")
|
@Deprecated("Will be extracted as extension for other add method")
|
||||||
suspend fun add(k: Key, v: Value)
|
suspend fun add(k: Key, v: Value) = add(mapOf(k to listOf(v)))
|
||||||
|
|
||||||
suspend fun remove(toRemove: Map<Key, List<Value>>) = toRemove.forEach { (k, values) ->
|
suspend fun remove(toRemove: Map<Key, List<Value>>)
|
||||||
values.forEach { v ->
|
|
||||||
remove(k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@Deprecated("Will be extracted as extension for other remove method")
|
@Deprecated("Will be extracted as extension for other remove method")
|
||||||
suspend fun remove(k: Key, v: Value)
|
suspend fun remove(k: Key, v: Value) = remove(mapOf(k to listOf(v)))
|
||||||
|
|
||||||
suspend fun clear(k: Key)
|
suspend fun clear(k: Key)
|
||||||
}
|
}
|
||||||
|
@@ -16,8 +16,16 @@ interface WriteStandardKeyValueRepo<Key, Value> : Repo {
|
|||||||
val onNewValue: Flow<Pair<Key, Value>>
|
val onNewValue: Flow<Pair<Key, Value>>
|
||||||
val onValueRemoved: Flow<Key>
|
val onValueRemoved: Flow<Key>
|
||||||
|
|
||||||
|
@Deprecated("Realize set with map instead")
|
||||||
suspend fun set(k: Key, v: Value)
|
suspend fun set(k: Key, v: Value)
|
||||||
|
suspend fun set(toSet: Map<Key, Value>) = toSet.forEach { (k, v) ->
|
||||||
|
set(k, v)
|
||||||
|
}
|
||||||
|
@Deprecated("Realize unset with list instead")
|
||||||
suspend fun unset(k: Key)
|
suspend fun unset(k: Key)
|
||||||
|
suspend fun unset(toUnset: List<Key>) = toUnset.forEach {
|
||||||
|
unset(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface StandardKeyValueRepo<Key, Value> : ReadStandardKeyValueRepo<Key, Value>, WriteStandardKeyValueRepo<Key, Value>
|
interface StandardKeyValueRepo<Key, Value> : ReadStandardKeyValueRepo<Key, Value>, WriteStandardKeyValueRepo<Key, Value>
|
@@ -1,6 +1,5 @@
|
|||||||
package dev.inmo.micro_utils.repos.exposed
|
package dev.inmo.micro_utils.repos.exposed
|
||||||
|
|
||||||
import dev.inmo.micro_utils.repos.Repo
|
|
||||||
import org.jetbrains.exposed.sql.*
|
import org.jetbrains.exposed.sql.*
|
||||||
|
|
||||||
interface ExposedCRUDRepo<ObjectType, IdType> : ExposedRepo {
|
interface ExposedCRUDRepo<ObjectType, IdType> : ExposedRepo {
|
||||||
|
@@ -1,11 +1,9 @@
|
|||||||
package dev.inmo.micro_utils.repos.exposed.keyvalue
|
package dev.inmo.micro_utils.repos.exposed.keyvalue
|
||||||
|
|
||||||
import dev.inmo.micro_utils.repos.StandardKeyValueRepo
|
import dev.inmo.micro_utils.repos.StandardKeyValueRepo
|
||||||
import dev.inmo.micro_utils.repos.exposed.*
|
import dev.inmo.micro_utils.repos.exposed.ColumnAllocator
|
||||||
import kotlinx.coroutines.channels.BroadcastChannel
|
|
||||||
import kotlinx.coroutines.channels.Channel
|
import kotlinx.coroutines.channels.Channel
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.*
|
||||||
import kotlinx.coroutines.flow.asFlow
|
|
||||||
import org.jetbrains.exposed.sql.*
|
import org.jetbrains.exposed.sql.*
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
@@ -20,11 +18,11 @@ open class ExposedKeyValueRepo<Key, Value>(
|
|||||||
valueColumnAllocator,
|
valueColumnAllocator,
|
||||||
tableName
|
tableName
|
||||||
) {
|
) {
|
||||||
private val onNewValueChannel = BroadcastChannel<Pair<Key, Value>>(Channel.BUFFERED)
|
private val _onNewValue = MutableSharedFlow<Pair<Key, Value>>(Channel.BUFFERED)
|
||||||
private val onValueRemovedChannel = BroadcastChannel<Key>(Channel.BUFFERED)
|
private val _onValueRemoved = MutableSharedFlow<Key>(Channel.BUFFERED)
|
||||||
|
|
||||||
override val onNewValue: Flow<Pair<Key, Value>> = onNewValueChannel.asFlow()
|
override val onNewValue: Flow<Pair<Key, Value>> = _onNewValue.asSharedFlow()
|
||||||
override val onValueRemoved: Flow<Key> = onValueRemovedChannel.asFlow()
|
override val onValueRemoved: Flow<Key> = _onValueRemoved.asSharedFlow()
|
||||||
|
|
||||||
override suspend fun set(k: Key, v: Value) {
|
override suspend fun set(k: Key, v: Value) {
|
||||||
transaction(database) {
|
transaction(database) {
|
||||||
@@ -39,14 +37,50 @@ open class ExposedKeyValueRepo<Key, Value>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
onNewValueChannel.send(k to v)
|
_onNewValue.emit(k to v)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun set(toSet: Map<Key, Value>) {
|
||||||
|
transaction(database) {
|
||||||
|
toSet.mapNotNull { (k, v) ->
|
||||||
|
if (update({ keyColumn.eq(k) }) { it[valueColumn] = v } > 0) {
|
||||||
|
k to v
|
||||||
|
} else {
|
||||||
|
val inserted = insert {
|
||||||
|
it[keyColumn] = k
|
||||||
|
it[valueColumn] = v
|
||||||
|
}.getOrNull(keyColumn) != null
|
||||||
|
if (inserted) {
|
||||||
|
k to v
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.forEach {
|
||||||
|
_onNewValue.emit(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun unset(k: Key) {
|
override suspend fun unset(k: Key) {
|
||||||
transaction(database) {
|
transaction(database) {
|
||||||
deleteWhere { keyColumn.eq(k) }
|
deleteWhere { keyColumn.eq(k) }
|
||||||
}
|
}
|
||||||
onValueRemovedChannel.send(k)
|
_onValueRemoved.emit(k)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun unset(toUnset: List<Key>) {
|
||||||
|
transaction(database) {
|
||||||
|
toUnset.mapNotNull {
|
||||||
|
if (deleteWhere { keyColumn.eq(it) } > 0) {
|
||||||
|
it
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.forEach {
|
||||||
|
_onValueRemoved.emit(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -2,7 +2,8 @@ package dev.inmo.micro_utils.repos.exposed.keyvalue
|
|||||||
|
|
||||||
import dev.inmo.micro_utils.pagination.*
|
import dev.inmo.micro_utils.pagination.*
|
||||||
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
|
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
|
||||||
import dev.inmo.micro_utils.repos.exposed.*
|
import dev.inmo.micro_utils.repos.exposed.ColumnAllocator
|
||||||
|
import dev.inmo.micro_utils.repos.exposed.ExposedRepo
|
||||||
import org.jetbrains.exposed.sql.*
|
import org.jetbrains.exposed.sql.*
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
|
@@ -3,8 +3,7 @@ package dev.inmo.micro_utils.repos.exposed.onetomany
|
|||||||
import dev.inmo.micro_utils.coroutines.BroadcastFlow
|
import dev.inmo.micro_utils.coroutines.BroadcastFlow
|
||||||
import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo
|
import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo
|
||||||
import dev.inmo.micro_utils.repos.exposed.ColumnAllocator
|
import dev.inmo.micro_utils.repos.exposed.ColumnAllocator
|
||||||
import dev.inmo.micro_utils.repos.exposed.initTable
|
import kotlinx.coroutines.flow.*
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
import org.jetbrains.exposed.sql.*
|
import org.jetbrains.exposed.sql.*
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
@@ -19,13 +18,13 @@ open class ExposedOneToManyKeyValueRepo<Key, Value>(
|
|||||||
valueColumnAllocator,
|
valueColumnAllocator,
|
||||||
tableName
|
tableName
|
||||||
) {
|
) {
|
||||||
protected val _onNewValue: BroadcastFlow<Pair<Key, Value>> = BroadcastFlow()
|
protected val _onNewValue: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
|
||||||
override val onNewValue: Flow<Pair<Key, Value>>
|
override val onNewValue: Flow<Pair<Key, Value>>
|
||||||
get() = _onNewValue
|
get() = _onNewValue
|
||||||
protected val _onValueRemoved: BroadcastFlow<Pair<Key, Value>> = BroadcastFlow()
|
protected val _onValueRemoved: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
|
||||||
override val onValueRemoved: Flow<Pair<Key, Value>>
|
override val onValueRemoved: Flow<Pair<Key, Value>>
|
||||||
get() = _onValueRemoved
|
get() = _onValueRemoved
|
||||||
protected val _onDataCleared: BroadcastFlow<Key> = BroadcastFlow()
|
protected val _onDataCleared: MutableSharedFlow<Key> = MutableSharedFlow()
|
||||||
override val onDataCleared: Flow<Key>
|
override val onDataCleared: Flow<Key>
|
||||||
get() = _onDataCleared
|
get() = _onDataCleared
|
||||||
|
|
||||||
@@ -35,19 +34,42 @@ open class ExposedOneToManyKeyValueRepo<Key, Value>(
|
|||||||
it[keyColumn] = k
|
it[keyColumn] = k
|
||||||
it[valueColumn] = v
|
it[valueColumn] = v
|
||||||
}
|
}
|
||||||
}.also { _onNewValue.send(k to v) }
|
}.also { _onNewValue.emit(k to v) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun remove(k: Key, v: Value) {
|
override suspend fun add(toAdd: Map<Key, List<Value>>) {
|
||||||
transaction(database) {
|
transaction(database) {
|
||||||
deleteWhere { keyColumn.eq(k).and(valueColumn.eq(v)) }
|
toAdd.keys.flatMap { k ->
|
||||||
}.also { _onValueRemoved.send(k to v) }
|
toAdd[k] ?.mapNotNull { v ->
|
||||||
|
insertIgnore {
|
||||||
|
it[keyColumn] = k
|
||||||
|
it[valueColumn] = v
|
||||||
|
}.getOrNull(keyColumn) ?.let { k to v }
|
||||||
|
} ?: emptyList()
|
||||||
|
}
|
||||||
|
}.forEach { _onNewValue.emit(it) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun remove(toRemove: Map<Key, List<Value>>) {
|
||||||
|
transaction(database) {
|
||||||
|
toRemove.keys.flatMap { k ->
|
||||||
|
toRemove[k] ?.mapNotNull { v ->
|
||||||
|
if (deleteIgnoreWhere { keyColumn.eq(k).and(valueColumn.eq(v)) } > 0 ) {
|
||||||
|
k to v
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
} ?: emptyList()
|
||||||
|
}
|
||||||
|
}.forEach {
|
||||||
|
_onValueRemoved.emit(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun clear(k: Key) {
|
override suspend fun clear(k: Key) {
|
||||||
transaction(database) {
|
transaction(database) {
|
||||||
deleteWhere { keyColumn.eq(k) }
|
deleteWhere { keyColumn.eq(k) }
|
||||||
}.also { _onDataCleared.send(k) }
|
}.also { _onDataCleared.emit(k) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,10 +1,12 @@
|
|||||||
package dev.inmo.micro_utils.repos
|
package dev.inmo.micro_utils.repos
|
||||||
|
|
||||||
import dev.inmo.micro_utils.coroutines.BroadcastFlow
|
import dev.inmo.micro_utils.coroutines.BroadcastFlow
|
||||||
import dev.inmo.micro_utils.pagination.*
|
import dev.inmo.micro_utils.pagination.Pagination
|
||||||
|
import dev.inmo.micro_utils.pagination.PaginationResult
|
||||||
import dev.inmo.micro_utils.pagination.utils.paginate
|
import dev.inmo.micro_utils.pagination.utils.paginate
|
||||||
import dev.inmo.micro_utils.pagination.utils.reverse
|
import dev.inmo.micro_utils.pagination.utils.reverse
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||||
|
|
||||||
class ReadMapKeyValueRepo<Key, Value>(
|
class ReadMapKeyValueRepo<Key, Value>(
|
||||||
private val map: Map<Key, Value> = emptyMap()
|
private val map: Map<Key, Value> = emptyMap()
|
||||||
@@ -46,20 +48,31 @@ class ReadMapKeyValueRepo<Key, Value>(
|
|||||||
class WriteMapKeyValueRepo<Key, Value>(
|
class WriteMapKeyValueRepo<Key, Value>(
|
||||||
private val map: MutableMap<Key, Value> = mutableMapOf()
|
private val map: MutableMap<Key, Value> = mutableMapOf()
|
||||||
) : WriteStandardKeyValueRepo<Key, Value> {
|
) : WriteStandardKeyValueRepo<Key, Value> {
|
||||||
private val _onNewValue: BroadcastFlow<Pair<Key, Value>> = BroadcastFlow()
|
private val _onNewValue: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
|
||||||
override val onNewValue: Flow<Pair<Key, Value>>
|
override val onNewValue: Flow<Pair<Key, Value>>
|
||||||
get() = _onNewValue
|
get() = _onNewValue
|
||||||
private val _onValueRemoved: BroadcastFlow<Key> = BroadcastFlow()
|
private val _onValueRemoved: MutableSharedFlow<Key> = MutableSharedFlow()
|
||||||
override val onValueRemoved: Flow<Key>
|
override val onValueRemoved: Flow<Key>
|
||||||
get() = _onValueRemoved
|
get() = _onValueRemoved
|
||||||
|
|
||||||
override suspend fun set(k: Key, v: Value) {
|
override suspend fun set(k: Key, v: Value) {
|
||||||
map[k] = v
|
map[k] = v
|
||||||
_onNewValue.send(k to v)
|
_onNewValue.emit(k to v)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun set(toSet: Map<Key, Value>) {
|
||||||
|
map.putAll(toSet)
|
||||||
|
toSet.forEach { (k, v) -> _onNewValue.emit(k to v) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun unset(k: Key) {
|
override suspend fun unset(k: Key) {
|
||||||
map.remove(k) ?.also { _onValueRemoved.send(k) }
|
map.remove(k) ?.also { _onValueRemoved.emit(k) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun unset(toUnset: List<Key>) {
|
||||||
|
toUnset.forEach { k ->
|
||||||
|
map.remove(k) ?.also { _ -> _onValueRemoved.emit(k) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -55,13 +55,18 @@ class MapWriteOneToManyKeyValueRepo<Key, Value>(
|
|||||||
override val onDataCleared: Flow<Key>
|
override val onDataCleared: Flow<Key>
|
||||||
get() = _onDataCleared
|
get() = _onDataCleared
|
||||||
|
|
||||||
override suspend fun add(k: Key, v: Value) {
|
override suspend fun add(toAdd: Map<Key, List<Value>>) {
|
||||||
map.getOrPut(k) { mutableListOf() }.add(v)
|
toAdd.keys.forEach {
|
||||||
_onNewValue.send(k to v)
|
map.getOrPut(it) {
|
||||||
|
mutableListOf()
|
||||||
|
}.addAll(toAdd[it] ?: return@forEach)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun remove(k: Key, v: Value) {
|
override suspend fun remove(toRemove: Map<Key, List<Value>>) {
|
||||||
map[k] ?.remove(v) ?.also { _onValueRemoved.send(k to v) }
|
toRemove.keys.forEach {
|
||||||
|
map[it] ?.removeAll(toRemove[it] ?: return@forEach)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun clear(k: Key) {
|
override suspend fun clear(k: Key) {
|
||||||
|
@@ -3,9 +3,7 @@ package dev.inmo.micro_utils.repos.ktor.client.crud
|
|||||||
import dev.inmo.micro_utils.ktor.client.encodeUrlQueryValue
|
import dev.inmo.micro_utils.ktor.client.encodeUrlQueryValue
|
||||||
import dev.inmo.micro_utils.ktor.client.uniget
|
import dev.inmo.micro_utils.ktor.client.uniget
|
||||||
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
|
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
|
||||||
import dev.inmo.micro_utils.pagination.Pagination
|
import dev.inmo.micro_utils.pagination.*
|
||||||
import dev.inmo.micro_utils.pagination.PaginationResult
|
|
||||||
import dev.inmo.micro_utils.pagination.asUrlQueryParts
|
|
||||||
import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo
|
import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo
|
||||||
import dev.inmo.micro_utils.repos.ktor.common.crud.*
|
import dev.inmo.micro_utils.repos.ktor.common.crud.*
|
||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
|
@@ -1,8 +1,6 @@
|
|||||||
package dev.inmo.micro_utils.repos.ktor.client.crud
|
package dev.inmo.micro_utils.repos.ktor.client.crud
|
||||||
|
|
||||||
import dev.inmo.micro_utils.ktor.client.BodyPair
|
import dev.inmo.micro_utils.ktor.client.*
|
||||||
import dev.inmo.micro_utils.ktor.client.createStandardWebsocketFlow
|
|
||||||
import dev.inmo.micro_utils.ktor.client.unipost
|
|
||||||
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
|
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
|
||||||
import dev.inmo.micro_utils.repos.UpdatedValuePair
|
import dev.inmo.micro_utils.repos.UpdatedValuePair
|
||||||
import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo
|
import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo
|
||||||
|
@@ -3,12 +3,10 @@ package dev.inmo.micro_utils.repos.ktor.client.key_value
|
|||||||
import dev.inmo.micro_utils.ktor.client.encodeUrlQueryValue
|
import dev.inmo.micro_utils.ktor.client.encodeUrlQueryValue
|
||||||
import dev.inmo.micro_utils.ktor.client.uniget
|
import dev.inmo.micro_utils.ktor.client.uniget
|
||||||
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
|
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
|
||||||
import dev.inmo.micro_utils.pagination.Pagination
|
import dev.inmo.micro_utils.pagination.*
|
||||||
import dev.inmo.micro_utils.pagination.PaginationResult
|
|
||||||
import dev.inmo.micro_utils.pagination.asUrlQueryParts
|
|
||||||
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
|
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
|
||||||
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
|
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
|
||||||
import io.ktor.client.*
|
import io.ktor.client.HttpClient
|
||||||
import kotlinx.serialization.KSerializer
|
import kotlinx.serialization.KSerializer
|
||||||
import kotlinx.serialization.builtins.serializer
|
import kotlinx.serialization.builtins.serializer
|
||||||
|
|
||||||
|
@@ -1,9 +1,7 @@
|
|||||||
package dev.inmo.micro_utils.repos.ktor.client.key_value
|
package dev.inmo.micro_utils.repos.ktor.client.key_value
|
||||||
|
|
||||||
import dev.inmo.micro_utils.repos.StandardKeyValueRepo
|
import dev.inmo.micro_utils.repos.*
|
||||||
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
|
import io.ktor.client.HttpClient
|
||||||
import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo
|
|
||||||
import io.ktor.client.*
|
|
||||||
import kotlinx.serialization.KSerializer
|
import kotlinx.serialization.KSerializer
|
||||||
|
|
||||||
class KtorStandartKeyValueRepo<K, V> (
|
class KtorStandartKeyValueRepo<K, V> (
|
||||||
|
@@ -1,16 +1,13 @@
|
|||||||
package dev.inmo.micro_utils.repos.ktor.client.key_value
|
package dev.inmo.micro_utils.repos.ktor.client.key_value
|
||||||
|
|
||||||
import dev.inmo.micro_utils.ktor.client.BodyPair
|
import dev.inmo.micro_utils.ktor.client.*
|
||||||
import dev.inmo.micro_utils.ktor.client.createStandardWebsocketFlow
|
|
||||||
import dev.inmo.micro_utils.ktor.client.unipost
|
|
||||||
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
|
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
|
||||||
import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo
|
import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo
|
||||||
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
|
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
|
||||||
import io.ktor.client.*
|
import io.ktor.client.HttpClient
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.serialization.KSerializer
|
import kotlinx.serialization.KSerializer
|
||||||
import kotlinx.serialization.builtins.PairSerializer
|
import kotlinx.serialization.builtins.*
|
||||||
import kotlinx.serialization.builtins.serializer
|
|
||||||
|
|
||||||
class KtorWriteStandardKeyValueRepo<K, V> (
|
class KtorWriteStandardKeyValueRepo<K, V> (
|
||||||
private var baseUrl: String,
|
private var baseUrl: String,
|
||||||
@@ -18,6 +15,8 @@ class KtorWriteStandardKeyValueRepo<K, V> (
|
|||||||
private var keySerializer: KSerializer<K>,
|
private var keySerializer: KSerializer<K>,
|
||||||
private var valueSerializer: KSerializer<V>,
|
private var valueSerializer: KSerializer<V>,
|
||||||
) : WriteStandardKeyValueRepo<K, V> {
|
) : WriteStandardKeyValueRepo<K, V> {
|
||||||
|
private val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer)
|
||||||
|
private val keysListSerializer = ListSerializer(keySerializer)
|
||||||
override val onNewValue: Flow<Pair<K, V>> = client.createStandardWebsocketFlow(
|
override val onNewValue: Flow<Pair<K, V>> = client.createStandardWebsocketFlow(
|
||||||
buildStandardUrl(baseUrl, onNewValueRoute),
|
buildStandardUrl(baseUrl, onNewValueRoute),
|
||||||
deserializer = PairSerializer(keySerializer, valueSerializer)
|
deserializer = PairSerializer(keySerializer, valueSerializer)
|
||||||
@@ -28,23 +27,25 @@ class KtorWriteStandardKeyValueRepo<K, V> (
|
|||||||
deserializer = keySerializer
|
deserializer = keySerializer
|
||||||
)
|
)
|
||||||
|
|
||||||
override suspend fun set(k: K, v: V) = client.unipost(
|
override suspend fun set(toSet: Map<K, V>) = client.unipost(
|
||||||
buildStandardUrl(
|
buildStandardUrl(
|
||||||
baseUrl,
|
baseUrl,
|
||||||
setRoute
|
setRoute
|
||||||
),
|
),
|
||||||
BodyPair(KeyValuePostObject.serializer(keySerializer, valueSerializer), KeyValuePostObject(k, v)),
|
BodyPair(keyValueMapSerializer, toSet),
|
||||||
Unit.serializer()
|
Unit.serializer()
|
||||||
)
|
)
|
||||||
|
override suspend fun set(k: K, v: V) = set(mapOf(k to v))
|
||||||
|
|
||||||
override suspend fun unset(k: K) = client.unipost(
|
override suspend fun unset(toUnset: List<K>) = client.unipost(
|
||||||
buildStandardUrl(
|
buildStandardUrl(
|
||||||
baseUrl,
|
baseUrl,
|
||||||
unsetRoute,
|
unsetRoute,
|
||||||
),
|
),
|
||||||
BodyPair(keySerializer, k),
|
BodyPair(keysListSerializer, toUnset),
|
||||||
Unit.serializer()
|
Unit.serializer()
|
||||||
)
|
)
|
||||||
|
override suspend fun unset(k: K) = unset(listOf(k))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated("Renamed", ReplaceWith("KtorWriteStandardKeyValueRepo", "dev.inmo.micro_utils.repos.ktor.client.key_value.KtorWriteStandardKeyValueRepo"))
|
@Deprecated("Renamed", ReplaceWith("KtorWriteStandardKeyValueRepo", "dev.inmo.micro_utils.repos.ktor.client.key_value.KtorWriteStandardKeyValueRepo"))
|
||||||
|
@@ -1,8 +1,6 @@
|
|||||||
package dev.inmo.micro_utils.repos.ktor.client.one_to_many
|
package dev.inmo.micro_utils.repos.ktor.client.one_to_many
|
||||||
|
|
||||||
import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo
|
import dev.inmo.micro_utils.repos.*
|
||||||
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
|
|
||||||
import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo
|
|
||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import kotlinx.serialization.KSerializer
|
import kotlinx.serialization.KSerializer
|
||||||
|
|
||||||
|
@@ -3,9 +3,7 @@ package dev.inmo.micro_utils.repos.ktor.client.one_to_many
|
|||||||
import dev.inmo.micro_utils.ktor.client.encodeUrlQueryValue
|
import dev.inmo.micro_utils.ktor.client.encodeUrlQueryValue
|
||||||
import dev.inmo.micro_utils.ktor.client.uniget
|
import dev.inmo.micro_utils.ktor.client.uniget
|
||||||
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
|
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
|
||||||
import dev.inmo.micro_utils.pagination.Pagination
|
import dev.inmo.micro_utils.pagination.*
|
||||||
import dev.inmo.micro_utils.pagination.PaginationResult
|
|
||||||
import dev.inmo.micro_utils.pagination.asUrlQueryParts
|
|
||||||
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
|
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
|
||||||
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
|
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
|
||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
|
@@ -1,16 +1,13 @@
|
|||||||
package dev.inmo.micro_utils.repos.ktor.client.one_to_many
|
package dev.inmo.micro_utils.repos.ktor.client.one_to_many
|
||||||
|
|
||||||
import dev.inmo.micro_utils.coroutines.broadcastStateFlow
|
|
||||||
import dev.inmo.micro_utils.ktor.client.*
|
import dev.inmo.micro_utils.ktor.client.*
|
||||||
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
|
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
|
||||||
import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo
|
import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo
|
||||||
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
|
import dev.inmo.micro_utils.repos.ktor.common.one_to_many.*
|
||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.serialization.KSerializer
|
import kotlinx.serialization.KSerializer
|
||||||
import kotlinx.serialization.builtins.PairSerializer
|
import kotlinx.serialization.builtins.*
|
||||||
import kotlinx.serialization.builtins.serializer
|
|
||||||
|
|
||||||
class KtorWriteOneToManyKeyValueRepo<Key, Value> (
|
class KtorWriteOneToManyKeyValueRepo<Key, Value> (
|
||||||
private val baseUrl: String,
|
private val baseUrl: String,
|
||||||
@@ -19,6 +16,7 @@ class KtorWriteOneToManyKeyValueRepo<Key, Value> (
|
|||||||
private val valueSerializer: KSerializer<Value>
|
private val valueSerializer: KSerializer<Value>
|
||||||
) : WriteOneToManyKeyValueRepo<Key, Value> {
|
) : WriteOneToManyKeyValueRepo<Key, Value> {
|
||||||
private val keyValueSerializer = PairSerializer(keySerializer, valueSerializer)
|
private val keyValueSerializer = PairSerializer(keySerializer, valueSerializer)
|
||||||
|
private val keyValueMapSerializer = MapSerializer(keySerializer, ListSerializer(valueSerializer))
|
||||||
override val onNewValue: Flow<Pair<Key, Value>> = client.createStandardWebsocketFlow(
|
override val onNewValue: Flow<Pair<Key, Value>> = client.createStandardWebsocketFlow(
|
||||||
buildStandardUrl(baseUrl, onNewValueRoute),
|
buildStandardUrl(baseUrl, onNewValueRoute),
|
||||||
deserializer = keyValueSerializer
|
deserializer = keyValueSerializer
|
||||||
@@ -41,15 +39,23 @@ class KtorWriteOneToManyKeyValueRepo<Key, Value> (
|
|||||||
Unit.serializer(),
|
Unit.serializer(),
|
||||||
)
|
)
|
||||||
|
|
||||||
override suspend fun remove(k: Key, v: Value) = client.unipost(
|
override suspend fun remove(toRemove: Map<Key, List<Value>>) = client.unipost(
|
||||||
buildStandardUrl(
|
buildStandardUrl(
|
||||||
baseUrl,
|
baseUrl,
|
||||||
removeRoute,
|
removeRoute,
|
||||||
),
|
),
|
||||||
BodyPair(keyValueSerializer, k to v),
|
BodyPair(keyValueMapSerializer, toRemove),
|
||||||
Unit.serializer(),
|
Unit.serializer(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
override suspend fun add(toAdd: Map<Key, List<Value>>) = client.unipost(
|
||||||
|
buildStandardUrl(
|
||||||
|
baseUrl,
|
||||||
|
clearRoute,
|
||||||
|
),
|
||||||
|
BodyPair(keyValueMapSerializer, toAdd),
|
||||||
|
Unit.serializer(),
|
||||||
|
)
|
||||||
override suspend fun clear(k: Key) = client.unipost(
|
override suspend fun clear(k: Key) = client.unipost(
|
||||||
buildStandardUrl(
|
buildStandardUrl(
|
||||||
baseUrl,
|
baseUrl,
|
||||||
|
@@ -1,8 +1,6 @@
|
|||||||
package dev.inmo.micro_utils.repos.ktor.server.crud
|
package dev.inmo.micro_utils.repos.ktor.server.crud
|
||||||
|
|
||||||
import dev.inmo.micro_utils.ktor.server.includeWebsocketHandling
|
import dev.inmo.micro_utils.ktor.server.*
|
||||||
import dev.inmo.micro_utils.ktor.server.unianswer
|
|
||||||
import dev.inmo.micro_utils.ktor.server.uniload
|
|
||||||
import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo
|
import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo
|
||||||
import dev.inmo.micro_utils.repos.ktor.common.crud.*
|
import dev.inmo.micro_utils.repos.ktor.common.crud.*
|
||||||
import io.ktor.application.call
|
import io.ktor.application.call
|
||||||
|
@@ -1,7 +1,8 @@
|
|||||||
package dev.inmo.micro_utils.repos.ktor.server.key_value
|
package dev.inmo.micro_utils.repos.ktor.server.key_value
|
||||||
|
|
||||||
import dev.inmo.micro_utils.repos.StandardKeyValueRepo
|
import dev.inmo.micro_utils.repos.StandardKeyValueRepo
|
||||||
import io.ktor.routing.*
|
import io.ktor.routing.Route
|
||||||
|
import io.ktor.routing.route
|
||||||
import kotlinx.serialization.KSerializer
|
import kotlinx.serialization.KSerializer
|
||||||
|
|
||||||
fun <K, V> Route.configureStandartKeyValueRepoRoutes(
|
fun <K, V> Route.configureStandartKeyValueRepoRoutes(
|
||||||
|
@@ -6,8 +6,9 @@ import dev.inmo.micro_utils.pagination.PaginationResult
|
|||||||
import dev.inmo.micro_utils.pagination.extractPagination
|
import dev.inmo.micro_utils.pagination.extractPagination
|
||||||
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
|
import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo
|
||||||
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
|
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
|
||||||
import io.ktor.application.*
|
import io.ktor.application.call
|
||||||
import io.ktor.routing.*
|
import io.ktor.routing.Route
|
||||||
|
import io.ktor.routing.get
|
||||||
import kotlinx.serialization.KSerializer
|
import kotlinx.serialization.KSerializer
|
||||||
import kotlinx.serialization.builtins.serializer
|
import kotlinx.serialization.builtins.serializer
|
||||||
|
|
||||||
|
@@ -1,19 +1,21 @@
|
|||||||
package dev.inmo.micro_utils.repos.ktor.server.key_value
|
package dev.inmo.micro_utils.repos.ktor.server.key_value
|
||||||
|
|
||||||
import dev.inmo.micro_utils.ktor.server.includeWebsocketHandling
|
import dev.inmo.micro_utils.ktor.server.*
|
||||||
import dev.inmo.micro_utils.ktor.server.uniload
|
|
||||||
import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo
|
import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo
|
||||||
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
|
import dev.inmo.micro_utils.repos.ktor.common.key_value.*
|
||||||
import io.ktor.application.*
|
import io.ktor.application.call
|
||||||
import io.ktor.routing.*
|
import io.ktor.routing.Route
|
||||||
|
import io.ktor.routing.post
|
||||||
import kotlinx.serialization.KSerializer
|
import kotlinx.serialization.KSerializer
|
||||||
import kotlinx.serialization.builtins.PairSerializer
|
import kotlinx.serialization.builtins.*
|
||||||
|
|
||||||
fun <K, V> Route.configureWriteStandartKeyValueRepoRoutes (
|
fun <K, V> Route.configureWriteStandartKeyValueRepoRoutes (
|
||||||
originalRepo: WriteStandardKeyValueRepo<K, V>,
|
originalRepo: WriteStandardKeyValueRepo<K, V>,
|
||||||
keySerializer: KSerializer<K>,
|
keySerializer: KSerializer<K>,
|
||||||
valueSerializer: KSerializer<V>,
|
valueSerializer: KSerializer<V>,
|
||||||
) {
|
) {
|
||||||
|
val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer)
|
||||||
|
val keysListSerializer = ListSerializer(keySerializer)
|
||||||
includeWebsocketHandling(
|
includeWebsocketHandling(
|
||||||
onNewValueRoute,
|
onNewValueRoute,
|
||||||
originalRepo.onNewValue,
|
originalRepo.onNewValue,
|
||||||
@@ -27,18 +29,16 @@ fun <K, V> Route.configureWriteStandartKeyValueRepoRoutes (
|
|||||||
)
|
)
|
||||||
|
|
||||||
post(setRoute) {
|
post(setRoute) {
|
||||||
val (key, value) = call.uniload(
|
val toSet = call.uniload(
|
||||||
KeyValuePostObject.serializer(keySerializer, valueSerializer)
|
keyValueMapSerializer
|
||||||
)
|
)
|
||||||
|
|
||||||
originalRepo.set(key, value)
|
call.unianswer(Unit.serializer(), originalRepo.set(toSet))
|
||||||
}
|
}
|
||||||
|
|
||||||
post(unsetRoute) {
|
post(unsetRoute) {
|
||||||
val key = call.uniload(
|
val toUnset = call.uniload(keysListSerializer)
|
||||||
keySerializer
|
|
||||||
)
|
|
||||||
|
|
||||||
originalRepo.unset(key)
|
call.unianswer(Unit.serializer(), originalRepo.unset(toUnset))
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,8 +1,6 @@
|
|||||||
package dev.inmo.micro_utils.repos.ktor.server.one_to_many
|
package dev.inmo.micro_utils.repos.ktor.server.one_to_many
|
||||||
|
|
||||||
import dev.inmo.micro_utils.ktor.server.decodeUrlQueryValue
|
import dev.inmo.micro_utils.ktor.server.*
|
||||||
import dev.inmo.micro_utils.ktor.server.decodeUrlQueryValueOrSendError
|
|
||||||
import dev.inmo.micro_utils.ktor.server.unianswer
|
|
||||||
import dev.inmo.micro_utils.pagination.PaginationResult
|
import dev.inmo.micro_utils.pagination.PaginationResult
|
||||||
import dev.inmo.micro_utils.pagination.extractPagination
|
import dev.inmo.micro_utils.pagination.extractPagination
|
||||||
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
|
import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo
|
||||||
|
@@ -7,15 +7,15 @@ import io.ktor.application.call
|
|||||||
import io.ktor.routing.Route
|
import io.ktor.routing.Route
|
||||||
import io.ktor.routing.post
|
import io.ktor.routing.post
|
||||||
import kotlinx.serialization.KSerializer
|
import kotlinx.serialization.KSerializer
|
||||||
import kotlinx.serialization.builtins.PairSerializer
|
import kotlinx.serialization.builtins.*
|
||||||
import kotlinx.serialization.builtins.serializer
|
|
||||||
|
|
||||||
fun <Key, Value> Route.configureOneToManyWriteKeyValueRepoRoutes(
|
fun <Key, Value> Route.configureOneToManyWriteKeyValueRepoRoutes(
|
||||||
originalRepo: WriteOneToManyKeyValueRepo<Key, Value>,
|
originalRepo: WriteOneToManyKeyValueRepo<Key, Value>,
|
||||||
keySerializer: KSerializer<Key>,
|
keySerializer: KSerializer<Key>,
|
||||||
valueSealizer: KSerializer<Value>,
|
valueSerializer: KSerializer<Value>,
|
||||||
) {
|
) {
|
||||||
val keyValueSerializer = PairSerializer(keySerializer, valueSealizer)
|
val keyValueSerializer = PairSerializer(keySerializer, valueSerializer)
|
||||||
|
val keyValueMapSerializer = MapSerializer(keySerializer, ListSerializer(valueSerializer))
|
||||||
|
|
||||||
includeWebsocketHandling(
|
includeWebsocketHandling(
|
||||||
onNewValueRoute,
|
onNewValueRoute,
|
||||||
@@ -34,24 +34,22 @@ fun <Key, Value> Route.configureOneToManyWriteKeyValueRepoRoutes(
|
|||||||
)
|
)
|
||||||
|
|
||||||
post(addRoute) {
|
post(addRoute) {
|
||||||
val obj = call.uniload(
|
val obj = call.uniload(keyValueMapSerializer)
|
||||||
keyValueSerializer
|
|
||||||
)
|
|
||||||
|
|
||||||
call.unianswer(
|
call.unianswer(
|
||||||
Unit.serializer(),
|
Unit.serializer(),
|
||||||
originalRepo.add(obj.first, obj.second)
|
originalRepo.add(obj)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
post(removeRoute) {
|
post(removeRoute) {
|
||||||
val obj = call.uniload(
|
val obj = call.uniload(
|
||||||
keyValueSerializer
|
keyValueMapSerializer
|
||||||
)
|
)
|
||||||
|
|
||||||
call.unianswer(
|
call.unianswer(
|
||||||
Unit.serializer(),
|
Unit.serializer(),
|
||||||
originalRepo.remove(obj.first, obj.second),
|
originalRepo.remove(obj),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -16,7 +16,9 @@ String[] includes = [
|
|||||||
":ktor:server",
|
":ktor:server",
|
||||||
":ktor:common",
|
":ktor:common",
|
||||||
":ktor:client",
|
":ktor:client",
|
||||||
":coroutines"
|
":coroutines",
|
||||||
|
|
||||||
|
":dokka"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user