Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The {@link IoToMainThreadTransformers} is a factory of convenient Rx transformers to use with {@link io.reactivex.Observable#compose(ObservableTransformer)} operator to transform observable via apply {@link io.reactivex.Observable#subscribeOn(Scheduler)} operator with the {@link Schedulers#io()} and {@link io.reactivex.Observable#observeOn(Sche…
/**
* The {@link IoToMainThreadTransformers} is a factory of convenient Rx transformers to use with {@link
* io.reactivex.Observable#compose(ObservableTransformer)} operator to transform observable via
* apply {@link io.reactivex.Observable#subscribeOn(Scheduler)} operator with the {@link
* Schedulers#io()} and {@link io.reactivex.Observable#observeOn(Scheduler)} operator with the
* {@link AndroidSchedulers#mainThread()}.
*/
public final class IoToMainThreadTransformers {
private IoToMainThreadTransformers() {
throw new AssertionError("Not instantiated class");
}
/**
* Creates the {@link ObservableTransformer} to transform observable via apply {@link
* io.reactivex.Observable#subscribeOn(Scheduler)} operator with the {@link Schedulers#io()} and {@link
* io.reactivex.Observable#observeOn(Scheduler)} operator with the {@link
* AndroidSchedulers#mainThread()}.
*
* @param <T> the type of emitting value
* @return the {@link ObservableTransformer} to use
*/
@NonNull
public static <T> ObservableTransformer<T, T> ofObservable() {
return upstream -> upstream.subscribeOn(Schedulers.io()).observeOn(
AndroidSchedulers.mainThread());
}
/**
* Creates the {@link FlowableTransformer} to transform observable via apply {@link
* io.reactivex.Flowable#subscribeOn(Scheduler)} operator with the {@link Schedulers#io()} and {@link
* io.reactivex.Flowable#observeOn(Scheduler)} operator with the {@link
* AndroidSchedulers#mainThread()}.
*
* @param <T> the type of emitting value
* @return the {@link FlowableTransformer} to use
*/
@NonNull
public static <T> FlowableTransformer<T, T> ofFlowable() {
return upstream -> upstream.subscribeOn(Schedulers.io()).observeOn(
AndroidSchedulers.mainThread());
}
/**
* Creates the {@link MaybeTransformer} to transform observable via apply {@link
* io.reactivex.Maybe#subscribeOn(Scheduler)} operator with the {@link Schedulers#io()} and {@link
* io.reactivex.Maybe#observeOn(Scheduler)} operator with the {@link
* AndroidSchedulers#mainThread()}.
*
* @param <T> the type of emitting value
* @return the {@link MaybeTransformer} to use
*/
@NonNull
public static <T> MaybeTransformer<T, T> ofMaybe() {
return upstream -> upstream.subscribeOn(Schedulers.io()).observeOn(
AndroidSchedulers.mainThread());
}
/**
* Creates the {@link SingleTransformer} to transform observable via apply {@link
* io.reactivex.Single#subscribeOn(Scheduler)} operator with the {@link Schedulers#io()} and
* {@link io.reactivex.Single#observeOn(Scheduler)} operator with the {@link
* AndroidSchedulers#mainThread()}.
*
* @param <T> the type of emitting value
* @return the {@link SingleTransformer} to use
*/
@NonNull
public static <T> SingleTransformer<T, T> ofSingle() {
return upstream -> upstream.subscribeOn(Schedulers.io()).observeOn(
AndroidSchedulers.mainThread());
}
/**
* Creates the {@link CompletableTransformer} to transform observable via apply {@link
* io.reactivex.Completable#subscribeOn(Scheduler)} operator with the {@link Schedulers#io()} and
* {@link io.reactivex.Completable#observeOn(Scheduler)} operator with the {@link
* AndroidSchedulers#mainThread()}.
*
* @return the {@link CompletableTransformer} to use
*/
@NonNull
public static CompletableTransformer ofCompletable() {
return upstream -> upstream.subscribeOn(Schedulers.io()).observeOn(
AndroidSchedulers.mainThread());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.