Class CompletableSubject

java.lang.Object
io.reactivex.rxjava3.core.Completable
io.reactivex.rxjava3.subjects.CompletableSubject
All Implemented Interfaces:
CompletableObserver, CompletableSource

public final class CompletableSubject extends Completable implements CompletableObserver
Represents a hot Completable-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 CompletableSubject can be created via the create() method.

Since the CompletableSubject is conceptionally derived from the Processor type in the Reactive Streams specification, nulls are not allowed (Rule 2.13) as parameters to onError(Throwable).

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

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

This CompletableSubject supports the standard state-peeking methods hasComplete(), hasThrowable(), getThrowable() and hasObservers().

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

Example usage:


 CompletableSubject subject = CompletableSubject.create();

 TestObserver<Void> to1 = subject.test();

 // a fresh CompletableSubject is empty
 to1.assertEmpty();

 subject.onComplete();

 // a CompletableSubject is always void of items
 to1.assertResult();

 TestObserver<Void> to2 = subject.test()

 // late CompletableObservers receive the terminal event
 to2.assertResult();
 

History: 2.0.5 - experimental

Since:
2.1