-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from futuredapp/feature/v3-decompose-stateflow
Refactor asStateFlow() extension
- Loading branch information
Showing
7 changed files
with
44 additions
and
41 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
...compose/src/commonMain/kotlin/app/futured/arkitekt/decompose/coroutines/ValueStateFlow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package app.futured.arkitekt.decompose.coroutines | ||
|
||
import com.arkivanov.decompose.value.Value | ||
import kotlinx.coroutines.flow.FlowCollector | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
|
||
/** | ||
* A [StateFlow] implementation that wraps a Decompose Value. | ||
* | ||
* @param T The type of the value. | ||
* @property decomposeValue The Decompose Value to be wrapped. | ||
*/ | ||
internal class ValueStateFlow<out T : Any>( | ||
private val decomposeValue: Value<T>, | ||
) : StateFlow<T> { | ||
|
||
override val value: T | ||
get() = decomposeValue.value | ||
|
||
override val replayCache: List<T> = listOf(value) | ||
|
||
override suspend fun collect(collector: FlowCollector<T>): Nothing { | ||
val relay = MutableStateFlow(decomposeValue.value) | ||
val cancellation = decomposeValue.subscribe { value -> relay.value = value } | ||
|
||
try { | ||
relay.collect(collector) | ||
} finally { | ||
cancellation.cancel() | ||
} | ||
} | ||
} |
36 changes: 3 additions & 33 deletions
36
...t-decompose/src/commonMain/kotlin/app/futured/arkitekt/decompose/ext/DecomposeValueExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,13 @@ | ||
package app.futured.arkitekt.decompose.ext | ||
|
||
import app.futured.arkitekt.decompose.coroutines.ValueStateFlow | ||
import com.arkivanov.decompose.value.Value | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.channels.trySendBlocking | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.callbackFlow | ||
import kotlinx.coroutines.flow.stateIn | ||
|
||
/** | ||
* Converts this Decompose [Value] to Kotlin [Flow]. | ||
* | ||
* @param T The type of the value. | ||
* @return A [Flow] emitting the values of the Decompose [Value]. | ||
*/ | ||
fun <T : Any> Value<T>.asFlow(): Flow<T> = callbackFlow { | ||
val cancellation = subscribe { value -> | ||
trySendBlocking(value) | ||
} | ||
|
||
awaitClose { | ||
cancellation.cancel() | ||
} | ||
} | ||
|
||
/** | ||
* Converts this Decompose [Value] to Kotlin [StateFlow]. | ||
* | ||
* @param T The type of the value. | ||
* @param coroutineScope The [CoroutineScope] in which the [StateFlow] is active. | ||
* @return A [StateFlow] emitting the values of the [Value]. | ||
* @return A [StateFlow] emitting the values of this [Value]. | ||
*/ | ||
fun <T : Any> Value<T>.asStateFlow( | ||
coroutineScope: CoroutineScope, | ||
): StateFlow<T> = asFlow() | ||
.stateIn( | ||
scope = coroutineScope, | ||
started = SharingStarted.Lazily, | ||
initialValue = value, | ||
) | ||
fun <T : Any> Value<T>.asStateFlow(): StateFlow<T> = ValueStateFlow(decomposeValue = this) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,5 +73,5 @@ internal class HomeNavHostComponent( | |
) | ||
} | ||
}, | ||
).asStateFlow(componentCoroutineScope) | ||
).asStateFlow() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters