Skip to content

Instantly share code, notes, and snippets.

@mattrob33
Last active March 15, 2022 01:11
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/37ccc249fdc52da5e7f041d54dd3fca9 to your computer and use it in GitHub Desktop.
Save mattrob33/37ccc249fdc52da5e7f041d54dd3fca9 to your computer and use it in GitHub Desktop.
fun <T, K> StateFlow<T>.mapState(
scope: CoroutineScope,
transform: (data: T) -> K
): StateFlow<K> {
return mapLatest {
transform(it)
}
.stateIn(scope, SharingStarted.Eagerly, transform(value))
}
fun <T, K> StateFlow<T>.mapState(
scope: CoroutineScope,
initialValue: K,
transform: suspend (data: T) -> K
): StateFlow<K> {
return mapLatest {
transform(it)
}
.stateIn(scope, SharingStarted.Eagerly, initialValue)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment