Created
December 14, 2022 09:29
-
-
Save aartikov/460a6ee7fa2e6e69c0a8f04d5d1e1fe0 to your computer and use it in GitHub Desktop.
Converts Decompose Value to StateFlow
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
fun <T : Any> Value<T>.toStateFlow(lifecycle: Lifecycle): StateFlow<T> { | |
val state = MutableStateFlow(this.value) | |
if (lifecycle.state != Lifecycle.State.DESTROYED) { | |
val observer = { value: T -> state.value = value } | |
subscribe(observer) | |
lifecycle.doOnDestroy { | |
unsubscribe(observer) | |
} | |
} | |
return state | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment