-
Notifications
You must be signed in to change notification settings - Fork 7.6k
2.x: Add materialize() and dematerialize() #6278
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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/io/reactivex/internal/operators/completable/CompletableMaterialize.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Copyright (c) 2016-present, RxJava Contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is | ||
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See | ||
* the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.reactivex.internal.operators.completable; | ||
|
||
import io.reactivex.*; | ||
import io.reactivex.annotations.Experimental; | ||
import io.reactivex.internal.operators.mixed.MaterializeSingleObserver; | ||
|
||
/** | ||
* Turn the signal types of a Completable source into a single Notification of | ||
* equal kind. | ||
* | ||
* @param <T> the element type of the source | ||
* @since 2.2.4 - experimental | ||
*/ | ||
@Experimental | ||
public final class CompletableMaterialize<T> extends Single<Notification<T>> { | ||
|
||
final Completable source; | ||
|
||
public CompletableMaterialize(Completable source) { | ||
this.source = source; | ||
} | ||
|
||
@Override | ||
protected void subscribeActual(SingleObserver<? super Notification<T>> observer) { | ||
source.subscribe(new MaterializeSingleObserver<T>(observer)); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/io/reactivex/internal/operators/maybe/MaybeMaterialize.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Copyright (c) 2016-present, RxJava Contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is | ||
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See | ||
* the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.reactivex.internal.operators.maybe; | ||
|
||
import io.reactivex.*; | ||
import io.reactivex.annotations.Experimental; | ||
import io.reactivex.internal.operators.mixed.MaterializeSingleObserver; | ||
|
||
/** | ||
* Turn the signal types of a Maybe source into a single Notification of | ||
* equal kind. | ||
* | ||
* @param <T> the element type of the source | ||
* @since 2.2.4 - experimental | ||
*/ | ||
@Experimental | ||
public final class MaybeMaterialize<T> extends Single<Notification<T>> { | ||
|
||
final Maybe<T> source; | ||
|
||
public MaybeMaterialize(Maybe<T> source) { | ||
this.source = source; | ||
} | ||
|
||
@Override | ||
protected void subscribeActual(SingleObserver<? super Notification<T>> observer) { | ||
source.subscribe(new MaterializeSingleObserver<T>(observer)); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/io/reactivex/internal/operators/mixed/MaterializeSingleObserver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Copyright (c) 2016-present, RxJava Contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is | ||
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See | ||
* the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.reactivex.internal.operators.mixed; | ||
|
||
import io.reactivex.*; | ||
import io.reactivex.annotations.Experimental; | ||
import io.reactivex.disposables.Disposable; | ||
import io.reactivex.internal.disposables.DisposableHelper; | ||
|
||
/** | ||
* A consumer that implements the consumer types of Maybe, Single and Completable | ||
* and turns their signals into Notifications for a SingleObserver. | ||
* @param <T> the element type of the source | ||
* @since 2.2.4 - experimental | ||
*/ | ||
@Experimental | ||
public final class MaterializeSingleObserver<T> | ||
implements SingleObserver<T>, MaybeObserver<T>, CompletableObserver, Disposable { | ||
|
||
final SingleObserver<? super Notification<T>> downstream; | ||
|
||
Disposable upstream; | ||
|
||
public MaterializeSingleObserver(SingleObserver<? super Notification<T>> downstream) { | ||
this.downstream = downstream; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Disposable d) { | ||
if (DisposableHelper.validate(upstream, d)) { | ||
this.upstream = d; | ||
downstream.onSubscribe(this); | ||
} | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
downstream.onSuccess(Notification.<T>createOnComplete()); | ||
} | ||
|
||
@Override | ||
public void onSuccess(T t) { | ||
downstream.onSuccess(Notification.<T>createOnNext(t)); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
downstream.onSuccess(Notification.<T>createOnError(e)); | ||
} | ||
|
||
@Override | ||
public boolean isDisposed() { | ||
return upstream.isDisposed(); | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
upstream.dispose(); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/main/java/io/reactivex/internal/operators/single/SingleDematerialize.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* Copyright (c) 2016-present, RxJava Contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is | ||
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See | ||
* the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.reactivex.internal.operators.single; | ||
|
||
import io.reactivex.*; | ||
import io.reactivex.annotations.Experimental; | ||
import io.reactivex.disposables.Disposable; | ||
import io.reactivex.internal.disposables.DisposableHelper; | ||
|
||
/** | ||
* Maps the Notification success value of the source back to normal | ||
* onXXX method call as a Maybe. | ||
* @param <T> the element type of the notification and result | ||
* @since 2.2.4 - experimental | ||
*/ | ||
@Experimental | ||
public final class SingleDematerialize<T> extends Maybe<T> { | ||
|
||
final Single<Object> source; | ||
|
||
public SingleDematerialize(Single<Object> source) { | ||
this.source = source; | ||
} | ||
|
||
@Override | ||
protected void subscribeActual(MaybeObserver<? super T> observer) { | ||
source.subscribe(new DematerializeObserver<T>(observer)); | ||
} | ||
|
||
static final class DematerializeObserver<T> implements SingleObserver<Object>, Disposable { | ||
|
||
final MaybeObserver<? super T> downstream; | ||
|
||
Disposable upstream; | ||
|
||
DematerializeObserver(MaybeObserver<? super T> downstream) { | ||
this.downstream = downstream; | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
upstream.dispose(); | ||
} | ||
|
||
@Override | ||
public boolean isDisposed() { | ||
return upstream.isDisposed(); | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Disposable d) { | ||
if (DisposableHelper.validate(upstream, d)) { | ||
upstream = d; | ||
downstream.onSubscribe(this); | ||
} | ||
} | ||
|
||
@Override | ||
public void onSuccess(Object t) { | ||
if (t instanceof Notification) { | ||
@SuppressWarnings("unchecked") | ||
Notification<T> notification = (Notification<T>)t; | ||
if (notification.isOnNext()) { | ||
downstream.onSuccess(notification.getValue()); | ||
} else if (notification.isOnComplete()) { | ||
downstream.onComplete(); | ||
} else { | ||
downstream.onError(notification.getError()); | ||
} | ||
} else { | ||
downstream.onError(new ClassCastException("io.reactivex.Notification expected but got " + t.getClass())); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
downstream.onError(e); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this API very awkward since it's available on every type but should only be used on
Single<Notification<T>>
. I don't have a better suggestion though. Except defining it yourself and using a Kotlin extension function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been the workaround for
Observable.dematerialize
too.The alternative is to have the user select the notification:
Maybe<R> dematerialize(Function<T, Notification<R>> selector);
thus if you have a
Single<Notification<T>>
, you only need an identity selector:However, this selector pattern is not an established one in RxJava right now. We could go for this but then the other existing
dematerialize
operators should also get a selector overload.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't know about
dematerialize
, then I guess we stick to that convention. Providing a mapper sounds good. Then maybe we could deprecate the currentdematerialize
in Observable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay then. I'll change this to use the selector and post a separate PR for adding a selector variant to
Observable
/Flowable
and deprecate the type-fragile versions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good!