Last active
November 12, 2015 19:30
-
-
Save mandybess/f69b1ce96ae9d4323a49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import rx.Observable.Operator; | |
import rx.Subscriber; | |
import rx.functions.Action0; | |
public final class DoOnEmptyOperator<T> implements Operator<T, T> { | |
private final Action0 empty; | |
public DoOnEmptyOperator(Action0 empty) { | |
this.empty = empty; | |
} | |
@Override | |
public Subscriber<? super T> call(final Subscriber<? super T> child) { | |
return new Subscriber<T>(child) { | |
boolean hasValue; | |
@Override | |
public void onNext(T t) { | |
hasValue = true; | |
child.onNext(t); | |
} | |
@Override | |
public void onError(Throwable e) { | |
child.onError(e); | |
} | |
@Override | |
public void onCompleted() { | |
if (!hasValue) { | |
try { | |
empty.call(); | |
} catch (Throwable e) { | |
child.onError(e); | |
return; | |
} | |
} | |
child.onCompleted(); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment