Skip to content

Instantly share code, notes, and snippets.

@hkusu
Created April 18, 2019 15:22
Show Gist options
  • Save hkusu/946e30121f55f863aca491c776abbba0 to your computer and use it in GitHub Desktop.
Save hkusu/946e30121f55f863aca491c776abbba0 to your computer and use it in GitHub Desktop.
suspend function が利用できる map
inline fun <T, U> LiveData<T>.map(
scope: CoroutineScope,
crossinline block: suspend (T) -> U
): LiveData<U> {
val result = MediatorLiveData<U>()
result.addSource(this) {
scope.launch {
result.postValue(block.invoke(it))
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment