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>:

  1. @Override
  2. public WikiDatabaseService fetchAllPagesData(Handler<AsyncResult<List<JsonObject>>> resultHandler) { (1)
  3. dbClient.rxQuery(sqlQueries.get(SqlQuery.ALL_PAGES_DATA))
  4. .map(ResultSet::getRows)
  5. .subscribe(SingleHelper.toObserver(resultHandler)); (2)
  6. return this;
  7. }
  1. fetchAllPagesData is an asynchronous service proxy operation, defined with a Handler<AsyncResult<List<JsonObject>>> callback.

  2. The toObserver method adapts the resultHandler to a SingleObserver<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.