Skip to content

Instantly share code, notes, and snippets.

@twyatt
Created November 22, 2017 09:40
Show Gist options
  • Save twyatt/acdfd46f7b173774757cff64a3294d01 to your computer and use it in GitHub Desktop.
Save twyatt/acdfd46f7b173774757cff64a3294d01 to your computer and use it in GitHub Desktop.
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.observers.DisposableObserver
import io.reactivex.schedulers.Schedulers
fun main(args: Array<String>) {
val list = listOf(3, 2, 1)
val list2 = listOf(1f, 7f, 8f)
Observable.fromIterable(list)
.flatMap { value -> longRunningAsync(value.toString()).toObservable() }
.mergeWith(Observable.fromIterable(list2)
.flatMap { value2 -> longRunningAsync(value2.toString()).toObservable() }
)
.subscribe(object : DisposableObserver<String>() {
override fun onComplete() {
println("onComplete")
}
override fun onError(e: Throwable?) {
println("onError $e")
}
override fun onNext(t: String?) {
println("onNext $t")
}
})
Thread.sleep(1_000_000L)
}
fun longRunningAsync(value: String): Single<String> {
return Single.create<String>({
emitter ->
Thread.sleep(value.toFloat().toInt() * 100L)
emitter.onSuccess("Result: $value")
}).subscribeOn(Schedulers.io())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment