I know this breaks a lot of Rx rules, but I really like RxJava-JDBC and so do my teammates. Relational databases are very core to what we do and so is Rx.
However there are some occasions where we do not want to emit as an Observable<ResultSet>
but would rather just have a pull-based Java 8 Stream<ResultSet>
or Kotlin Sequence<ResultSet>
. But we are very accustomed to the RxJava-JDBC library which only returns an Observable<ResultSet>
.
Therefore, I am wondering if there is a way I can turn an Observable<ResultSet>
into a Sequence<ResultSet>
using an extension function, and not do any intermediary collection or toBlocking()
calls. Below is all I have so far but my head is spinning now trying to connect push and pull based systems, and I cannot buffer either as the ResultSet
is stateful with each onNext()
call. Is this an impossible task?
import rx.Observable
import rx.Subscriber
import java.sql.ResultSet
fun Observable<ResultSet>.asSequence() = object: Iterator<ResultSet>, Subscriber<ResultSet>() {
private var isComplete = false
override fun onCompleted() {
isComplete = true
}
override fun onError(e: Throwable?) {
throw UnsupportedOperationException()
}
override fun onNext(rs: ResultSet?) {
throw UnsupportedOperationException()
}
override fun hasNext(): Boolean {
throw UnsupportedOperationException()
}
override fun next(): ResultSet {
throw UnsupportedOperationException()
}
}.asSequence()