-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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: add Observable.switchMapSingle and switchMapSingleDelayError #5161
Conversation
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.
Looks good but some changes are required.
} else { | ||
single = Single.unsafeCreate(source); | ||
} | ||
return (Observable<R>) single.toObservable(); |
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.
You could just use:
return RxJavaPlugins.onAssembly(new SingleToObservable(mapper.apply(t)));
@Experimental | ||
@CheckReturnValue | ||
@SchedulerSupport(SchedulerSupport.NONE) | ||
public final <R> Observable<R> switchMapSingle(Function<? super T, ? extends SingleSource<? extends R>> mapper) { |
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.
@NonNull
?
@Experimental | ||
@CheckReturnValue | ||
@SchedulerSupport(SchedulerSupport.NONE) | ||
public final <R> Observable<R> switchMapSingleDelayError(Function<? super T, ? extends SingleSource<? extends R>> mapper) { |
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.
@NonNull Function
?
|
||
private static <T, R> Function<T, Observable<R>> convertSingleMapperToObservableMapper( | ||
final Function<? super T, ? extends SingleSource<? extends R>> mapper) { | ||
return new Function<T, Observable<R>>() { |
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.
ObjectHelper.requireNonNull(mapper, "mapper is null");
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.
Please avoid anonymous inner classes.
f2e0b0e
to
b733386
Compare
updated with suggested changes |
unrelated ci failure
|
Tracking via #5154. Could be due to low timeout settings and Travis overload. Just rerun the build next time; I did it just now. |
@Test | ||
public void switchMapSingleFunctionDoesntReturnSingle() { | ||
Observable.just(0) | ||
.switchMapSingle(new Function<Object, SingleSource<Integer>>() { |
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 remember that with flatMapSingle there was in the initial draft a few errors that errors weren't propagated correctly. Should not those cases also be tested that in the future once the operators receive custom implementations everything still works correctly?
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 don't think so. At the moment those cases have full coverage. If we make custom implementations then a whole suite of tests need to be added just get coverage back up again and it will be an obvious timesaver to duplicate the tests you refer to.
|
||
@Override | ||
public Observable<R> apply(T t) throws Exception { | ||
return RxJavaPlugins.onAssembly(new SingleToObservable<R>(mapper.apply(t))); |
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.
check that mapper.apply(t)
does not return null?
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.
Indeed, this should do it:
return RxJavaPlugins.onAssembly(new SingleToObservable<R>(
ObjectHelper.requireNonNull(mapper.apply(t), "The mapper returned a null value")));
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.
thanks will do
Codecov Report
@@ Coverage Diff @@
## 2.x #5161 +/- ##
============================================
+ Coverage 95.86% 95.88% +0.02%
- Complexity 5648 5652 +4
============================================
Files 621 621
Lines 39962 39972 +10
Branches 5610 5610
============================================
+ Hits 38309 38329 +20
+ Misses 665 657 -8
+ Partials 988 986 -2
Continue to review full report at Codecov.
|
b733386
to
315fb80
Compare
updated with null check on mapper call and added unit tests for null mapper and null mapper call result. Not proposing to duplicate all tests. @akarnokd what would you like? |
Don't duplicate tests. When the specialized implementation happens, that will ask for proper coverage by itself. |
This is a new operator discussed in #4853. The issue refers to a goodly number of new operators which I'll do bit by bit as my time allows and to ensure review is not too daunting.