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: Add lazy loading of default main Scheduler #338

Merged
merged 10 commits into from
Oct 7, 2016
Merged

2.x: Add lazy loading of default main Scheduler #338

merged 10 commits into from
Oct 7, 2016

Conversation

peter-tackage
Copy link
Contributor

@peter-tackage peter-tackage commented Oct 5, 2016

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 and ExceptionHelper. Happy to consider alternatives if we don't want this.

@peter-tackage peter-tackage changed the title Add lazy loading of default main Scheduler 2.x: Add lazy loading of default main Scheduler Oct 5, 2016
import io.reactivex.functions.Function;
import io.reactivex.internal.functions.ObjectHelper;
import io.reactivex.internal.util.ExceptionHelper;
Copy link
Contributor

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");
Copy link
Contributor

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

Copy link
Contributor

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");
Copy link
Contributor

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);
Copy link
Contributor

Choose a reason for hiding this comment

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

Inline this implementation.

Copy link
Contributor Author

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?

Copy link
Contributor

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");
Copy link
Contributor

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

Copy link
Contributor

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 {
Copy link
Contributor

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");
Copy link
Contributor

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
Copy link
Contributor

@JakeWharton JakeWharton Oct 6, 2016

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
Copy link
Contributor

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");
Copy link
Contributor

Choose a reason for hiding this comment

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

remove message

Copy link
Contributor

@JakeWharton JakeWharton left a 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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: space after if

Copy link
Contributor Author

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) {
Copy link
Contributor

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();
Copy link
Contributor

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> __) {
Copy link
Contributor

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>() {
Copy link
Contributor

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 =
Copy link
Contributor

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

@JakeWharton
Copy link
Contributor

Sadly, no. I just use our Square style except with the (terrible) 4 space
indent.

On Fri, Oct 7, 2016 at 5:04 PM Peter Tackage notifications@github.com
wrote:

@peter-tackage commented on this pull request.

In
rxandroid/src/main/java/io/reactivex/android/plugins/RxAndroidPlugins.java
#338:

     onInitMainThreadHandler = handler;
 }
  • public static Scheduler initMainThreadScheduler(Scheduler scheduler) {
  •    Function<Scheduler, Scheduler> f = onInitMainThreadHandler;
    
  •    if (f == null) {
    
  •        return scheduler;
    
  • public static Scheduler initMainThreadScheduler(Callable scheduler) {
  •    if(scheduler == null) {
    

Is there a project style settings file (IntelliJ/AS format) available?
I've been manually formatting which can lead to missing stuff like this.


You are receiving this because you commented.

Reply to this email directly, view it on GitHub
#338, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAEEEScumvpcDq6hsOjtPt4mVndnvunmks5qxrPNgaJpZM4KPYKd
.

@JakeWharton
Copy link
Contributor

Er, that was a reply to your inline comment. Guess it doesn't work well from email.

@peter-tackage
Copy link
Contributor Author

OK, I think I've covered each of your comments. Thanks for the review.

@JakeWharton JakeWharton merged commit 79d0991 into ReactiveX:2.x Oct 7, 2016
@JakeWharton
Copy link
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants