Skip to content

Commit 54adc1e

Browse files
Operator: doOnTerminate
Like finallyDo but before emitting the terminal state instead of after.
1 parent 27c0956 commit 54adc1e

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

rxjava-core/src/main/java/rx/Observable.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4293,17 +4293,17 @@ public final Observable<T> doOnEach(final Action1<Notification<? super T>> onNot
42934293
Observer<T> observer = new Observer<T>() {
42944294
@Override
42954295
public final void onCompleted() {
4296-
onNotification.call(new Notification<T>());
4296+
onNotification.call(Notification.createOnCompleted());
42974297
}
42984298

42994299
@Override
43004300
public final void onError(Throwable e) {
4301-
onNotification.call(new Notification<T>(e));
4301+
onNotification.call(Notification.createOnError(e));
43024302
}
43034303

43044304
@Override
43054305
public final void onNext(T v) {
4306-
onNotification.call(new Notification<T>(v));
4306+
onNotification.call(Notification.createOnNext(v));
43074307
}
43084308

43094309
};
@@ -4387,6 +4387,39 @@ public final void onNext(T args) {
43874387

43884388
return lift(new OperatorDoOnEach<T>(observer));
43894389
}
4390+
4391+
/**
4392+
* Modifies an Observable so that it invokes an action when it calls {@code onCompleted} or {@code onError} <p>
4393+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/doOnCompleted.png">
4394+
* <p>
4395+
* This differs from {@code finallyDo} in that this happens BEFORE onCompleted/onError are emitted.
4396+
*
4397+
* @param onTerminate
4398+
* the action to invoke when the source Observable calls {@code onCompleted} or {@code onError}
4399+
* @return the source Observable with the side-effecting behavior applied
4400+
* @see <a href="https://github.com/Netflix/RxJava/wiki/Observable-Utility-Operators#wiki-dooncompleted">RxJava Wiki: doOnCompleted()</a>
4401+
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229804.aspx">MSDN: Observable.Do</a>
4402+
*/
4403+
public final Observable<T> doOnTerminate(final Action0 onTerminate) {
4404+
Observer<T> observer = new Observer<T>() {
4405+
@Override
4406+
public final void onCompleted() {
4407+
onTerminate.call();
4408+
}
4409+
4410+
@Override
4411+
public final void onError(Throwable e) {
4412+
onTerminate.call();
4413+
}
4414+
4415+
@Override
4416+
public final void onNext(T args) {
4417+
}
4418+
4419+
};
4420+
4421+
return lift(new OperatorDoOnEach<T>(observer));
4422+
}
43904423

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

0 commit comments

Comments
 (0)