if you're using Navi, it becomes even simpler. her...
# coroutines
k
if you're using Navi, it becomes even simpler. here's all the code you need:
Copy code
fun Subscription.autoUnsubscribe(naviComponent: NaviComponent) = apply { naviComponent.addListener(Event.DESTROY) { unsubscribe() } }

fun NaviComponent.asyncRx(coroutine c: RxController.() -> Continuation<Unit>) {
    RxController(this).c().resume(Unit)
}

class RxController internal constructor(
        private val naviComponent: NaviComponent
) {

    suspend fun <V> Observable<V>.awaitFirst(x: Continuation<V>) {
        this.first().subscribeWithContinuation(x)
    }

    private fun <V> Observable<V>.subscribeWithContinuation(x: Continuation<V>) {
        subscribe(x::resume, x::resumeWithException).autoUnsubscribe(naviComponent)
    }
}