diff --git a/common/build.gradle b/common/build.gradle new file mode 100644 index 00000000000..9597bd30a20 --- /dev/null +++ b/common/build.gradle @@ -0,0 +1,6 @@ +plugins { + id "org.jetbrains.kotlin.multiplatform" + id "org.jetbrains.kotlin.plugin.serialization" +} + +apply from: "$mppProjectWithSerializationPresetPath" diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt new file mode 100644 index 00000000000..92e0dca214d --- /dev/null +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt @@ -0,0 +1,10 @@ +package dev.inmo.micro_utils.common + +fun Iterable.syncWith( + other: Iterable, + removed: (List) -> Unit = {}, + added: (List) -> Unit = {} +) { + removed(filter { it !in other }) + added(other.filter { it !in this }) +} diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/InputAllocator.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/InputAllocator.kt new file mode 100644 index 00000000000..2fd48505694 --- /dev/null +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/InputAllocator.kt @@ -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 { + 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()) + } +} + diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/BroadcastStateFlow.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/BroadcastStateFlow.kt new file mode 100644 index 00000000000..f8d54aab376 --- /dev/null +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/BroadcastStateFlow.kt @@ -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 internal constructor( + parentFlow: Flow, + private val stateGetter: () -> T +) : StateFlow, Flow by parentFlow { + override val value: T + get() = stateGetter() +} + +fun BroadcastChannel.asStateFlow(value: T, scope: CoroutineScope): StateFlow = asFlow().let { + var state: T = value + it.onEach { state = it }.launchIn(scope) + BroadcastStateFlow(it) { + state + } +} + +fun BroadcastChannel.asStateFlow(scope: CoroutineScope): StateFlow = asStateFlow(null, scope) + +fun broadcastStateFlow(initial: T, scope: CoroutineScope, channelSize: Int = Channel.BUFFERED) = BroadcastChannel( + channelSize +).let { + it to it.asStateFlow(initial, scope) +} + +fun broadcastStateFlow(scope: CoroutineScope, channelSize: Int = Channel.BUFFERED) = broadcastStateFlow(null, scope, channelSize) + diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/DataAsDeferred.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/DataAsDeferred.kt new file mode 100644 index 00000000000..e196df51555 --- /dev/null +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/DataAsDeferred.kt @@ -0,0 +1,7 @@ +package dev.inmo.micro_utils.coroutines + +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.Deferred + +val T.asDeferred: Deferred + get() = CompletableDeferred(this) diff --git a/settings.gradle b/settings.gradle index c7baba74fd2..9acb121ff06 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,6 +1,7 @@ rootProject.name='micro_utils' String[] includes = [ + ":common", ":pagination:common", ":pagination:exposed", ":pagination:ktor:common",