Skip to content

Add Single.finallyDo() #3434

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

Merged
merged 1 commit into from
Dec 9, 2015
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
22 changes: 22 additions & 0 deletions src/main/java/rx/Single.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import rx.internal.operators.OperatorDelay;
import rx.internal.operators.OperatorDoOnEach;
import rx.internal.operators.OperatorDoOnUnsubscribe;
import rx.internal.operators.OperatorFinally;
import rx.internal.operators.OperatorMap;
import rx.internal.operators.OperatorObserveOn;
import rx.internal.operators.OperatorOnErrorReturn;
Expand Down Expand Up @@ -2020,4 +2021,25 @@ public void call(SingleSubscriber<? super T> singleSubscriber) {
public final Single<T> doOnUnsubscribe(final Action0 action) {
return lift(new OperatorDoOnUnsubscribe<T>(action));
}

/**
* Registers an {@link Action0} to be called when this {@link Single} invokes either
* {@link SingleSubscriber#onSuccess(Object)} onSuccess} or {@link SingleSubscriber#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 {@link Single} finishes.
* @return a {@link Single} that emits the same item or error as the source {@link Single}, then invokes the
* {@link Action0}
* @see <a href="http://reactivex.io/documentation/operators/do.html">ReactiveX operators documentation: Do</a>
*/
@Experimental
public final Single<T> doAfterTerminate(Action0 action) {
return lift(new OperatorFinally<T>(action));
}
}
52 changes: 51 additions & 1 deletion src/test/java/rx/SingleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import rx.schedulers.Schedulers;
import rx.subscriptions.Subscriptions;


public class SingleTest {

@Test
Expand Down Expand Up @@ -891,4 +890,55 @@ public void call(SingleSubscriber<? super Object> singleSubscriber) {
testSubscriber.assertNoValues();
testSubscriber.assertNoTerminalEvent();
}

@Test
public void doAfterTerminateActionShouldBeInvokedAfterOnSuccess() {
Action0 action = mock(Action0.class);

TestSubscriber<String> testSubscriber = new TestSubscriber<String>();

Single
.just("value")
.doAfterTerminate(action)
.subscribe(testSubscriber);

testSubscriber.assertValue("value");
testSubscriber.assertNoErrors();

verify(action).call();
}

@Test
public void doAfterTerminateActionShouldBeInvokedAfterOnError() {
Action0 action = mock(Action0.class);

TestSubscriber<Object> testSubscriber = new TestSubscriber<Object>();

Throwable error = new IllegalStateException();

Single
.error(error)
.doAfterTerminate(action)
.subscribe(testSubscriber);

testSubscriber.assertNoValues();
testSubscriber.assertError(error);

verify(action).call();
}

@Test
public void doAfterTerminateActionShouldNotBeInvokedUntilSubscriberSubscribes() {
Action0 action = mock(Action0.class);

Single
.just("value")
.doAfterTerminate(action);

Single
.error(new IllegalStateException())
.doAfterTerminate(action);

verifyZeroInteractions(action);
}
}