several more addings

This commit is contained in:
InsanusMokrassar 2020-09-26 22:19:20 +06:00
parent dd3b9f3674
commit c7d0f7856c
6 changed files with 84 additions and 0 deletions

6
common/build.gradle Normal file
View File

@ -0,0 +1,6 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
}
apply from: "$mppProjectWithSerializationPresetPath"

View File

@ -0,0 +1,10 @@
package dev.inmo.micro_utils.common
fun <T> Iterable<T>.syncWith(
other: Iterable<T>,
removed: (List<T>) -> Unit = {},
added: (List<T>) -> Unit = {}
) {
removed(filter { it !in other })
added(other.filter { it !in this })
}

View File

@ -0,0 +1,27 @@
package dev.inmo.micro_utils.common
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ByteArraySerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
typealias ByteArrayAllocator = () -> ByteArray
val ByteArray.asAllocator: ByteArrayAllocator
get() = { this }
object ByteArrayAllocatorSerializer : KSerializer<ByteArrayAllocator> {
private val realSerializer = ByteArraySerializer()
override val descriptor: SerialDescriptor = realSerializer.descriptor
override fun deserialize(decoder: Decoder): ByteArrayAllocator {
val bytes = realSerializer.deserialize(decoder)
return { bytes }
}
override fun serialize(encoder: Encoder, value: ByteArrayAllocator) {
realSerializer.serialize(encoder, value())
}
}

View File

@ -0,0 +1,33 @@
package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.*
class BroadcastStateFlow<T> internal constructor(
parentFlow: Flow<T>,
private val stateGetter: () -> T
) : StateFlow<T>, Flow<T> by parentFlow {
override val value: T
get() = stateGetter()
}
fun <T> BroadcastChannel<T>.asStateFlow(value: T, scope: CoroutineScope): StateFlow<T> = asFlow().let {
var state: T = value
it.onEach { state = it }.launchIn(scope)
BroadcastStateFlow(it) {
state
}
}
fun <T> BroadcastChannel<T?>.asStateFlow(scope: CoroutineScope): StateFlow<T?> = asStateFlow(null, scope)
fun <T> broadcastStateFlow(initial: T, scope: CoroutineScope, channelSize: Int = Channel.BUFFERED) = BroadcastChannel<T>(
channelSize
).let {
it to it.asStateFlow(initial, scope)
}
fun <T> broadcastStateFlow(scope: CoroutineScope, channelSize: Int = Channel.BUFFERED) = broadcastStateFlow<T?>(null, scope, channelSize)

View File

@ -0,0 +1,7 @@
package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Deferred
val <T> T.asDeferred: Deferred<T>
get() = CompletableDeferred(this)

View File

@ -1,6 +1,7 @@
rootProject.name='micro_utils'
String[] includes = [
":common",
":pagination:common",
":pagination:exposed",
":pagination:ktor:common",