Skip to content

2.x: Defer Scheduler initialization in hook init API #4572

@JakeWharton

Description

@JakeWharton

An issue was filed on RxAndroid asking for the default "main thread" scheduler to be lazily initialized. This would allow support for unit testing on the JVM by swapping out the scheduler for one that works where Android's main thread doesn't exist.

The hook API provides a means of replacing the scheduler during initialization, but the problem is that in order to call these "init" methods the instance needs to be eagerly created.

What I'm proposing is that we change the "init" functions from Function<Scheduler, Scheduler> to Function<Scheduler, Callable<Scheduler>> which would allow ignoring the default scheduler instance by never delegating to Callable.call() to create it.

The implementation of the initializer would change to something like:

Scheduler initFoo(Callable<Scheduler> defaultScheduler);
Function f = initFunc;
if (f == null) {
  return defaultScheduler.call();
}
return apply(f, defaultScheduler);

Now we can make this change only in RxAndroid to support this case. I think that there's a large value in having as much symmetry between the two libraries as possible, however.

Is this something that would be acceptable to change in RxJava? Or are there any alternative APIs that anyone can think of for this?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions