Skip to content

Instantly share code, notes, and snippets.

@mattrob33
Last active March 15, 2022 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattrob33/779ed08177785494ea2771e0e6be5f35 to your computer and use it in GitHub Desktop.
Save mattrob33/779ed08177785494ea2771e0e6be5f35 to your computer and use it in GitHub Desktop.
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