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

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

The newest version!
package io.vertx.rx.java;

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

/**
 * @author Julien Viet
 */
public class ObservableFuture extends Observable {

  private static 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());
      }
    }
  }

  public ObservableFuture() {
    this(new HandlerAdapter() {
      @Override
      protected void dispatch(AsyncResult event) {
        if (event.succeeded()) {
          this.fireNext(event.result());
          this.fireComplete();
        } else {
          this.fireError(event.cause());
        }
      }
    });
  }

  private HandlerAdapter adapter;

  private ObservableFuture(HandlerAdapter adapter) {
    super(adapter);
    this.adapter = adapter;
  }

  public Handler> toHandler() {
    return adapter;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy