add a set of tools for JS and Web Compose

This commit is contained in:
2022-03-12 00:55:03 +06:00
parent 8f790360bc
commit 9e84dc5031
12 changed files with 167 additions and 0 deletions

View File

@@ -12,6 +12,7 @@ kotlin {
commonMain {
dependencies {
api libs.kt.coroutines
api project(":micro_utils.coroutines")
}
}
}

View File

@@ -0,0 +1,22 @@
package dev.inmo.micro_utils.coroutines.compose
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
fun <T> Flow<T>.toMutableState(
initial: T,
scope: CoroutineScope
): MutableState<T> {
val state = mutableStateOf(initial)
subscribeSafelyWithoutExceptions(scope) { state.value = it }
return state
}
inline fun <T> StateFlow<T>.toMutableState(
scope: CoroutineScope
): MutableState<T> = toMutableState(value, scope)