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

1.x: Single.flatMapCompletable #4226

Merged
merged 7 commits into from
Jul 27, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions src/main/java/rx/Single.java
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,25 @@ public final <R> Observable<R> flatMapObservable(Func1<? super T, ? extends Obse
return Observable.merge(asObservable(map(func)));
}

public final Completable flatMapCompletable(final Func1<? super T, ? extends Completable> func) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

javadoc

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please tag this as @Experimental.

return Completable.create(new Completable.CompletableOnSubscribe() {
@Override
public void call(final Completable.CompletableSubscriber completableSubscriber) {
subscribe(new SingleSubscriber<T>() {
@Override
public void onSuccess(final T value) {
func.call(value).subscribe(completableSubscriber);
}

@Override
public void onError(final Throwable error) {
completableSubscriber.onError(error);
}
});
}
});
}

/**
* Returns a Single that applies a specified function to the item emitted by the source Single and
* emits the result of this function application.
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/rx/SingleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2005,4 +2005,32 @@ public Single<Integer> call(Integer v) {
assertFalse("Observers present?!", ps.hasObservers());
}

@Test
public void flatMapCompletableComplete() {
TestSubscriber testSubscriber = new TestSubscriber();

Single.just(1).flatMapCompletable(new Func1<Integer, Completable>() {
@Override
public Completable call(final Integer integer) {
return Completable.complete();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be better to test against more complicated Completable, for example check that some function was invoked

}
}).subscribe(testSubscriber);

testSubscriber.assertCompleted();
}

@Test
public void flatMapCompletableError() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

final RuntimeException error = new RuntimeException("some error");
TestSubscriber testSubscriber = new TestSubscriber();

Single.just(1).flatMapCompletable(new Func1<Integer, Completable>() {
@Override
public Completable call(final Integer integer) {
return Completable.error(error);
}
}).subscribe(testSubscriber);

testSubscriber.assertError(error);
}
}