Skip to content

Instantly share code, notes, and snippets.

@Jeff-Soares
Created August 11, 2022 17:18
Show Gist options
  • Save Jeff-Soares/fb87992935f25044169167648d5c17ac to your computer and use it in GitHub Desktop.
Save Jeff-Soares/fb87992935f25044169167648d5c17ac to your computer and use it in GitHub Desktop.
combine operator for livedata
fun <T, K, R> LiveData<T>.combineWith(
liveData: LiveData<K>,
block: (T?, K?) -> R
): LiveData<R> {
val result = MediatorLiveData<R>()
result.addSource(this) {
result.value = block(this.value, liveData.value)
}
result.addSource(liveData) {
result.value = block(this.value, liveData.value)
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment