diff --git a/core/src/commonMain/kotlin/app/meetacy/vm/flow/CFlowWrappers.kt b/core/src/commonMain/kotlin/app/meetacy/vm/flow/CFlowWrappers.kt index e319bc2..69bce79 100644 --- a/core/src/commonMain/kotlin/app/meetacy/vm/flow/CFlowWrappers.kt +++ b/core/src/commonMain/kotlin/app/meetacy/vm/flow/CFlowWrappers.kt @@ -1,9 +1,12 @@ +@file:Suppress("FunctionName") + package app.meetacy.vm.flow import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel +import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow @@ -11,45 +14,56 @@ import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach -import kotlin.jvm.JvmName -public class CStateFlow(private val origin: StateFlow) : StateFlow by origin { +public class CStateFlow(private val origin: StateFlow) : StateFlow by origin { public fun subscribe(block: (T) -> Unit): Disposable = flowSubscribe(block) } -public class CSharedFlow(private val origin: SharedFlow) : SharedFlow by origin { +public class CSharedFlow(private val origin: SharedFlow) : + SharedFlow by origin { public fun subscribe(block: (T) -> Unit): Disposable = flowSubscribe(block) } -public class CFlow(private val origin: Flow) : Flow by origin { +public class CFlow(private val origin: Flow) : Flow by origin { public fun subscribe(block: (T) -> Unit): Disposable = flowSubscribe(block) } -public class CMutableStateFlow(private val origin: MutableStateFlow) : +public class CMutableStateFlow(private val origin: MutableStateFlow) : MutableStateFlow by origin { public fun subscribe(block: (T) -> Unit): Disposable = flowSubscribe(block) } -public class CMutableSharedFlow(private val origin: MutableSharedFlow) : +public class CMutableSharedFlow(private val origin: MutableSharedFlow) : MutableSharedFlow by origin { public fun subscribe(block: (T) -> Unit): Disposable = flowSubscribe(block) } -public fun Flow.cFlow(): CFlow = CFlow(this) - -public fun StateFlow.cStateFlow(): CStateFlow = CStateFlow(this) +public fun Flow.cFlow(): CFlow = CFlow(this) -public fun SharedFlow.cSharedFlow(): CSharedFlow = CSharedFlow(this) +public fun StateFlow.cStateFlow(): CStateFlow = CStateFlow(this) -@JvmName("cSharedFlowOptional") -public fun SharedFlow.cSharedFlow(): CSharedFlow = CSharedFlow(this) +public fun SharedFlow.cSharedFlow(): CSharedFlow = CSharedFlow(this) -public fun MutableStateFlow.cMutableStateFlow(): CMutableStateFlow = +public fun MutableStateFlow.cMutableStateFlow(): CMutableStateFlow = CMutableStateFlow(this) -public fun CMutableSharedFlow.cMutableSharedFlow(): CMutableSharedFlow = +public fun MutableSharedFlow.cMutableSharedFlow(): CMutableSharedFlow = CMutableSharedFlow(this) + +public fun CMutableStateFlow(value: T): CMutableStateFlow = + MutableStateFlow(value).cMutableStateFlow() + +public fun CMutableSharedFlow( + replay: Int = 0, + extraBufferCapacity: Int = 0, + onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND +): CMutableSharedFlow = MutableSharedFlow( + replay = replay, + extraBufferCapacity = extraBufferCapacity, + onBufferOverflow = onBufferOverflow +).cMutableSharedFlow() + private fun Flow.flowSubscribe(block: (T) -> Unit): Disposable = CoroutineScope(SupervisorJob() + Dispatchers.Main) .also { onEach(block).launchIn(it) } diff --git a/core/src/commonMain/kotlin/app/meetacy/vm/flow/StateAndReceiveExt.kt b/core/src/commonMain/kotlin/app/meetacy/vm/flow/StateAndReceiveExt.kt index 74178cb..1a57cac 100644 --- a/core/src/commonMain/kotlin/app/meetacy/vm/flow/StateAndReceiveExt.kt +++ b/core/src/commonMain/kotlin/app/meetacy/vm/flow/StateAndReceiveExt.kt @@ -7,10 +7,10 @@ import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.flow.stateIn -public fun Flow.cStateIn( +public fun Flow.cStateIn( scope: CoroutineScope, started: SharingStarted = SharingStarted.Eagerly, initialValue: T ): CStateFlow = stateIn(scope, started, initialValue).cStateFlow() -public fun ReceiveChannel.receiveAsCFlow(): CFlow = receiveAsFlow().cFlow() +public fun ReceiveChannel.receiveAsCFlow(): CFlow = receiveAsFlow().cFlow() diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index b0955d6..c9682ca 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -5,7 +5,7 @@ androidGradle = "7.4.0" androidLifecycleVersion = "2.6.1" kotlinxCoroutines = "1.7.3" -mvm = "0.0.3" +mvm = "0.0.4" [libraries]