Skip to content

Commit

Permalink
feat: generic rework
Browse files Browse the repository at this point in the history
  • Loading branch information
kramlex committed Sep 15, 2023
1 parent f7720fa commit 91fa922
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
42 changes: 28 additions & 14 deletions core/src/commonMain/kotlin/app/meetacy/vm/flow/CFlowWrappers.kt
Original file line number Diff line number Diff line change
@@ -1,55 +1,69 @@
@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
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<out T : Any>(private val origin: StateFlow<T>) : StateFlow<T> by origin {
public class CStateFlow<out T>(private val origin: StateFlow<T>) : StateFlow<T> by origin {
public fun subscribe(block: (T) -> Unit): Disposable = flowSubscribe(block)
}

public class CSharedFlow<out T : Any?>(private val origin: SharedFlow<T>) : SharedFlow<T> by origin {
public class CSharedFlow<out T>(private val origin: SharedFlow<T>) :
SharedFlow<T> by origin {
public fun subscribe(block: (T) -> Unit): Disposable = flowSubscribe(block)
}

public class CFlow<out T : Any>(private val origin: Flow<T>) : Flow<T> by origin {
public class CFlow<out T>(private val origin: Flow<T>) : Flow<T> by origin {
public fun subscribe(block: (T) -> Unit): Disposable = flowSubscribe(block)
}

public class CMutableStateFlow<T : Any>(private val origin: MutableStateFlow<T>) :
public class CMutableStateFlow<T>(private val origin: MutableStateFlow<T>) :
MutableStateFlow<T> by origin {
public fun subscribe(block: (T) -> Unit): Disposable = flowSubscribe(block)
}

public class CMutableSharedFlow<T : Any>(private val origin: MutableSharedFlow<T>) :
public class CMutableSharedFlow<T>(private val origin: MutableSharedFlow<T>) :
MutableSharedFlow<T> by origin {
public fun subscribe(block: (T) -> Unit): Disposable = flowSubscribe(block)
}

public fun <T : Any> Flow<T>.cFlow(): CFlow<T> = CFlow(this)

public fun <T : Any> StateFlow<T>.cStateFlow(): CStateFlow<T> = CStateFlow(this)
public fun <T> Flow<T>.cFlow(): CFlow<T> = CFlow(this)

public fun <T : Any> SharedFlow<T>.cSharedFlow(): CSharedFlow<T> = CSharedFlow(this)
public fun <T> StateFlow<T>.cStateFlow(): CStateFlow<T> = CStateFlow(this)

@JvmName("cSharedFlowOptional")
public fun <T : Any?> SharedFlow<T>.cSharedFlow(): CSharedFlow<T?> = CSharedFlow(this)
public fun <T> SharedFlow<T>.cSharedFlow(): CSharedFlow<T> = CSharedFlow(this)

public fun <T : Any> MutableStateFlow<T>.cMutableStateFlow(): CMutableStateFlow<T> =
public fun <T> MutableStateFlow<T>.cMutableStateFlow(): CMutableStateFlow<T> =
CMutableStateFlow(this)

public fun <T : Any> CMutableSharedFlow<T>.cMutableSharedFlow(): CMutableSharedFlow<T> =
public fun <T> MutableSharedFlow<T>.cMutableSharedFlow(): CMutableSharedFlow<T> =
CMutableSharedFlow(this)


public fun <T> CMutableStateFlow(value: T): CMutableStateFlow<T> =
MutableStateFlow(value).cMutableStateFlow()

public fun <T> CMutableSharedFlow(
replay: Int = 0,
extraBufferCapacity: Int = 0,
onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND
): CMutableSharedFlow<T> = MutableSharedFlow<T>(
replay = replay,
extraBufferCapacity = extraBufferCapacity,
onBufferOverflow = onBufferOverflow
).cMutableSharedFlow()

private fun <T> Flow<T>.flowSubscribe(block: (T) -> Unit): Disposable =
CoroutineScope(SupervisorJob() + Dispatchers.Main)
.also { onEach(block).launchIn(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.stateIn

public fun <T : Any> Flow<T>.cStateIn(
public fun <T> Flow<T>.cStateIn(
scope: CoroutineScope,
started: SharingStarted = SharingStarted.Eagerly,
initialValue: T
): CStateFlow<T> = stateIn(scope, started, initialValue).cStateFlow()

public fun <T : Any> ReceiveChannel<T>.receiveAsCFlow(): CFlow<T> = receiveAsFlow().cFlow()
public fun <T> ReceiveChannel<T>.receiveAsCFlow(): CFlow<T> = receiveAsFlow().cFlow()
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down

0 comments on commit 91fa922

Please sign in to comment.