Skip to content

Instantly share code, notes, and snippets.

@shiehnpin
Created August 14, 2020 10:32
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 shiehnpin/ce13e340f64774c044d11c9e8c138f0c to your computer and use it in GitHub Desktop.
Save shiehnpin/ce13e340f64774c044d11c9e8c138f0c to your computer and use it in GitHub Desktop.
fun <T1, T2, R> zip(
source1 : LiveData<T1?>,
source2 : LiveData<T2?>,
source1Name: String = "first",
source2Name: String = "second",
zipFunction: (t1:T1?, t2:T2?, emitter:(R)->Unit) -> Unit
): LiveData<R>{
return object : MediatorLiveData<R>(){
var t1: T1? = null
var t2: T2? = null
var emit:(r: R)-> Unit = { r ->
value = r
}
init {
addSource(source1){
Timber.d("zip by $source1Name:$it")
t1 = it
zipFunction(t1, t2, emit)
}
addSource(source2){
Timber.d("zip by $source2Name:$it")
t2 = it
zipFunction(t1, t2, emit)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment