-
Notifications
You must be signed in to change notification settings - Fork 7.6k
1.x: Add extend() for Single and Completable. #4419
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
JakeWharton
wants to merge
1
commit into
ReactiveX:1.x
from
JakeWharton:jw/single-completable-extend
Closed
Changes from all commits
Commits
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
34 changes: 34 additions & 0 deletions
34
src/main/java/rx/internal/operators/CompletableOnSubscribeExtend.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,34 @@ | ||
/* | ||
* Copyright 2016 Netflix, Inc. | ||
* | ||
* 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 rx.internal.operators; | ||
|
||
import rx.Completable; | ||
|
||
/** | ||
* Transforms a CompletableOnSubscribe.call() into an Completable.subscribe() call. | ||
*/ | ||
public final class CompletableOnSubscribeExtend implements Completable.CompletableOnSubscribe { | ||
final Completable parent; | ||
|
||
public CompletableOnSubscribeExtend(Completable parent) { | ||
this.parent = parent; | ||
} | ||
|
||
@Override | ||
public void call(Completable.CompletableSubscriber subscriber) { | ||
parent.subscribe(subscriber); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/rx/internal/operators/SingleOnSubscribeExtend.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,35 @@ | ||
/* | ||
* Copyright 2016 Netflix, Inc. | ||
* | ||
* 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 rx.internal.operators; | ||
|
||
import rx.*; | ||
|
||
/** | ||
* Transforms a Single.OnSubscribe.call() into an Single.subscribe() call. | ||
* @param <T> the value type | ||
*/ | ||
public final class SingleOnSubscribeExtend<T> implements Single.OnSubscribe<T> { | ||
final Single<T> parent; | ||
|
||
public SingleOnSubscribeExtend(Single<T> parent) { | ||
this.parent = parent; | ||
} | ||
|
||
@Override | ||
public void call(SingleSubscriber<? super T> subscriber) { | ||
subscriber.add(parent.subscribe(subscriber)); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/test/java/rx/internal/operators/CompletableOnSubscribeExtendTest.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,72 @@ | ||
/* | ||
* Copyright 2016 Netflix, Inc. | ||
* | ||
* 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 rx.internal.operators; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.atomic.*; | ||
|
||
import org.junit.Test; | ||
|
||
import rx.*; | ||
import rx.Completable.*; | ||
import rx.functions.Func1; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public final class CompletableOnSubscribeExtendTest { | ||
@Test | ||
public void convertToBoolean() { | ||
boolean completeWorked = Completable.complete().extend(toBoolean()); | ||
assertTrue(completeWorked); | ||
|
||
RuntimeException e = new RuntimeException(); | ||
boolean errorWorked = Completable.error(e).extend(toBoolean()); | ||
assertFalse(errorWorked); | ||
} | ||
|
||
private Func1<CompletableOnSubscribe, Boolean> toBoolean() { | ||
return new Func1<CompletableOnSubscribe, Boolean>() { | ||
@Override | ||
public Boolean call(CompletableOnSubscribe onSubscribe) { | ||
final AtomicBoolean worked = new AtomicBoolean(); | ||
final CountDownLatch latch = new CountDownLatch(1); | ||
onSubscribe.call(new CompletableSubscriber() { | ||
@Override | ||
public void onCompleted() { | ||
worked.set(true); | ||
latch.countDown(); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
worked.set(false); | ||
latch.countDown(); | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Subscription d) { | ||
} | ||
}); | ||
try { | ||
latch.await(); | ||
} catch (InterruptedException e) { | ||
return false; | ||
} | ||
return worked.get(); | ||
} | ||
}; | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
src/test/java/rx/internal/operators/SingleOnSubscribeExtendTest.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,112 @@ | ||
/* | ||
* Copyright 2016 Netflix, Inc. | ||
* | ||
* 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 rx.internal.operators; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import org.junit.Test; | ||
|
||
import rx.*; | ||
import rx.Single.OnSubscribe; | ||
import rx.functions.Func1; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public final class SingleOnSubscribeExtendTest { | ||
@Test | ||
public void convertToEither() { | ||
Either<String> maybeHi = Single.just("Hi").extend(toEither()); | ||
assertTrue(maybeHi.hasValue()); | ||
assertFalse(maybeHi.hasThrowable()); | ||
assertEquals("Hi", maybeHi.value()); | ||
|
||
RuntimeException e = new RuntimeException(); | ||
Either<String> maybeThrowable = Single.<String>error(e).extend(toEither()); | ||
assertFalse(maybeThrowable.hasValue()); | ||
assertTrue(maybeThrowable.hasThrowable()); | ||
assertSame(e, maybeThrowable.throwable()); | ||
} | ||
|
||
private Func1<OnSubscribe<String>, Either<String>> toEither() { | ||
return new Func1<OnSubscribe<String>, Either<String>>() { | ||
@Override | ||
public Either<String> call(OnSubscribe<String> onSubscribe) { | ||
final AtomicReference<Either<String>> eitherRef = new AtomicReference<Either<String>>(); | ||
final CountDownLatch latch = new CountDownLatch(1); | ||
onSubscribe.call(new SingleSubscriber<String>() { | ||
@Override | ||
public void onSuccess(String value) { | ||
eitherRef.set(Either.ofValue(value)); | ||
latch.countDown(); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable error) { | ||
eitherRef.set(Either.<String>ofThrowable(error)); | ||
latch.countDown(); | ||
} | ||
}); | ||
try { | ||
latch.await(); | ||
} catch (InterruptedException e) { | ||
return Either.ofThrowable(e); | ||
} | ||
return eitherRef.get(); | ||
} | ||
}; | ||
} | ||
|
||
static final class Either<T> { | ||
static <T> Either<T> ofValue(T value) { | ||
return new Either<T>(value, null); | ||
} | ||
|
||
static <T> Either<T> ofThrowable(Throwable throwable) { | ||
return new Either<T>(null, throwable); | ||
} | ||
|
||
private final T value; | ||
private final Throwable throwable; | ||
|
||
private Either(T value, Throwable throwable) { | ||
this.value = value; | ||
this.throwable = throwable; | ||
} | ||
|
||
public boolean hasValue() { | ||
return throwable == null; // Allows null as value. | ||
} | ||
|
||
public T value() { | ||
if (throwable != null) { | ||
throw new NullPointerException("No value."); | ||
} | ||
return value; | ||
} | ||
|
||
public boolean hasThrowable() { | ||
return throwable != null; | ||
} | ||
|
||
public Throwable throwable() { | ||
if (throwable == null) { | ||
throw new NullPointerException("No throwable."); | ||
} | ||
return throwable; | ||
} | ||
} | ||
} |
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.
The problem with this is that
CompletableOnSubscribe
is practucally useless; you'll have to callCompletable.create()
with it to get meaningful opererations. The whole method should be the same asto
in 2.x. TheObservable.extend
was pushed hard back then disergarding my reservations.Of course if you can give a compelling case why reduce the options for the conversion, I'm open minded.
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 agree, and would much prefer
to()
's semantics.Since
extend()
is still@Experimental
we have that option...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 much better
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.
Closing and will make a new PR, since it might be a day or two.
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.
#4423