Skip to content

1.x: Add Single.toCompletable() #3866

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
Apr 19, 2016
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: 25 additions & 0 deletions src/main/java/rx/Single.java
Original file line number Diff line number Diff line change
Expand Up @@ -2095,6 +2095,31 @@ public final Observable<T> toObservable() {
return asObservable(this);
}

/**
* Returns a {@link Completable} that discards result of the {@link Single} (similar to
* {@link Observable#ignoreElements()}) and calls {@code onCompleted} when this source {@link Single} calls
* {@code onSuccess}. Error terminal event is propagated.
* <p>
* <img width="640" height="295" src=
* "https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Completable.toCompletable.png"
* alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code toCompletable} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @return a {@link Completable} that calls {@code onCompleted} on it's subscriber when the source {@link Single}
* calls {@code onSuccess}.
* @see <a href="http://reactivex.io/documentation/completable.html">ReactiveX documentation:
* Completable</a>.
* @since (if this graduates from Experimental/Beta to supported, replace this parenthetical
* with the release number).
*/
@Experimental
public final Completable toCompletable() {
return Completable.fromSingle(this);
}

/**
* Returns a Single that mirrors the source Single but applies a timeout policy for its emitted item. If it
* is not emitted within the specified timeout duration, the resulting Single terminates and notifies
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/rx/SingleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,29 @@ public void testToObservable() {
ts.assertCompleted();
}

@Test
public void toCompletableSuccess() {
Completable completable = Single.just("value").toCompletable();
TestSubscriber<Object> testSubscriber = new TestSubscriber<Object>();
completable.subscribe(testSubscriber);

testSubscriber.assertCompleted();
testSubscriber.assertNoValues();
testSubscriber.assertNoErrors();
}

@Test
public void toCompletableError() {
TestException exception = new TestException();
Completable completable = Single.error(exception).toCompletable();
TestSubscriber<Object> testSubscriber = new TestSubscriber<Object>();
completable.subscribe(testSubscriber);

testSubscriber.assertError(exception);
testSubscriber.assertNoValues();
testSubscriber.assertNotCompleted();
}

@Test
public void doOnErrorShouldNotCallActionIfNoErrorHasOccurred() {
Action1<Throwable> action = mock(Action1.class);
Expand Down