Skip to content

Make sure we keep a Subscriber’s subscription when passed as Observer #2600

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

Closed
wants to merge 1 commit into from
Closed
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
34 changes: 20 additions & 14 deletions src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -7282,24 +7282,30 @@ public final void onNext(T args) {
* @see <a href="http://reactivex.io/documentation/operators/subscribe.html">ReactiveX operators documentation: Subscribe</a>
*/
public final Subscription subscribe(final Observer<? super T> observer) {
return subscribe(new Subscriber<T>() {
if (observer instanceof Subscriber) {
// this will ensure that we don't drop the Subscriber's subscription from the chain,
// as it would otherwise be hidden by the wrapping Subscriber
return subscribe((Subscriber) observer);
} else {
return subscribe(new Subscriber<T>() {

@Override
public void onCompleted() {
observer.onCompleted();
}
@Override
public void onCompleted() {
observer.onCompleted();
}

@Override
public void onError(Throwable e) {
observer.onError(e);
}
@Override
public void onError(Throwable e) {
observer.onError(e);
}

@Override
public void onNext(T t) {
observer.onNext(t);
}
@Override
public void onNext(T t) {
observer.onNext(t);
}

});
});
}
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/rx/ObservableTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1116,4 +1116,12 @@ public void testErrorThrownIssue1685() {
System.out.println("Done");
}

@Test // cf. https://github.com/ReactiveX/RxJava/issues/2599
public void testSubscribingSubscriberAsObserverMaintainsSubscriptionChain() {
TestSubscriber subscriber = new TestSubscriber();
Subscription subscription = Observable.just("event").subscribe((Observer) subscriber);
subscription.unsubscribe();

subscriber.assertUnsubscribed();
}
}