https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

javad

11/02/2018, 4:48 PM
Is there a library to provide extension functions for RxJava 1 types? Like
Single.await()
? The official lib only supports RxJava 2 😞
j

Jonathan

11/02/2018, 4:48 PM
No support has been droped
RxJava 1 is discontinued for quite a wile already
But it is easy to implement your own
j

javad

11/02/2018, 4:49 PM
do you mind explaining more about implementing one?
or giving some hints
j

Jonathan

11/02/2018, 4:51 PM
You can use
suspendCoroutine
Here's an example:
Copy code
suspend fun <T> Single<T>.await(): T = supendCancellableCoroutine { cont ->
	val disposable = subscribe(
		{ cont.resume(it) }, // onSuccess
		{ cont.resumeWithException(it) } // onError
	)
	
	cont.invokeOnCancellation {
		disposable.dispose()
	}
}
My example is only on conceptual level, and probably doesn't compile
j

javad

11/02/2018, 4:51 PM
thank you very much! appreciate it 🙂
j

Jonathan

11/02/2018, 4:53 PM
Actually my exmple would work with RxJava 2 (I just tried)
👏 1
So you only have to adapt to the RxJava1 API, which I don't know well.
🙇‍♂️ 1
z

Zach Klippenstein (he/him) [MOD]

11/02/2018, 10:07 PM
The Rx2 adapter library actually does a lot of work to manage backpressure etc, you might want to consider just using that library with the Rx1/Rx2 adapter library from David Karnok
g

gildor

11/03/2018, 2:29 PM
You can copy old (dropped) implementation of Rx1 adapter to you project
2 Views