Skip to content

Commit

Permalink
Merge pull request #3566 from artem-zinnatullin/observable-doAfterTer…
Browse files Browse the repository at this point in the history
…minate

Deprecate Observable.finallyDo() and add Observable.doAfterTerminate() instead
  • Loading branch information
Aaron Tull committed Dec 9, 2015
2 parents 150e1e8 + 60769b3 commit 99a46a8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
25 changes: 24 additions & 1 deletion src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -5115,9 +5115,32 @@ public final Observable<T> filter(Func1<? super T, Boolean> predicate) {
* {@link Action0}
* @see <a href="http://reactivex.io/documentation/operators/do.html">ReactiveX operators documentation: Do</a>
* @see #doOnTerminate(Action0)
* @deprecated use {@link #doAfterTerminate(Action0)} instead.
*/
@Deprecated
public final Observable<T> finallyDo(Action0 action) {
return lift(new OperatorFinally<T>(action));
return lift(new OperatorDoAfterTerminate<T>(action));
}

/**
* Registers an {@link Action0} to be called when this Observable invokes either
* {@link Observer#onCompleted onCompleted} or {@link Observer#onError onError}.
* <p>
* <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/finallyDo.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code doAfterTerminate} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param action
* an {@link Action0} to be invoked when the source Observable finishes
* @return an Observable that emits the same items as the source Observable, then invokes the
* {@link Action0}
* @see <a href="http://reactivex.io/documentation/operators/do.html">ReactiveX operators documentation: Do</a>
* @see #doOnTerminate(Action0)
*/
public final Observable<T> doAfterTerminate(Action0 action) {
return lift(new OperatorDoAfterTerminate<T>(action));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
*
* @param <T> the value type
*/
public final class OperatorFinally<T> implements Operator<T, T> {
public final class OperatorDoAfterTerminate<T> implements Operator<T, T> {
final Action0 action;

public OperatorFinally(Action0 action) {
public OperatorDoAfterTerminate(Action0 action) {
if (action == null) {
throw new NullPointerException("Action can not be null");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import rx.Observer;
import rx.functions.Action0;

public class OperatorFinallyTest {
public class OperatorDoAfterTerminateTest {

private Action0 aAction0;
private Observer<String> observer;
Expand All @@ -42,24 +42,24 @@ public void before() {
}

private void checkActionCalled(Observable<String> input) {
input.finallyDo(aAction0).subscribe(observer);
input.doAfterTerminate(aAction0).subscribe(observer);
verify(aAction0, times(1)).call();
}

@Test
public void testFinallyCalledOnComplete() {
public void testDoAfterTerminateCalledOnComplete() {
checkActionCalled(Observable.from(new String[] { "1", "2", "3" }));
}

@Test
public void testFinallyCalledOnError() {
public void testDoAfterTerminateCalledOnError() {
checkActionCalled(Observable.<String> error(new RuntimeException("expected")));
}

@Test
public void nullActionShouldBeCheckedInConstructor() {
try {
new OperatorFinally<Object>(null);
new OperatorDoAfterTerminate<Object>(null);
fail();
} catch (NullPointerException expected) {
assertEquals("Action can not be null", expected.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void call(String t1) {
assertTrue(correctThreadName);
}

}).finallyDo(new Action0() {
}).doAfterTerminate(new Action0() {

@Override
public void call() {
Expand Down

0 comments on commit 99a46a8

Please sign in to comment.