Class SingleSubject<T>

java.lang.Object
io.reactivex.rxjava3.core.Single<T>
io.reactivex.rxjava3.subjects.SingleSubject<T>
Type Parameters:
T - the value type received and emitted
All Implemented Interfaces:
SingleObserver<T>, SingleSource<T>

public final class SingleSubject<T> extends Single<T> implements SingleObserver<T>
Represents a hot Single-like source and consumer of events similar to Subjects.

This subject does not have a public constructor by design; a new non-terminated instance of this SingleSubject can be created via the create() method.

Since the SingleSubject is conceptionally derived from the Processor type in the Reactive Streams specification, nulls are not allowed (Rule 2.13) as parameters to onSuccess(Object) and onError(Throwable). Such calls will result in a NullPointerException being thrown and the subject's state is not changed.

Since a SingleSubject is a Single, calling onSuccess or onError will move this SingleSubject into its terminal state atomically.

All methods are thread safe. Calling onSuccess(Object) multiple times has no effect. Calling onError(Throwable) multiple times relays the Throwable to the RxJavaPlugins.onError(Throwable) global error handler.

Even though SingleSubject implements the SingleObserver interface, calling onSubscribe is not required (Rule 2.12) if the subject is used as a standalone source. However, calling onSubscribe after the SingleSubject reached its terminal state will result in the given Disposable being disposed immediately.

This SingleSubject supports the standard state-peeking methods hasThrowable(), getThrowable() and hasObservers() as well as means to read any success item in a non-blocking and thread-safe manner via hasValue() and getValue().

The SingleSubject does not support clearing its cached onSuccess value.

Scheduler:
SingleSubject does not operate by default on a particular Scheduler and the SingleObservers get notified on the thread where the terminating onSuccess or onError methods were invoked.
Error handling:
When the onError(Throwable) is called, the SingleSubject enters into a terminal state and emits the same Throwable instance to the last set of SingleObservers. During this emission, if one or more SingleObservers dispose their respective Disposables, the Throwable is delivered to the global error handler via RxJavaPlugins.onError(Throwable) (multiple times if multiple SingleObservers cancel at once). If there were no SingleObservers subscribed to this SingleSubject when the onError() was called, the global error handler is not invoked.

Example usage:


 SingleSubject<Integer> subject1 = SingleSubject.create();
 
 TestObserver<Integer> to1 = subject1.test();
 
 // SingleSubjects are empty by default
 to1.assertEmpty();
 
 subject1.onSuccess(1);
 
 // onSuccess is a terminal event with SingleSubjects
 // TestObserver converts onSuccess into onNext + onComplete
 to1.assertResult(1);

 TestObserver<Integer> to2 = subject1.test();
 
 // late Observers receive the terminal signal (onSuccess) too
 to2.assertResult(1);
 

History: 2.0.5 - experimental

Since:
2.1