public final class CompletableSubject extends Completable implements CompletableObserver
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,
null
s 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()
.
CompletableSubject
does not operate by default on a particular Scheduler
and
the CompletableObserver
s get notified on the thread where the terminating onError
or onComplete
methods were invoked.onError(Throwable)
is called, the CompletableSubject
enters into a terminal state
and emits the same Throwable
instance to the last set of CompletableObserver
s. During this emission,
if one or more CompletableObserver
s dispose their respective Disposable
s, the
Throwable
is delivered to the global error handler via
RxJavaPlugins.onError(Throwable)
(multiple times if multiple CompletableObserver
s
cancel at once).
If there were no CompletableObserver
s 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
Modifier and Type | Method and Description |
---|---|
static CompletableSubject |
create()
Creates a fresh CompletableSubject.
|
Throwable |
getThrowable()
Returns the terminal error if this CompletableSubject has been terminated with an error, null otherwise.
|
boolean |
hasComplete()
Returns true if this CompletableSubject has been completed.
|
boolean |
hasObservers()
Returns true if this CompletableSubject has observers.
|
boolean |
hasThrowable()
Returns true if this CompletableSubject has been terminated with an error.
|
void |
onComplete()
Called once the deferred computation completes normally.
|
void |
onError(Throwable e)
Called once if the deferred computation 'throws' an exception.
|
void |
onSubscribe(Disposable d)
Called once by the Completable to set a Disposable on this instance which
then can be used to cancel the subscription at any time.
|
protected void |
subscribeActual(CompletableObserver observer)
Implement this method to handle the incoming
CompletableObserver s and
perform the business logic in your operator. |
amb, ambArray, ambWith, andThen, andThen, andThen, andThen, andThen, blockingAwait, blockingAwait, cache, complete, compose, concat, concat, concat, concatArray, concatWith, create, defer, delay, delay, delay, delaySubscription, delaySubscription, doAfterTerminate, doFinally, doOnComplete, doOnDispose, doOnError, doOnEvent, doOnSubscribe, doOnTerminate, error, error, fromAction, fromCallable, fromFuture, fromMaybe, fromObservable, fromPublisher, fromRunnable, fromSingle, fromSupplier, hide, lift, materialize, merge, merge, merge, mergeArray, mergeArrayDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeWith, never, observeOn, onErrorComplete, onErrorComplete, onErrorResumeNext, onTerminateDetach, repeat, repeat, repeatUntil, repeatWhen, retry, retry, retry, retry, retry, retryWhen, startWith, startWith, startWith, subscribe, subscribe, subscribe, subscribe, subscribeOn, subscribeWith, takeUntil, test, test, timeout, timeout, timeout, timeout, timer, timer, to, toFlowable, toMaybe, toObservable, toSingle, toSingleDefault, unsafeCreate, unsubscribeOn, using, using, wrap
@CheckReturnValue @NonNull public static CompletableSubject create()
public void onSubscribe(Disposable d)
CompletableObserver
onSubscribe
in interface CompletableObserver
d
- the Disposable instance to call dispose on for cancellation, not nullpublic void onError(Throwable e)
CompletableObserver
onError
in interface CompletableObserver
e
- the exception, not null.public void onComplete()
CompletableObserver
onComplete
in interface CompletableObserver
protected void subscribeActual(CompletableObserver observer)
Completable
CompletableObserver
s 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.
subscribeActual
in class Completable
observer
- the CompletableObserver instance, never null@Nullable public Throwable getThrowable()
public boolean hasThrowable()
public boolean hasComplete()
public boolean hasObservers()