-
-
Save mattrob33/779ed08177785494ea2771e0e6be5f35 to your computer and use it in GitHub Desktop.
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, K> StateFlow<T>.mapState( | |
transform: (data: T) -> K | |
): StateFlow<K> { | |
return mapLatest { | |
transform(it) | |
} | |
.stateIn(viewModelScope, SharingStarted.Eagerly, transform(value)) | |
} | |
fun <T, K> StateFlow<T>.mapState( | |
initialValue: K, | |
transform: suspend (data: T) -> K | |
): StateFlow<K> { | |
return mapLatest { | |
transform(it) | |
} | |
.stateIn(viewModelScope, SharingStarted.Eagerly, initialValue) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment