
com.darylteo.rx.promises.AbstractPromise Maven / Gradle / Ivy
package com.darylteo.rx.promises;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.operators.SafeObservableSubscription;
import rx.util.functions.Action0;
import rx.util.functions.Action1;
import rx.util.functions.Func0;
import rx.util.functions.Func1;
import rx.util.functions.Function;
public abstract class AbstractPromise extends Observable implements Observer {
public static enum STATE {
PENDING,
FULFILLED,
REJECTED
}
/* Properties */
private AbstractPromise that = this;
private LinkedHashMap> observers;
private STATE state = STATE.PENDING;
private T value = null;
private Throwable reason;
public STATE getState() {
return this.state;
}
public boolean isPending() {
return this.state == STATE.PENDING;
}
public boolean isFulfilled() {
return this.state == STATE.FULFILLED;
}
public boolean isRejected() {
return this.state == STATE.REJECTED;
}
public T getValue() {
return this.value;
}
public Throwable getReason() {
return this.reason;
}
/* Constructor */
public AbstractPromise(final LinkedHashMap> observers) {
super(new Observable.OnSubscribeFunc() {
@Override
public Subscription onSubscribe(Observer super T> observer) {
final rx.operators.SafeObservableSubscription subscription = new
SafeObservableSubscription();
subscription.wrap(new Subscription() {
@Override
public void unsubscribe() {
// on unsubscribe remove it from the map of outbound observers
// to notify
observers.remove(subscription);
}
});
observers.put(subscription, observer);
return subscription;
}
});
this.observers = observers;
}
/* ================== */
/* Main Defer Function */
protected AbstractPromise _then(
final Function onFulfilled,
final Function onRejected,
final Function onFinally)
{
// This is the next promise in the chain.
// The handlers you see below will resolve their values and forward them
// to this promise.
final AbstractPromise deferred = this._create();
// Create the Observer
final Observer observer = new Observer() {
@Override
public void onCompleted() {
this.evaluate();
}
@Override
public void onError(Throwable reason) {
that.reason = reason;
this.evaluate();
}
@Override
public void onNext(T value) {
that.value = value;
}
private void evaluate() {
try {
// onfinally and onFulfilled/onRejected are mutually exclusive
if (onFinally != null) {
evaluateFinally();
return;
}
// No finally block was provided, thus we need to evaluate fulfillment
// or rejection.
// If the appropriate handler is not provided, it is forwarded to the
// next promise
if (that.state == STATE.FULFILLED) {
evaluateFulfilled();
return;
}
if (that.state == STATE.REJECTED) {
evaluateRejected();
return;
}
} catch (Throwable e) {
// On any exception in the handlers above, we should throw the
// exception to the next promise
deferred.reject(e);
}
}
private void evaluateFinally() {
AbstractPromise> result = callFinally();
if (result != null) {
// the finally block returned a promise, so we need to delay
// fulfillment of the next promise until the returned promise is
// fulfilled
((AbstractPromise super Object>) result)._then(
new Action1
© 2015 - 2025 Weber Informatics LLC | Privacy Policy