Class CompletableSubject

java.lang.Object
io.reactivex.rxjava4.core.Completable
io.reactivex.rxjava4.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
  • Method Details

    • create

      Creates a fresh CompletableSubject.
      Returns:
      the new CompletableSubject instance
    • onSubscribe

      public void onSubscribe(Disposable d)
      Description copied from interface: CompletableObserver
      Called once by the Completable to set a Disposable on this instance which then can be used to cancel the subscription at any time.
      Specified by:
      onSubscribe in interface CompletableObserver
      Parameters:
      d - the Disposable instance to call dispose on for cancellation, not null
    • onError

      public void onError(Throwable e)
      Description copied from interface: CompletableObserver
      Called once if the deferred computation 'throws' an exception.
      Specified by:
      onError in interface CompletableObserver
      Parameters:
      e - the exception, not null.
    • onComplete

      public void onComplete()
      Description copied from interface: CompletableObserver
      Called once the deferred computation completes normally.
      Specified by:
      onComplete in interface CompletableObserver
    • subscribeActual

      protected void subscribeActual(CompletableObserver observer)
      Description copied from class: Completable
      Implement this method to handle the incoming CompletableObservers and perform the business logic in your operator.

      There is no need to call any of the plugin hooks on the current Completable instance or the CompletableObserver; all hooks and basic safeguards have been applied by Completable.subscribe(CompletableObserver) before this method gets called.

      Specified by:
      subscribeActual in class Completable
      Parameters:
      observer - the CompletableObserver instance, never null
    • getThrowable

      @Nullable public @Nullable Throwable getThrowable()
      Returns the terminal error if this CompletableSubject has been terminated with an error, null otherwise.
      Returns:
      the terminal error or null if not terminated or not with an error
    • hasThrowable

      public boolean hasThrowable()
      Returns true if this CompletableSubject has been terminated with an error.
      Returns:
      true if this CompletableSubject has been terminated with an error
    • hasComplete

      public boolean hasComplete()
      Returns true if this CompletableSubject has been completed.
      Returns:
      true if this CompletableSubject has been completed
    • hasObservers

      public boolean hasObservers()
      Returns true if this CompletableSubject has observers.
      Returns:
      true if this CompletableSubject has observers