Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operator: doOnTerminate #918

Merged
merged 1 commit into from
Feb 21, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -4293,17 +4293,17 @@ public final Observable<T> doOnEach(final Action1<Notification<? super T>> onNot
Observer<T> observer = new Observer<T>() {
@Override
public final void onCompleted() {
onNotification.call(new Notification<T>());
onNotification.call(Notification.createOnCompleted());
}

@Override
public final void onError(Throwable e) {
onNotification.call(new Notification<T>(e));
onNotification.call(Notification.createOnError(e));
}

@Override
public final void onNext(T v) {
onNotification.call(new Notification<T>(v));
onNotification.call(Notification.createOnNext(v));
}

};
Expand Down Expand Up @@ -4387,6 +4387,39 @@ public final void onNext(T args) {

return lift(new OperatorDoOnEach<T>(observer));
}

/**
* Modifies an Observable so that it invokes an action when it calls {@code onCompleted} or {@code onError} <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/doOnCompleted.png">
* <p>
* This differs from {@code finallyDo} in that this happens BEFORE onCompleted/onError are emitted.
*
* @param onTerminate
* the action to invoke when the source Observable calls {@code onCompleted} or {@code onError}
* @return the source Observable with the side-effecting behavior applied
* @see <a href="https://github.com/Netflix/RxJava/wiki/Observable-Utility-Operators#wiki-dooncompleted">RxJava Wiki: doOnCompleted()</a>
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229804.aspx">MSDN: Observable.Do</a>
*/
public final Observable<T> doOnTerminate(final Action0 onTerminate) {
Observer<T> observer = new Observer<T>() {
@Override
public final void onCompleted() {
onTerminate.call();
}

@Override
public final void onError(Throwable e) {
onTerminate.call();
}

@Override
public final void onNext(T args) {
}

};

return lift(new OperatorDoOnEach<T>(observer));
}

/**
* Returns an Observable that emits the single item at a specified index in a sequence of emissions from a
Expand Down