Skip to content

Implemented Completable#andThen(Observable) #3570

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
Jan 4, 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
15 changes: 15 additions & 0 deletions src/main/java/rx/Completable.java
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,21 @@ public final Completable compose(CompletableTransformer transformer) {
return to(transformer);
}

/**
* Returns an Observable which will subscribe to this Completable and once that is completed then
* will subscribe to the {@code next} Observable. An error event from this Completable will be
* propagated to the downstream subscriber and will result in skipping the subscription of the
* Observable.
*
* @param next the Observable to subscribe after this Completable is completed, not null
* @return Observable that composes this Completable and next
* @throws NullPointerException if next is null
*/
public final <T> Observable<T> andThen(Observable<T> next) {
requireNonNull(next);
return next.delaySubscription(toObservable());
Copy link
Member

Choose a reason for hiding this comment

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

Completable already has an endWith overload with the same purpose.

Copy link
Author

Choose a reason for hiding this comment

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

Oh, I missed that.

Copy link
Member

Choose a reason for hiding this comment

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

Is there a verdict on Observable.andThen? Renaming endWith to andThen may serve some consistency purpose among base classes.

Copy link
Author

Choose a reason for hiding this comment

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

One initial motivation for andThen was based on the Option::and_then method in Rust stdlib. It chains through the computation and transforms it's type/contents to the next Option.

Copy link
Contributor

Choose a reason for hiding this comment

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

My first thought was java.util.function.Function a; a.andThen(b). Also endWith doesn't end the composition this would look strange a.endWith(b).endWith(c) vs a.andThen(b).andThen(c).

Copy link
Member

Choose a reason for hiding this comment

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

I think andThen is more idiomatic than endWith, especially for chaining purposes.

Copy link
Author

Choose a reason for hiding this comment

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

I guess that if we are going to rename or change any of the public API on Completable now is the time before our next release. Should we aim to review the operators before next release?

Copy link
Member

Choose a reason for hiding this comment

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

Yes.

}

/**
* Concatenates this Completable with another Completable.
* @param other the other Completable, not null
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/rx/CompletableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.junit.*;

import rx.Completable.*;
import rx.Observable.OnSubscribe;
import rx.exceptions.*;
import rx.functions.*;
import rx.observers.TestSubscriber;
Expand Down Expand Up @@ -357,6 +358,64 @@ public void call(Long v) {
Assert.assertEquals(Arrays.asList(5L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), requested);
}

@Test
public void andThen() {
TestSubscriber<String> ts = new TestSubscriber<String>(0);
Completable.complete().andThen(Observable.just("foo")).subscribe(ts);
ts.requestMore(1);
ts.assertValue("foo");
ts.assertCompleted();
ts.assertNoErrors();
}

@Test
public void andThenNever() {
TestSubscriber<String> ts = new TestSubscriber<String>(0);
Completable.never().andThen(Observable.just("foo")).subscribe(ts);
ts.requestMore(1);
ts.assertNoValues();
ts.assertNoTerminalEvent();
}

@Test
public void andThenError() {
TestSubscriber<String> ts = new TestSubscriber<String>(0);
final AtomicBoolean hasRun = new AtomicBoolean(false);
final Exception e = new Exception();
Completable.create(new CompletableOnSubscribe() {
@Override
public void call(CompletableSubscriber cs) {
cs.onError(e);
}
})
.andThen(Observable.<String>create(new OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> s) {
hasRun.set(true);
s.onNext("foo");
s.onCompleted();
}
}))
.subscribe(ts);
ts.assertNoValues();
ts.assertError(e);
Assert.assertFalse("Should not have subscribed to observable when completable errors", hasRun.get());
}

@Test
public void andThenSubscribeOn() {
TestSubscriber<String> ts = new TestSubscriber<String>(0);
TestScheduler scheduler = new TestScheduler();
Completable.complete().andThen(Observable.just("foo").delay(1, TimeUnit.SECONDS, scheduler)).subscribe(ts);
ts.requestMore(1);
ts.assertNoValues();
ts.assertNoTerminalEvent();
scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
ts.assertValue("foo");
ts.assertCompleted();
ts.assertNoErrors();
}

@Test(expected = NullPointerException.class)
public void createNull() {
Completable.create(null);
Expand Down