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

2.x: Completable class for valueless event composition. #3439

Closed
wants to merge 1 commit into from

Conversation

akarnokd
Copy link
Member

I saw this mentioned in this talk from @benjchristensen

This PR adds a new class Completable which allows composing events
that never want to fire onNext, but only onError or onComplete. It
is built upon the same Reactive-Streams principles as Single,
NbpObservable and Observable.

I've added most methods that made sense and left out the others (such as
map and flatMap) that would require at least a single value.

Depending on whether this type is actually required, I'll add unit test to this PR or in a new PR in case this gets merged.

@akarnokd akarnokd added this to the 2.0 milestone Oct 12, 2015
@akarnokd
Copy link
Member Author

I'm adding unit tests and some fixes, stay tuned.

@akarnokd
Copy link
Member Author

Added 240 unit tests and fixed a few bugs.

@akarnokd
Copy link
Member Author

I've added amb, startWith and endWith methods + their unit tests. The latter two have convenience overload that help with continuation-style flows (run the Completable before or after the Observable/NbpObservable delivered its values).

import io.reactivex.Completable.*;
import io.reactivex.disposables.*;

public final class CompletableOnSubscribeConcatArray implements CompletableOnSubscribe {
Copy link
Contributor

Choose a reason for hiding this comment

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

@akarnokd do you really see much sense in a separate class for arrays?

You can provide an override where passed array will be wrapped into ArrayList and then passed to the CompletableOnSubscribeConcatIterable.

Less code — less bugs :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Less allocation, more performant code.

@ibaca
Copy link

ibaca commented Oct 15, 2015

What is the difference between Completable vs Single<Void>? It's possible to add some utility methods to Single to terminate using null values, so end up almost with the same interface than Completable. Maybe is better to improve Single as something similar to a Deferred/Promise which will handle the 1/0 items situations (been 0 a null item) which is probably the origin of both interfaces. Even Completable might be just a Completable extends Future<Void>. So end up with Observable<T> (async multiple items) and Future<Void> (async single item) as main types (as puts here for some time http://reactivex.io/intro.html 😲).

Hope this helps. Just ignore me if you think that this is non sense. I see that @akarnokd are doing amazing things this days (specially rxjava v2), I'm not try to say that this pull request is bad or something similar.

@akarnokd
Copy link
Member Author

The difference is that, among other things, you don't have to drag around a mute type parameter. Null values are forbidden with 2.x and Reactive-Streams anyway. Future void is not an option because Completable is a deferred cold computation which runs only if one subscribes to it whereas Future is hot and is expected to be running or already completed such as AsyncSubject.

@benjchristensen
Copy link
Member

Consider making Completable implement both Publisher and IObservable (an interface for Observable similar to Publisher for Flowable) so that Completable can be composed directly with either a Flowable or Observable without special method overloads.

@akarnokd
Copy link
Member Author

I've started out with Completable implements Publisher<Object> but it didn't work with Subscriber<T> because T is not a supertype of Object and java doesn't have a bottom type in its type system (its top type is Object, however, Void is not a bottom type so that Void extends every other type). The same would be true for IObservable. So what's left is a bunch of subscribe() method with call-site variance.

@benjchristensen
Copy link
Member

Well that really sucks that Void doesn't work properly. How about as Publisher<?>?

@akarnokd
Copy link
Member Author

You can't have wildcards as the type parameter of the super type.

@benjchristensen
Copy link
Member

:( Oh well.

@NoahAndrews
Copy link

Is there any chance we will get this with RxJava 1.x?

@akarnokd
Copy link
Member Author

The PR for 1.x has already been merged and should appear in 1.1.1

@stealthcode
Copy link

see #3444

@akarnokd
Copy link
Member Author

akarnokd commented Feb 4, 2016

Closing, will be included in the complete backport PR.

@akarnokd akarnokd closed this Feb 4, 2016
@akarnokd akarnokd deleted the Completable2x branch May 18, 2016 22:53
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.

6 participants