hu.akarnokd.rxjava3.async.AsyncObservable Maven / Gradle / Ivy
Show all versions of rxjava3-extensions Show documentation
/*
* Copyright 2016-2019 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.rxjava3.async;
import java.util.concurrent.*;
import hu.akarnokd.rxjava3.functions.*;
import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.exceptions.*;
import io.reactivex.rxjava3.functions.*;
import io.reactivex.rxjava3.internal.disposables.SequentialDisposable;
import io.reactivex.rxjava3.internal.functions.Functions;
import io.reactivex.rxjava3.internal.observers.LambdaObserver;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
import io.reactivex.rxjava3.schedulers.Schedulers;
import io.reactivex.rxjava3.subjects.*;
/**
* Utility methods to convert functions and actions into asynchronous operations through the Observable/Observer
* pattern.
*/
public final class AsyncObservable {
/** Utility class. */
private AsyncObservable() {
throw new IllegalStateException("No instances!");
}
/**
* Invokes the specified function asynchronously and returns an Observable that emits the result.
*
* Note: The function is called immediately and once, not whenever an observer subscribes to the resulting
* Observable. Multiple subscriptions to this Observable observe the same return value.
*
*
*
* - 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 Observable that emits the function's result value, or notifies observers of an exception
* @see RxJava Wiki: start()
* @see MSDN: Observable.Start
*/
public static Observable start(Supplier extends T> func) {
return start(func, Schedulers.computation());
}
/**
* Invokes the specified function asynchronously on the specified Scheduler and returns an Observable that
* emits the result.
*
* Note: The function is called immediately and once, not whenever an observer subscribes to the resulting
* Observable. Multiple subscriptions to this Observable observe the same return value.
*
*
*
* - 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 Observable that emits the function's result value, or notifies observers of an exception
* @see RxJava Wiki: start()
* @see MSDN: Observable.Start
*/
public static Observable start(Supplier extends T> func, Scheduler scheduler) {
return Observable.fromSupplier(func).subscribeOn(scheduler).subscribeWith(AsyncSubject.create());
}
/**
* Convert a synchronous action call into an asynchronous function call through a Observable.
*
*
*
* - Scheduler:
* - {@code toAsync} by default operates on the {@code computation} {@link Scheduler}.
*
* @param action the action to convert
* @return a function that returns an Observable that executes the {@code action} and emits {@code null}
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static SimpleCallable> toAsync(Action action) {
return toAsync(action, Schedulers.computation());
}
/**
* Convert a synchronous function call into an asynchronous function call through a Observable.
*
*
*
* - Scheduler:
* - {@code toAsyncCallable} 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 Observable that executes the {@code func} and emits its returned value
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static SimpleCallable> toAsyncCallable(Callable extends R> func) {
return toAsyncCallable(func, Schedulers.computation());
}
/**
* Convert a synchronous function call into an asynchronous function call through a Observable.
*
*
*
* - Scheduler:
* - {@code toAsyncSupplier} 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 Observable that executes the {@code func} and emits its returned value
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static SimpleCallable> toAsyncSupplier(Supplier extends R> func) {
return toAsyncSupplier(func, Schedulers.computation());
}
/**
* Convert a synchronous action call into an asynchronous function call through a Observable.
*
*
*
* - 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 Observable that executes the {@code action} and emits {@code null}
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static PlainFunction> toAsync(Consumer super T1> action) {
return toAsync(action, Schedulers.computation());
}
/**
* Convert a synchronous function call into an asynchronous function call through a Observable.
*
*
*
* - 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 Observable that executes the {@code func} and emits its returned value
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static PlainFunction> toAsync(Function super T1, ? extends R> func) {
return toAsync(func, Schedulers.computation());
}
/**
* Convert a synchronous action call into an asynchronous function call through a Observable.
*
*
*
* - 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 Observable that executes the {@code action} and emits {@code null}
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static PlainBiFunction> toAsync(BiConsumer super T1, ? super T2> action) {
return toAsync(action, Schedulers.computation());
}
/**
* Convert a synchronous function call into an asynchronous function call through a Observable.
*
*
*
* - 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 Observable that executes the {@code func} and emits its returned value
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static PlainBiFunction> toAsync(BiFunction super T1, ? super T2, ? extends R> func) {
return toAsync(func, Schedulers.computation());
}
/**
* Convert a synchronous action call into an asynchronous function call through an Observable.
*
*
*
* - 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 Observable that executes the {@code action} and emits {@code null}
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static PlainFunction3> toAsync(Consumer3 super T1, ? super T2, ? super T3> action) {
return toAsync(action, Schedulers.computation());
}
/**
* Convert a synchronous function call into an asynchronous function call through an Observable.
*
*
*
* - 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 Observable that executes the {@code func} and emits its returned value
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static PlainFunction3> toAsync(Function3 super T1, ? super T2, ? super T3, ? extends R> func) {
return toAsync(func, Schedulers.computation());
}
/**
* Convert a synchronous action call into an asynchronous function call through an Observable.
*
*
*
* - 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 Observable that executes the {@code action} and emits {@code null}
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static PlainFunction4> toAsync(Consumer4 super T1, ? super T2, ? super T3, ? super T4> action) {
return toAsync(action, Schedulers.computation());
}
/**
* Convert a synchronous function call into an asynchronous function call through an Observable.
*
*
*
* - 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 Observable that executes the {@code func} and emits its returned value
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static PlainFunction4> toAsync(Function4 super T1, ? super T2, ? super T3, ? super T4, ? extends R> func) {
return toAsync(func, Schedulers.computation());
}
/**
* Convert a synchronous action call into an asynchronous function call through an Observable.
*
*
*
* - 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 Observable that executes the {@code action} and emits {@code null}
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static PlainFunction5> toAsync(Consumer5 super T1, ? super T2, ? super T3, ? super T4, ? super T5> action) {
return toAsync(action, Schedulers.computation());
}
/**
* Convert a synchronous function call into an asynchronous function call through an Observable.
*
*
*
* - 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 Observable that executes the {@code func} and emits its returned value
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static PlainFunction5> toAsync(Function5 super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? extends R> func) {
return toAsync(func, Schedulers.computation());
}
/**
* Convert a synchronous action call into an asynchronous function call through an Observable.
*
*
*
* - 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 Observable that executes the {@code action} and emits {@code null}
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static PlainFunction6> toAsync(Consumer6 super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6> action) {
return toAsync(action, Schedulers.computation());
}
/**
* Convert a synchronous function call into an asynchronous function call through an Observable.
*
*
*
* - 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 Observable that executes the {@code func} and emits its returned value
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static PlainFunction6> toAsync(Function6 super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? extends R> func) {
return toAsync(func, Schedulers.computation());
}
/**
* Convert a synchronous action call into an asynchronous function call through an Observable.
*
*
*
* - 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 Observable that executes the {@code action} and emits {@code null}
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static PlainFunction7> toAsync(Consumer7 super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7> action) {
return toAsync(action, Schedulers.computation());
}
/**
* Convert a synchronous function call into an asynchronous function call through an Observable.
*
*
*
* - 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 Observable that executes the {@code func} and emits its returned value
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static PlainFunction7> toAsync(Function7 super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? extends R> func) {
return toAsync(func, Schedulers.computation());
}
/**
* Convert a synchronous action call into an asynchronous function call through an Observable.
*
*
*
* - 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 Observable that executes the {@code action} and emits {@code null}
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static PlainFunction8> toAsync(Consumer8 super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? super T8> action) {
return toAsync(action, Schedulers.computation());
}
/**
* Convert a synchronous function call into an asynchronous function call through an Observable.
*
*
*
* - 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 Observable that executes the {@code func} and emits its returned value
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static PlainFunction8> toAsync(Function8 super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? super T8, ? extends R> func) {
return toAsync(func, Schedulers.computation());
}
/**
* Convert a synchronous action call into an asynchronous function call through an Observable.
*
*
*
* - 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 Observable that executes the {@code action} and emits {@code null}
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static PlainFunction9> toAsync(Consumer9 super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? super T8, ? super T9> action) {
return toAsync(action, Schedulers.computation());
}
/**
* Convert a synchronous function call into an asynchronous function call through an Observable.
*
*
*
* - 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 Observable that executes the {@code func} and emits its returned value
* @see RxJava Wiki: toAsync()
* @see MSDN: Observable.ToAsync
*/
public static PlainFunction9> toAsync(Function9 super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? super T8, ? super T9, ? extends R> func) {
return toAsync(func, Schedulers.computation());
}
/**
* Convert a synchronous action call into an asynchronous function call through an Observable.
*
*
*
* - Scheduler:
* - {@code toAsyncArray} by default operates on the {@code computation} {@link Scheduler}.
*
*
* @param action the action to convert
* @return a function that returns an Observable that executes the {@code action} and emits {@code null}
* @see RxJava Wiki: toAsync()
*/
public static PlainFunction