All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.rx.java.HandlerAdapter Maven / Gradle / Ivy

package io.vertx.rx.java;

import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;

/**
 * @author Julien Viet
 */
public class HandlerAdapter extends ObservableOnSubscribeAdapter implements Handler> {

  private AsyncResult buffered;
  private boolean subscribed;

  @Override
  public void onSubscribed() {
    AsyncResult result = buffered;
    if (result != null) {
      buffered = null;
      dispatch(result);
    } else {
      subscribed = true;
    }
  }

  @Override
  public void handle(AsyncResult event) {
    if (subscribed) {
      subscribed = false;
      dispatch(event);
    } else {
      this.buffered = event;
    }
  }

  @Override
  protected void onUnsubscribed() {
    subscribed = false;
  }

  protected void dispatch(AsyncResult event) {
    if (event.succeeded()) {
      this.fireNext(event.result());
      this.fireComplete();
    } else {
      this.fireError(event.cause());
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy