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

Deprecate Observable.finallyDo() and add Observable.doAfterTerminate() instead #3566

Merged
Merged
Show file tree
Hide file tree
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
25 changes: 24 additions & 1 deletion src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -5089,9 +5089,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