-
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: Defer Scheduler initialization in hook init API #4572
Comments
I don't think RxJava needs this change. We have standard Java ExecutorServices backing the Schedulers so their creation should always succeed in a reasonable JDK implementation. Maybe you could combine class AndroidSchedulers {
static final class DefaultHolder {
static final Scheduler DEFAULT_MAIN = new HandlerScheduler(
new Handler(Looper.getMainLooper())));
}
static volatile Callable<Scheduler> mainHook;
public static Scheduler main() {
Function f = mainHook;
if (f == null) {
return DEFAULT_HOLDER.DEFAULT_MAIN; // initialized once and lazily
}
try {
return mainHook.call();
} catch (Throwable ex) {
throw ExceptionHelper.wrapOrThrow(ex);
}
}
} |
That doesn't allow decorating the default scheduler which is a common use I know RxJava doesn't need this, what I'm asking is if the change would be On Wed, Sep 21, 2016, 3:40 AM David Karnok notifications@github.com wrote:
|
The OP wanted to avoid depending on Handler completely so he could test on desktop. The only workaround I see is a boolean flag that when set, invokes the Function with null instead of loading the default Android scheduler. class AndroidSchedulers {
static final class DefaultHolder {
static final Scheduler DEFAULT_MAIN = new HandlerScheduler(
new Handler(Looper.getMainLooper())));
}
static volatile boolean noDefault;
static volatile Function<Scheduler, Scheduler> mainHook;
public static Scheduler main() {
Function f = mainHook;
if (f == null) {
return DEFAULT_HOLDER.DEFAULT_MAIN; // initialized once and lazily
}
try {
if (noDefault) {
return f.apply(null);
}
return f.apply(DEFAULT_HOLDER.DEFAULT_MAIN);
} catch (Throwable ex) {
throw ExceptionHelper.wrapOrThrow(ex);
}
}
} |
My original proposal passes a |
Right, sounds reasonable now. How about a PR? |
@akarnokd I'm the said OP. Happy to put together a PR for the change. |
Sure. |
Closing via #4585 |
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>
toFunction<Scheduler, Callable<Scheduler>>
which would allow ignoring the default scheduler instance by never delegating toCallable.call()
to create it.The implementation of the initializer would change to something like:
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?
The text was updated successfully, but these errors were encountered: