-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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 lazy loading of default main Scheduler #338
2.x: Add lazy loading of default main Scheduler #338
Conversation
import io.reactivex.functions.Function; | ||
import io.reactivex.internal.functions.ObjectHelper; | ||
import io.reactivex.internal.util.ExceptionHelper; |
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.
Remove both of these. We cannot depend on internal classes.
public static Scheduler initMainThreadScheduler(Scheduler scheduler) { | ||
Function<Scheduler, Scheduler> f = onInitMainThreadHandler; | ||
public static Scheduler initMainThreadScheduler(Callable<Scheduler> scheduler) { | ||
ObjectHelper.requireNonNull(scheduler, "Scheduler Callable can't be 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.
Just do a null check yourself
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.
Also change error message to "scheduler == null"
} | ||
|
||
public static void setMainThreadSchedulerHandler(Function<Scheduler, Scheduler> handler) { | ||
onMainThreadHandler = handler; | ||
} | ||
|
||
public static Scheduler onMainThreadScheduler(Scheduler scheduler) { | ||
ObjectHelper.requireNonNull(scheduler, "Scheduler can't be 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.
Ditto
try { | ||
return ObjectHelper.requireNonNull(s.call(), "Scheduler Callable result can't be null"); | ||
} catch (Throwable ex) { | ||
throw ExceptionHelper.wrapOrThrow(ex); |
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.
Inline this implementation.
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.
How about I change it back to Exceptions.propagate
which is functionally the same and uses a public RxJava API?
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.
Sure
@@ -64,6 +62,26 @@ public static void reset() { | |||
setMainThreadSchedulerHandler(null); | |||
} | |||
|
|||
static Scheduler callRequireNonNull(Callable<Scheduler> s) { | |||
try { | |||
return ObjectHelper.requireNonNull(s.call(), "Scheduler Callable result can't be 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.
Just do null check yourself
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 think "returned null" instead of "result can't be null" is more clear
}; | ||
|
||
// unsafe default Scheduler Callable should not be evaluated when overriding | ||
try { |
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 whole try/catch isn't needed. The plugins are reset before and after each test.
// fail when Callable is null | ||
try { | ||
RxAndroidPlugins.initMainThreadScheduler(null); | ||
fail("Should have thrown NullPointerException"); |
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.
Remove message from fail()
.
|
||
@Test | ||
public void overrideInitMainSchedulerThrowsWhenSchedulerCallableIsNull() { | ||
// fail when Callable 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.
Comment is redundant with test name
} | ||
}; | ||
|
||
// fail when Callable result 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.
Comment is redundant with test name.
// fail when Callable result is null | ||
try { | ||
RxAndroidPlugins.initMainThreadScheduler(nullResultCallable); | ||
fail("Should have thrown NullPointerException"); |
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.
remove message
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 looks great. Just a few really tiny style comments.
if (f == null) { | ||
return scheduler; | ||
public static Scheduler initMainThreadScheduler(Callable<Scheduler> scheduler) { | ||
if(scheduler == 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.
nit: space after if
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.
Is there a project style settings file (IntelliJ/AS format) available? I've been manually formatting which can lead to missing stuff like this.
static Scheduler callRequireNonNull(Callable<Scheduler> s) { | ||
try { | ||
Scheduler scheduler = s.call(); | ||
if(scheduler == 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.
nit: space after if
RxAndroidPlugins | ||
.setInitMainThreadSchedulerHandler(new Function<Callable<Scheduler>, Scheduler>() { | ||
@Override public Scheduler apply(Callable<Scheduler> scheduler) { | ||
throw new RuntimeException(); |
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.
should be AssertionError
for better semantics
public void defaultMainThreadSchedulerIsInitializedLazily() { | ||
final Function<Callable<Scheduler>, Scheduler> safeOverride = | ||
new Function<Callable<Scheduler>, Scheduler>() { | ||
@Override public Scheduler apply(Callable<Scheduler> __) { |
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.
__
-> scheduler
return new EmptyScheduler(); | ||
} | ||
}; | ||
final Callable<Scheduler> unsafeDefault = new Callable<Scheduler>() { |
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.
nit: doesn't need to be final
|
||
@Test | ||
public void defaultMainThreadSchedulerIsInitializedLazily() { | ||
final Function<Callable<Scheduler>, Scheduler> safeOverride = |
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.
nit: doesn't need to be final
Sadly, no. I just use our Square style except with the (terrible) 4 space On Fri, Oct 7, 2016 at 5:04 PM Peter Tackage notifications@github.com
|
Er, that was a reply to your inline comment. Guess it doesn't work well from email. |
OK, I think I've covered each of your comments. Thanks for the review. |
Thanks! |
Initial proposal to address #332
I've essentially used the same implementation as in RxJava: ReactiveX/RxJava#4585.
For the sake of common implementation, I've used RxJava's internal(?) classes
ObjectHelper
andExceptionHelper
. Happy to consider alternatives if we don't want this.