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

single: add toSingle method to Observable #3049

Merged
merged 1 commit into from
Jun 24, 2015
Merged

single: add toSingle method to Observable #3049

merged 1 commit into from
Jun 24, 2015

Conversation

LalitMaganti
Copy link
Contributor

  • closes Variants of first, single etc. which return Single #3038
  • this method allows an observable which is guaranteed to return exactly one
    item to be converted to a Single
  • NOTE: the semantics of this function are very similar to that of single
  • i.e. errors are passed through, more than one item results in an
    IllegalArgumentException, completion without emission results in a
    NoSuchElementException and exactly one item is passed through the onSuccess
    method of SingleSubscriber

@LalitMaganti
Copy link
Contributor Author

Would appreciate feedback since this is my first contribution to RxJava! :)


@Override
public void call(final SingleSubscriber<? super T> singleSubscriber) {
observable.subscribe(new Subscriber<T>() {
Copy link
Member

Choose a reason for hiding this comment

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

Unsubscription chain is not maintained here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to double check: does this mean I need to add a few checks like
if (singleSubscriber.isUnsubscribed()) {
unsubscribe();
return;
}
in the 4 subscriber methods?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks that helps quite a bit in understanding. However, unlike in your post, I cannot pass the child subscriber to the parent since the child has the SingleSubscriber type which isn't accepted by the Subscriber constructor.

Copy link
Member

Choose a reason for hiding this comment

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

But you can add resources to singleSubscriber.

Copy link
Contributor

Choose a reason for hiding this comment

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

There is also a constructor overload that takes the child Subscriber to automatically chain together unsubscriptions.

Sent from my iPhone

On Jun 24, 2015, at 3:54 AM, Lalit Maganti [email protected] wrote:

In src/main/java/rx/internal/operators/OnSubscribeSingle.java:

+/**

  • * Allows conversion of an Observable to a Single ensuring that exactly one item is emitted - no more and no less.
  • * Also forwards errors as appropriate.
  • */
    +public class OnSubscribeSingle implements Single.OnSubscribe {
    +
  • private final Observable observable;
  • public OnSubscribeSingle(Observable observable) {
  •    this.observable = observable;
    
  • }
  • @OverRide
  • public void call(final SingleSubscriber<? super T> singleSubscriber) {
  •    observable.subscribe(new Subscriber<T>() {
    
    Just to double check: does this mean I need to add a few checks like
    if (singleSubscriber.isUnsubscribed()) {
    unsubscribe();
    return;
    }
    in the 4 subscriber methods?


Reply to this email directly or view it on GitHub.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I say though I can't use that overload since the type of the child is SingleSubscriber not Subscriber.

@akarnokd
Copy link
Member

Yes.

@benjchristensen
Copy link
Member

Looks good. Can you add the license headers to the files please? The block of code to add is shown here: https://github.com/ReactiveX/RxJava/blob/1.x/CONTRIBUTING.md

@LalitMaganti
Copy link
Contributor Author

Have done so.

@benjchristensen
Copy link
Member

Would you mind rebasing to a single commit?

* closes #3038
* this method allows an observable which is guaranteed to return exactly one
  item to be converted to a Single
* NOTE: the semantics of this function are very similar to that of single
* i.e. errors are passed through, more than one item results in an
  IllegalArgumentException, completion without emission results in a
  NoSuchElementException and exactly one item is passed through the onSuccess
  method of SingleSubscriber
@LalitMaganti
Copy link
Contributor Author

Also done :)

benjchristensen added a commit that referenced this pull request Jun 24, 2015
single: add toSingle method to Observable
@benjchristensen benjchristensen merged commit 9549363 into ReactiveX:1.x Jun 24, 2015
@benjchristensen
Copy link
Member

Thank you @tilal6991 for the contribution and responsiveness on changes.

@LalitMaganti
Copy link
Contributor Author

Glad to contribute.

I'm planning to look into more tricky issues soon so hopefully this is not the last you'll see of me :)

}
};
child.add(parent);
observable.subscribe(parent);
Copy link
Collaborator

Choose a reason for hiding this comment

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

@benjchristensen Shouldn't this use unsafeSubscribe?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants