All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hu.akarnokd.rxjava2.async.AsyncFlowable Maven / Gradle / Ivy

There is a newer version: 0.20.10
Show newest version
/*
 * Copyright 2016-2017 David Karnok
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package hu.akarnokd.rxjava2.async;

import java.util.concurrent.*;

import org.reactivestreams.*;

import hu.akarnokd.rxjava2.functions.*;
import io.reactivex.*;
import io.reactivex.disposables.Disposable;
import io.reactivex.exceptions.*;
import io.reactivex.functions.*;
import io.reactivex.internal.disposables.SequentialDisposable;
import io.reactivex.internal.functions.Functions;
import io.reactivex.internal.subscribers.LambdaSubscriber;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.processors.*;
import io.reactivex.schedulers.Schedulers;

/**
 * Utility methods to convert functions and actions into asynchronous operations through the Publisher/Subscriber
 * pattern.
 */
public final class AsyncFlowable {

    /** Utility class. */
    private AsyncFlowable() {
        throw new IllegalStateException("No instances!");
    }

    /**
     * Invokes the specified function asynchronously and returns an Flowable that emits the result.
     * 

* Note: The function is called immediately and once, not whenever an observer subscribes to the resulting * Flowable. Multiple subscriptions to this Flowable observe the same return value. *

* *

*
Backpressure:
*
The returned Flowable honors downstream backpressure.
*
Scheduler:
*
{@code start} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the result value type * @param func function to run asynchronously * @return an Flowable that emits the function's result value, or notifies observers of an exception * @see RxJava Wiki: start() * @see MSDN: Observable.Start */ public static Flowable start(Callable func) { return start(func, Schedulers.computation()); } /** * Invokes the specified function asynchronously on the specified Scheduler and returns an Flowable that * emits the result. *

* Note: The function is called immediately and once, not whenever an observer subscribes to the resulting * Flowable. Multiple subscriptions to this Flowable observe the same return value. *

* *

*
Backpressure:
*
The returned Flowable honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code start} works on.
*
*

* @param the result value type * @param func function to run asynchronously * @param scheduler Scheduler to run the function on * @return an Flowable that emits the function's result value, or notifies observers of an exception * @see RxJava Wiki: start() * @see MSDN: Observable.Start */ public static Flowable start(Callable func, Scheduler scheduler) { return Flowable.fromCallable(func).subscribeOn(scheduler).subscribeWith(AsyncProcessor.create()); } /** * Convert a synchronous action call into an asynchronous function call through a Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* @param action the action to convert * @return a function that returns an Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static Supplier> toAsync(Action action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through a Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the result value type * @param func the function to convert * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static Supplier> toAsync(Callable func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through a Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param first parameter type of the action * @param action the action to convert * @return a function that returns an Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction> toAsync(Consumer action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through a Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param first parameter type of the action * @param the result type * @param func the function to convert * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction> toAsync(Function func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through a Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param action the action to convert * @return a function that returns a Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainBiFunction> toAsync(BiConsumer action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through a Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the result type * @param func the function to convert * @return a function that returns a Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainBiFunction> toAsync(BiFunction func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param action the action to convert * @return a function that returns an Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction3> toAsync(Consumer3 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the result type * @param func the function to convert * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction3> toAsync(Function3 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param action the action to convert * @return a function that returns an Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction4> toAsync(Consumer4 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the result type * @param func the function to convert * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction4> toAsync(Function4 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param action the action to convert * @return a function that returns an Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction5> toAsync(Consumer5 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the result type * @param func the function to convert * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction5> toAsync(Function5 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param action the action to convert * @return a function that returns an Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction6> toAsync(Consumer6 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the result type * @param func the function to convert * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction6> toAsync(Function6 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param action the action to convert * @return a function that returns an Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction7> toAsync(Consumer7 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the result type * @param func the function to convert * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction7> toAsync(Function7 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the eighth parameter type * @param action the action to convert * @return a function that returns an Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction8> toAsync(Consumer8 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the eighth parameter type * @param the result type * @param func the function to convert * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction8> toAsync(Function8 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the eighth parameter type * @param the ninth parameter type * @param action the action to convert * @return a function that returns an Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction9> toAsync(Consumer9 action) { return toAsync(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the eighth parameter type * @param the ninth parameter type * @param the result type * @param func the function to convert * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction9> toAsync(Function9 func) { return toAsync(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through an Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsyncArray} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param action the action to convert * @return a function that returns an Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() */ public static PlainFunction> toAsyncArray(Consumer action) { return toAsyncArray(action, Schedulers.computation()); } /** * Convert a synchronous function call into an asynchronous function call through an Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Callable honors downstream backpressure.
*
Scheduler:
*
{@code toAsyncArray} by default operates on the {@code computation} {@link Scheduler}.
*
* * @param the result type * @param func the function to convert * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() */ public static PlainFunction> toAsyncArray(Function func) { return toAsyncArray(func, Schedulers.computation()); } /** * Convert a synchronous action call into an asynchronous function call through a Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Supplier honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Flowable that executes the {@code action} and emits an Object * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static Supplier> toAsync(final Action action, final Scheduler scheduler) { return new Supplier>() { @Override public Flowable call() { return Flowable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.run(); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through a Flowable. *

* *

*
Backpressure:
*
The Flowable returned by the Supplier honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the result value type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static Supplier> toAsync(final Callable func, final Scheduler scheduler) { return new Supplier>() { @Override public Flowable call() { return Flowable.fromCallable(func).subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through a Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param first parameter type of the action * @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction> toAsync(final Consumer action, final Scheduler scheduler) { return new PlainFunction>() { @Override public Flowable apply(final T1 t1) { return Flowable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t1); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through a Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param first parameter type of the action * @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction> toAsync(final Function func, final Scheduler scheduler) { return new PlainFunction>() { @Override public Flowable apply(final T1 t1) { return Flowable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t1); } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through a Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns a Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainBiFunction> toAsync(final BiConsumer action, final Scheduler scheduler) { return new PlainBiFunction>() { @Override public Flowable apply(final T1 t1, final T2 t2) { return Flowable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t1, t2); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through a Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns a Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainBiFunction> toAsync(final BiFunction func, final Scheduler scheduler) { return new PlainBiFunction>() { @Override public Flowable apply(final T1 t1, final T2 t2) { return Flowable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t1, t2); } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through an Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction3> toAsync(final Consumer3 action, final Scheduler scheduler) { return new PlainFunction3>() { @Override public Flowable apply(final T1 t1, final T2 t2, final T3 t3) { return Flowable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t1, t2, t3); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through an Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction3> toAsync(final Function3 func, final Scheduler scheduler) { return new PlainFunction3>() { @Override public Flowable apply(final T1 t1, final T2 t2, final T3 t3) { return Flowable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t1, t2, t3); } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through an Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction4> toAsync(final Consumer4 action, final Scheduler scheduler) { return new PlainFunction4>() { @Override public Flowable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4) { return Flowable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t1, t2, t3, t4); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through an Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction4> toAsync(final Function4 func, final Scheduler scheduler) { return new PlainFunction4>() { @Override public Flowable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4) { return Flowable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t1, t2, t3, t4); } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through an Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction5> toAsync(final Consumer5 action, final Scheduler scheduler) { return new PlainFunction5>() { @Override public Flowable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5) { return Flowable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t1, t2, t3, t4, t5); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through an Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction5> toAsync(final Function5 func, final Scheduler scheduler) { return new PlainFunction5>() { @Override public Flowable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5) { return Flowable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t1, t2, t3, t4, t5); } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through an Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction6> toAsync(final Consumer6 action, final Scheduler scheduler) { return new PlainFunction6>() { @Override public Flowable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5, final T6 t6) { return Flowable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t1, t2, t3, t4, t5, t6); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through an Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction6> toAsync(final Function6 func, final Scheduler scheduler) { return new PlainFunction6>() { @Override public Flowable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5, final T6 t6) { return Flowable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t1, t2, t3, t4, t5, t6); } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through an Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction7> toAsync( final Consumer7 action, final Scheduler scheduler) { return new PlainFunction7>() { @Override public Flowable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5, final T6 t6, final T7 t7) { return Flowable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t1, t2, t3, t4, t5, t6, t7); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through an Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction7> toAsync( final Function7 func, final Scheduler scheduler) { return new PlainFunction7>() { @Override public Flowable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5, final T6 t6, final T7 t7) { return Flowable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t1, t2, t3, t4, t5, t6, t7); } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through an Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the eighth parameter type * @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction8> toAsync( final Consumer8 action, final Scheduler scheduler) { return new PlainFunction8>() { @Override public Flowable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5, final T6 t6, final T7 t7, final T8 t8) { return Flowable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t1, t2, t3, t4, t5, t6, t7, t8); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through an Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the eighth parameter type * @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction8> toAsync( final Function8 func, final Scheduler scheduler) { return new PlainFunction8>() { @Override public Flowable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5, final T6 t6, final T7 t7, final T8 t8) { return Flowable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t1, t2, t3, t4, t5, t6, t7, t8); } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through an Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the eighth parameter type * @param the ninth parameter type * @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction9> toAsync( final Consumer9 action, final Scheduler scheduler) { return new PlainFunction9>() { @Override public Flowable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5, final T6 t6, final T7 t7, final T8 t8, final T9 t9) { return Flowable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t1, t2, t3, t4, t5, t6, t7, t8, t9); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through an Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the first parameter type * @param the second parameter type * @param the third parameter type * @param the fourth parameter type * @param the fifth parameter type * @param the sixth parameter type * @param the seventh parameter type * @param the eighth parameter type * @param the ninth parameter type * @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() * @see MSDN: Observable.ToAsync */ public static PlainFunction9> toAsync( final Function9 func, final Scheduler scheduler) { return new PlainFunction9>() { @Override public Flowable apply(final T1 t1, final T2 t2, final T3 t3, final T4 t4, final T5 t5, final T6 t6, final T7 t7, final T8 t8, final T9 t9) { return Flowable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t1, t2, t3, t4, t5, t6, t7, t8, t9); } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous action call into an asynchronous function call through an Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param action the action to convert * @param scheduler the Scheduler used to execute the {@code action} * @return a function that returns an Flowable that executes the {@code action} and emits {@code null} * @see RxJava Wiki: toAsync() */ public static PlainFunction> toAsyncArray(final Consumer action, final Scheduler scheduler) { return new PlainFunction>() { @Override public Flowable apply(final Object[] t) { return Flowable.fromCallable(new Callable() { @Override public Object call() throws Exception { action.accept(t); return AnyValue.INSTANCE; } }) .subscribeOn(scheduler); } }; } /** * Convert a synchronous function call into an asynchronous function call through an Flowable. *

* * *

*
Backpressure:
*
The Flowable returned by the Function honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code toAsync} works on.
*
* @param the result type * @param func the function to convert * @param scheduler the Scheduler used to call the {@code func} * @return a function that returns an Flowable that executes the {@code func} and emits its returned value * @see RxJava Wiki: toAsync() */ public static PlainFunction> toAsyncArray(final Function func, final Scheduler scheduler) { return new PlainFunction>() { @Override public Flowable apply(final Object[] t) { return Flowable.fromCallable(new Callable() { @Override public R call() throws Exception { return func.apply(t); } }) .subscribeOn(scheduler); } }; } /** * Invokes the asynchronous function immediately, surfacing the result through an Observable. *

* Important note subscribing to the resulting Observable blocks until the future completes. *

* * *

*
Backpressure:
*
The Flowable returned honors downstream backpressure.
*
Scheduler:
*
{@code startFuture} by default operates on the {@code computation} {@link Scheduler}.
*
* @param the result type * @param functionAsync the asynchronous function to run * @return an Observable that surfaces the result of the future * @see #startFuture(Callable, Scheduler) * @see RxJava Wiki: startFuture() */ public static Flowable startFuture(final Callable> functionAsync) { return RxJavaPlugins.onAssembly(new FlowableFromCallableNull(new Callable() { @Override public T call() throws Exception { return functionAsync.call().get(); } })); } /** * Invokes the asynchronous function immediately, surfacing the result through an Observable and waits on * the specified Scheduler. *

* * *

*
Backpressure:
*
The Flowable returned honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code startFuture} works on.
*
* @param the result type * @param functionAsync the asynchronous function to run * @param scheduler the Scheduler where the completion of the Future is awaited * @return an Observable that surfaces the result of the future * @see RxJava Wiki: startFuture() */ public static Flowable startFuture(Callable> functionAsync, Scheduler scheduler) { return startFuture(functionAsync).subscribeOn(scheduler); } /** * Returns an Observable that starts the specified asynchronous factory function whenever a new subscriber * subscribes. *

* Important note subscribing to the resulting Observable blocks until the future completes. *

* * *

*
Backpressure:
*
The Flowable returned honors downstream backpressure.
*
Scheduler:
*
{@code deferFuture} by default operates on the {@code computation} {@link Scheduler}.
*
* @param the result type * @param publisherFactoryAsync the asynchronous function to start for each observer * @return the Observable emitting items produced by the asynchronous observer produced by the factory * @see #deferFuture(Callable, Scheduler) * @see RxJava Wiki: deferFuture() */ public static Flowable deferFuture(Callable>> publisherFactoryAsync) { return deferFuture(publisherFactoryAsync, Schedulers.computation()); } /** * Returns an Observable that starts the specified asynchronous factory function whenever a new subscriber * subscribes. *

* * *

*
Backpressure:
*
The Flowable returned honors downstream backpressure.
*
Scheduler:
*
You specify the {@link Scheduler} {@code deferFuture} works on.
*
* @param the result type * @param publisherFactoryAsync the asynchronous function to start for each observer * @param scheduler the Scheduler where the completion of the Future is awaited * @return the Observable emitting items produced by the asynchronous observer produced by the factory * @see RxJava Wiki: deferFuture() */ public static Flowable deferFuture( final Callable>> publisherFactoryAsync, Scheduler scheduler) { return Flowable.defer(new Callable>() { @Override public Publisher call() throws Exception { return publisherFactoryAsync.call().get(); } }).subscribeOn(scheduler); } /** * Subscribes to the given source and calls the callback for each emitted item, and surfaces the completion * or error through a Future. *

* Important note: The returned task blocks indefinitely unless the {@code run()} method is called * or the task is scheduled on an Executor. *

* * *

*
Backpressure:
*
The input Publisher is consumed in an unbounded manner.
*
Scheduler:
*
{@code forEachFuture} by default operates on the {@code computation} {@link Scheduler}.
*
* @param the source value type * @param source the source Observable * @param onNext the action to call with each emitted element * @return the Future representing the entire for-each operation * @see #forEachFuture(Publisher, Consumer, Scheduler) * @see RxJava Wiki: forEachFuture() */ public static Future forEachFuture( Publisher source, Consumer onNext) { return forEachFuture(source, onNext, Functions.emptyConsumer(), Functions.EMPTY_ACTION, Schedulers.computation()); } /** * Subscribes to the given source and calls the callback for each emitted item, and surfaces the completion * or error through a Future. *

* Important note: The returned task blocks indefinitely unless the {@code run()} method is called * or the task is scheduled on an Executor. *

* * *

*
Backpressure:
*
The input Publisher is consumed in an unbounded manner.
*
Scheduler:
*
{@code forEachFuture} by default operates on the {@code computation} {@link Scheduler}.
*
* @param the source value type * @param source the source Observable * @param onNext the action to call with each emitted element * @param onError the action to call when an exception is emitted * @return the Future representing the entire for-each operation * @see #forEachFuture(Publisher, Consumer, Consumer, Scheduler) * @see RxJava Wiki: forEachFuture() */ public static Future forEachFuture( Publisher source, Consumer onNext, final Consumer onError) { return forEachFuture(source, onNext, onError, Functions.EMPTY_ACTION, Schedulers.computation()); } /** * Subscribes to the given source and calls the callback for each emitted item, and surfaces the completion * or error through a Future. *

* Important note: The returned task blocks indefinitely unless the {@code run()} method is called * or the task is scheduled on an Executor. *

* * *

*
Backpressure:
*
The input Publisher is consumed in an unbounded manner.
*
Scheduler:
*
{@code forEachFuture} by default operates on the {@code computation} {@link Scheduler}.
*
* @param the source value type * @param source the source Observable * @param onNext the action to call with each emitted element * @param onError the action to call when an exception is emitted * @param onComplete the action to call when the source completes * @return the Future representing the entire for-each operation * @see #forEachFuture(Publisher, Consumer, Consumer, Action, Scheduler) * @see RxJava Wiki: forEachFuture() */ public static Future forEachFuture( Publisher source, Consumer onNext, Consumer onError, Action onComplete) { return forEachFuture(source, onNext, onError, onComplete, Schedulers.computation()); } /** * Subscribes to the given source and calls the callback for each emitted item, and surfaces the completion * or error through a Future, scheduled on the given scheduler. *

* * *

*
Backpressure:
*
The input Publisher is consumed in an unbounded manner.
*
Scheduler:
*
You specify the {@link Scheduler} {@code forEachFuture} works on.
*
* @param the source value type * @param source the source Observable * @param onNext the action to call with each emitted element * @param scheduler the Scheduler where the task will await the termination of the for-each * @return the Future representing the entire for-each operation * @see RxJava Wiki: forEachFuture() */ public static Future forEachFuture( Publisher source, Consumer onNext, Scheduler scheduler) { return forEachFuture(source, onNext, Functions.emptyConsumer(), Functions.EMPTY_ACTION, scheduler); } /** * Subscribes to the given source and calls the callback for each emitted item, and surfaces the completion * or error through a Future, scheduled on the given Scheduler. *

* * *

*
Backpressure:
*
The input Publisher is consumed in an unbounded manner.
*
Scheduler:
*
You specify the {@link Scheduler} {@code forEachFuture} works on.
*
* @param the source value type * @param source the source Observable * @param onNext the action to call with each emitted element * @param onError the action to call when an exception is emitted * @param scheduler the Scheduler where the task will await the termination of the for-each * @return the Future representing the entire for-each operation * @see RxJava Wiki: forEachFuture() */ public static Future forEachFuture( Publisher source, Consumer onNext, Consumer onError, Scheduler scheduler) { return forEachFuture(source, onNext, onError, Functions.EMPTY_ACTION, scheduler); } /** * Subscribes to the given source and calls the callback for each emitted item, and surfaces the completion * or error through a Future, scheduled on the given Scheduler. *

* * *

*
Backpressure:
*
The input Publisher is consumed in an unbounded manner.
*
Scheduler:
*
You specify the {@link Scheduler} {@code forEachFuture} works on.
*
* @param the source value type * @param source the source Observable * @param onNext the action to call with each emitted element * @param onError the action to call when an exception is emitted * @param onComplete the action to call when the source completes * @param scheduler the Scheduler where the task will await the termination of the for-each * @return the Future representing the entire for-each operation * @see RxJava Wiki: forEachFuture() */ public static Future forEachFuture( Publisher source, Consumer onNext, final Consumer onError, final Action onComplete, Scheduler scheduler) { SequentialDisposable d = new SequentialDisposable(); final FutureCompletable f = new FutureCompletable(d); LambdaSubscriber ls = new LambdaSubscriber(onNext, new Consumer() { @Override public void accept(Throwable e) throws Exception { try { onError.accept(e); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); f.completeExceptionally(new CompositeException(e, ex)); return; } f.completeExceptionally(e); } }, new Action() { @Override public void run() throws Exception { try { onComplete.run(); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); f.completeExceptionally(ex); return; } f.complete(null); } }, new Consumer() { @Override public void accept(Subscription s) throws Exception { s.request(Long.MAX_VALUE); } }); d.lazySet(ls); Flowable.fromPublisher(source).subscribeOn(scheduler).subscribe(ls); return f; } /** * Runs the provided action on the given scheduler and allows propagation of multiple events to the * observers of the returned DisposableFlowable. The action is immediately executed and unobserved values * will be lost. * *
*
Backpressure:
*
The resulting Flowable signals MissingBackpressureException if the downstream can't keep up.
*
Scheduler:
*
You specify the {@link Scheduler} {@code forEachFuture} works on.
*
* @param the output value type * @param scheduler the Scheduler where the action is executed * @param action the action to execute, receives a Subscriber where the events can be pumped and a * Disposable which lets it check for cancellation condition * @return an DisposableFlowable that provides a Disposable interface to cancel the action * @see RxJava Wiki: runAsync() */ public static DisposableFlowable runAsync(Scheduler scheduler, final BiConsumer, ? super Disposable> action) { return runAsync(scheduler, PublishProcessor.create(), action); } /** * Runs the provided action on the given scheduler and allows propagation of multiple events to the * observers of the returned DisposableFlowable. The action is immediately executed and unobserved values * might be lost, depending on the Subject type used. * *
*
Backpressure:
*
The provided Processor's backpressure behavior determines the returned Flowable's backpressure behavior.
*
Scheduler:
*
You specify the {@link Scheduler} {@code forEachFuture} works on.
*
* @param the output value of the action * @param the output type of the observable sequence * @param scheduler the Scheduler where the action is executed * @param processor the subject to use to distribute values emitted by the action * @param action the action to execute, receives a Subscriber where the events can be pumped and a * Disposable which lets it check for cancellation condition * @return an DisposableFlowable that provides a Disposable interface to cancel the action * @see RxJava Wiki: runAsync() */ public static DisposableFlowable runAsync(Scheduler scheduler, final Processor processor, final BiConsumer, ? super Disposable> action) { final SequentialDisposable d = new SequentialDisposable(); d.replace(scheduler.scheduleDirect(new Runnable() { @Override public void run() { try { action.accept(processor, d); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); processor.onError(ex); } } })); return new DisposableFlowable() { @Override protected void subscribeActual(Subscriber s) { processor.subscribe(s); } @Override public boolean isDisposed() { return d.isDisposed(); } @Override public void dispose() { d.dispose(); } }; } }