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 thecreate()
method.Since the
CompletableSubject
is conceptionally derived from theProcessor
type in the Reactive Streams specification,null
s are not allowed (Rule 2.13) as parameters toonError(Throwable)
.Even though
CompletableSubject
implements theCompletableObserver
interface, callingonSubscribe
is not required (Rule 2.12) if the subject is used as a standalone source. However, callingonSubscribe
after theCompletableSubject
reached its terminal state will result in the givenDisposable
being disposed immediately.All methods are thread safe. Calling
onComplete()
multiple times has no effect. CallingonError(Throwable)
multiple times relays theThrowable
to theRxJavaPlugins.onError(Throwable)
global error handler.This
CompletableSubject
supports the standard state-peeking methodshasComplete()
,hasThrowable()
,getThrowable()
andhasObservers()
.- Scheduler:
CompletableSubject
does not operate by default on a particularScheduler
and theCompletableObserver
s get notified on the thread where the terminatingonError
oronComplete
methods were invoked.- Error handling:
- When the
onError(Throwable)
is called, theCompletableSubject
enters into a terminal state and emits the sameThrowable
instance to the last set ofCompletableObserver
s. During this emission, if one or moreCompletableObserver
s dispose their respectiveDisposable
s, theThrowable
is delivered to the global error handler viaRxJavaPlugins.onError(Throwable)
(multiple times if multipleCompletableObserver
s cancel at once). If there were noCompletableObserver
s subscribed to thisCompletableSubject
when theonError()
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
CompletableSubject.CompletableDisposable
-
Field Summary
Fields Modifier and Type Field Description (package private) static CompletableSubject.CompletableDisposable[]
EMPTY
(package private) java.lang.Throwable
error
(package private) java.util.concurrent.atomic.AtomicReference<CompletableSubject.CompletableDisposable[]>
observers
(package private) java.util.concurrent.atomic.AtomicBoolean
once
(package private) static CompletableSubject.CompletableDisposable[]
TERMINATED
-
Constructor Summary
Constructors Constructor Description CompletableSubject()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) boolean
add(CompletableSubject.CompletableDisposable inner)
static @NonNull CompletableSubject
create()
Creates a fresh CompletableSubject.@Nullable java.lang.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.(package private) int
observerCount()
Returns the number of current observers.void
onComplete()
Called once the deferred computation completes normally.void
onError(java.lang.Throwable e)
Called once if the deferred computation 'throws' an exception.void
onSubscribe(Disposable d)
Called once by theCompletable
to set aDisposable
on this instance which then can be used to cancel the subscription at any time.(package private) void
remove(CompletableSubject.CompletableDisposable inner)
protected void
subscribeActual(CompletableObserver observer)
Implement this method to handle the incomingCompletableObserver
s and perform the business logic in your operator.-
Methods inherited from class io.reactivex.rxjava3.core.Completable
amb, ambArray, ambWith, andThen, andThen, andThen, andThen, andThen, blockingAwait, blockingAwait, blockingSubscribe, blockingSubscribe, blockingSubscribe, blockingSubscribe, cache, complete, compose, concat, concat, concat, concatArray, concatArrayDelayError, concatDelayError, concatDelayError, concatDelayError, concatWith, create, defer, delay, delay, delay, delaySubscription, delaySubscription, doAfterTerminate, doFinally, doOnComplete, doOnDispose, doOnError, doOnEvent, doOnLifecycle, doOnSubscribe, doOnTerminate, error, error, fromAction, fromCallable, fromCompletionStage, fromFuture, fromMaybe, fromObservable, fromPublisher, fromRunnable, fromSingle, fromSupplier, hide, lift, materialize, merge, merge, merge, mergeArray, mergeArrayDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeWith, never, observeOn, onErrorComplete, onErrorComplete, onErrorResumeNext, onErrorResumeWith, onErrorReturn, onErrorReturnItem, onTerminateDetach, repeat, repeat, repeatUntil, repeatWhen, retry, retry, retry, retry, retry, retryUntil, retryWhen, safeSubscribe, sequenceEqual, startWith, startWith, startWith, startWith, startWith, subscribe, subscribe, subscribe, subscribe, subscribe, subscribeOn, subscribeWith, switchOnNext, switchOnNextDelayError, takeUntil, test, test, timeout, timeout, timeout, timeout, timer, timer, to, toCompletionStage, toFlowable, toFuture, toMaybe, toObservable, toSingle, toSingleDefault, unsafeCreate, unsubscribeOn, using, using, wrap
-
-
-
-
Field Detail
-
observers
final java.util.concurrent.atomic.AtomicReference<CompletableSubject.CompletableDisposable[]> observers
-
EMPTY
static final CompletableSubject.CompletableDisposable[] EMPTY
-
TERMINATED
static final CompletableSubject.CompletableDisposable[] TERMINATED
-
once
final java.util.concurrent.atomic.AtomicBoolean once
-
error
java.lang.Throwable error
-
-
Method Detail
-
create
@CheckReturnValue @NonNull public static @NonNull CompletableSubject create()
Creates a fresh CompletableSubject.- Returns:
- the new CompletableSubject instance
-
onSubscribe
public void onSubscribe(Disposable d)
Description copied from interface:CompletableObserver
Called once by theCompletable
to set aDisposable
on this instance which then can be used to cancel the subscription at any time.- Specified by:
onSubscribe
in interfaceCompletableObserver
- Parameters:
d
- theDisposable
instance to call dispose on for cancellation, not null
-
onError
public void onError(java.lang.Throwable e)
Description copied from interface:CompletableObserver
Called once if the deferred computation 'throws' an exception.- Specified by:
onError
in interfaceCompletableObserver
- Parameters:
e
- the exception, notnull
.
-
onComplete
public void onComplete()
Description copied from interface:CompletableObserver
Called once the deferred computation completes normally.- Specified by:
onComplete
in interfaceCompletableObserver
-
subscribeActual
protected void subscribeActual(CompletableObserver observer)
Description copied from class:Completable
Implement this method to handle the incomingCompletableObserver
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 theCompletableObserver
; all hooks and basic safeguards have been applied byCompletable.subscribe(CompletableObserver)
before this method gets called.- Specified by:
subscribeActual
in classCompletable
- Parameters:
observer
- theCompletableObserver
instance, nevernull
-
add
boolean add(CompletableSubject.CompletableDisposable inner)
-
remove
void remove(CompletableSubject.CompletableDisposable inner)
-
getThrowable
@Nullable public @Nullable java.lang.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
-
observerCount
int observerCount()
Returns the number of current observers.- Returns:
- the number of current observers
-
-