Bridging the gap between callbacks and RxJava
At times, you may have to mix your RxJava code with a callback-based API. For example, service proxy interfaces can only be defined with callbacks, but the implementation uses the Vert.x Rxified API.
In this case, the io.vertx.reactivex.SingleHelper.toObserver
class can adapt a Handler<AsyncResult<T>>
to an RxJava SingleObserver<T>
:
@Override
public WikiDatabaseService fetchAllPagesData(Handler<AsyncResult<List<JsonObject>>> resultHandler) { (1)
dbClient.rxQuery(sqlQueries.get(SqlQuery.ALL_PAGES_DATA))
.map(ResultSet::getRows)
.subscribe(SingleHelper.toObserver(resultHandler)); (2)
return this;
}
fetchAllPagesData
is an asynchronous service proxy operation, defined with aHandler<AsyncResult<List<JsonObject>>>
callback.The
toObserver
method adapts theresultHandler
to aSingleObserver<List<JsonObject>>
, so that the handler is invoked when the list of rows is emitted.
Note | io.vertx.reactivex.CompletableHelper and io.vertx.reactivex.MaybeHelper also provide adapters for Completable and Maybe . |