Top Description Inners Fields Constructors Methods
io.reactivex.rxjava3.subjects

public final Class AsyncSubject<T>

extends Subject<T>
Class Inheritance
Type Parameters
<T>
the value type
Imports
java.util.concurrent.atomic.AtomicReference, io.reactivex.rxjava3.annotations.*, io.reactivex.rxjava3.core.Observer, io.reactivex.rxjava3.disposables.Disposable, io.reactivex.rxjava3.internal.observers.DeferredScalarDisposable, io.reactivex.rxjava3.internal.util.ExceptionHelper, io.reactivex.rxjava3.plugins.RxJavaPlugins

A Subject that emits the very last value followed by a completion event or the received error to Observers.

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

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

Since an AsyncSubject is an io.reactivex.rxjava3.core.Observable, it does not support backpressure.

When this AsyncSubject is terminated via onError(Throwable), the last observed item (if any) is cleared and late io.reactivex.rxjava3.core.Observers only receive the onError event.

The AsyncSubject caches the latest item internally and it emits this item only when onComplete is called. Therefore, it is not recommended to use this Subject with infinite or never-completing sources.

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

Calling onNext(Object), onError(Throwable) and onComplete() is required to be serialized (called from the same thread or called non-overlappingly from different threads through external means of serialization). The toSerialized() method available to all Subjects provides such serialization and also protects against reentrance (i.e., when a downstream Observer consuming this subject also wants to call onNext(Object) on this subject recursively). The implementation of onXXX methods are technically thread-safe but non-serialized calls to them may lead to undefined state in the currently subscribed Observers.

This AsyncSubject supports the standard state-peeking methods hasComplete(), hasThrowable(), getThrowable() and hasObservers() as well as means to read the very last observed value - after this AsyncSubject has been completed - in a non-blocking and thread-safe manner via hasValue() or getValue().

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

Example usage:


AsyncSubject<Object> subject = AsyncSubject.create();

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

to1.assertEmpty();

subject.onNext(1);

// AsyncSubject only emits when onComplete was called.
to1.assertEmpty();

subject.onNext(2);
subject.onComplete();

// onComplete triggers the emission of the last cached item and the onComplete event.
to1.assertResult(2);

TestObserver<Object> to2 = subject.test();

// late Observers receive the last cached item too
to2.assertResult(2);

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
pack-priv static final AsyncSubject.AsyncDisposable<T>[]
pack-priv Throwable
error

Write before updating subscribers, read after reading subscribers as TERMINATED.

pack-priv final AtomicReference<AsyncSubject.AsyncDisposable<T>[]>
pack-priv static final AsyncSubject.AsyncDisposable<T>[]
pack-priv T
value

Write before updating subscribers, read after reading subscribers as TERMINATED.

Constructor Summary

AccessConstructor and Description
pack-priv
AsyncSubject()

Constructs an AsyncSubject.

Method Summary

Modifier and TypeMethod and Description
pack-priv boolean

Returns:

true if successful, false if the subject has terminated
add
(AsyncSubject.AsyncDisposable<T>
the subscriber to add
ps
)

Tries to add the given subscriber to the subscribers array atomically or returns false if the subject has terminated.

public static <
the value type to be received and emitted
T
>
@NonNull AsyncSubject<T>

Returns:

the new AsyncProcessor instance
create
()

Creates a new AsyncProcessor.

public Throwable
getThrowable()

Implements abstract io.reactivex.rxjava3.subjects.Subject.getThrowable.

Returns the error that caused the Subject to terminate or null if the Subject hasn't terminated yet.
public T

Returns:

a single value the Subject currently has or null if no such value exists
getValue
()

Returns a single value the Subject currently has or null if no such value exists.

public boolean
hasComplete()

Implements abstract io.reactivex.rxjava3.subjects.Subject.hasComplete.

Returns true if the subject has reached a terminal state through a complete event.
public boolean
hasObservers()

Implements abstract io.reactivex.rxjava3.subjects.Subject.hasObservers.

Returns true if the subject has any Observers.
public boolean
hasThrowable()

Implements abstract io.reactivex.rxjava3.subjects.Subject.hasThrowable.

Returns true if the subject has reached a terminal state through an error event.
public boolean

Returns:

true if the subject has any value
hasValue
()

Returns true if the subject has any value.

public void
onComplete()

Implements io.reactivex.rxjava3.core.Observer.onComplete.

Notifies the Observer that the Observable has finished sending push-based notifications.
public void
public void
onNext(T
the item emitted by the Observable
t
)

Implements io.reactivex.rxjava3.core.Observer.onNext.

Provides the Observer with a new item to observe.
public void
pack-priv void
remove(AsyncSubject.AsyncDisposable<T>
the subject to remove
ps
)

Atomically removes the given subscriber if it is subscribed to the subject.

protected void
subscribeActual(Observer<? super T> observer)

Inherited from io.reactivex.rxjava3.subjects.Subject:
toSerialized

Field Detail

EMPTYback to summary
pack-priv static final AsyncSubject.AsyncDisposable<T>[] EMPTY
Annotations
@SuppressWarnings:rawtypes
errorback to summary
pack-priv Throwable error

Write before updating subscribers, read after reading subscribers as TERMINATED.

subscribersback to summary
pack-priv final AtomicReference<AsyncSubject.AsyncDisposable<T>[]> subscribers
TERMINATEDback to summary
pack-priv static final AsyncSubject.AsyncDisposable<T>[] TERMINATED
Annotations
@SuppressWarnings:rawtypes
valueback to summary
pack-priv T value

Write before updating subscribers, read after reading subscribers as TERMINATED.

Constructor Detail

AsyncSubjectback to summary
pack-priv AsyncSubject()

Constructs an AsyncSubject.

Annotations
@SuppressWarnings:unchecked
Since
2.0

Method Detail

addback to summary
pack-priv boolean add(AsyncSubject.AsyncDisposable<T> ps)

Tries to add the given subscriber to the subscribers array atomically or returns false if the subject has terminated.

Parameters
ps:AsyncSubject.AsyncDisposable<T>

the subscriber to add

Returns:boolean

true if successful, false if the subject has terminated

createback to summary
public static <T> @NonNull AsyncSubject<T> create()

Creates a new AsyncProcessor.

Parameters
<T>
the value type to be received and emitted
Returns:@NonNull AsyncSubject<T>

the new AsyncProcessor instance

Annotations
@CheckReturnValue
@NonNull
getThrowableback to summary
public Throwable getThrowable()

Implements abstract io.reactivex.rxjava3.subjects.Subject.getThrowable.

Doc from io.reactivex.rxjava3.subjects.Subject.getThrowable.

Returns the error that caused the Subject to terminate or null if the Subject hasn't terminated yet.

The method is thread-safe.

Returns:Throwable

the error that caused the Subject to terminate or null if the Subject hasn't terminated yet

Annotations
@Override
@CheckReturnValue
getValueback to summary
public T getValue()

Returns a single value the Subject currently has or null if no such value exists.

The method is thread-safe.

Returns:T

a single value the Subject currently has or null if no such value exists

Annotations
@Nullable
@CheckReturnValue
hasCompleteback to summary
public boolean hasComplete()

Implements abstract io.reactivex.rxjava3.subjects.Subject.hasComplete.

Doc from io.reactivex.rxjava3.subjects.Subject.hasComplete.

Returns true if the subject has reached a terminal state through a complete event.

The method is thread-safe.

Returns:boolean

true if the subject has reached a terminal state through a complete event

Annotations
@Override
@CheckReturnValue
hasObserversback to summary
public boolean hasObservers()

Implements abstract io.reactivex.rxjava3.subjects.Subject.hasObservers.

Doc from io.reactivex.rxjava3.subjects.Subject.hasObservers.

Returns true if the subject has any Observers.

The method is thread-safe.

Returns:boolean

true if the subject has any Observers

Annotations
@Override
@CheckReturnValue
hasThrowableback to summary
public boolean hasThrowable()

Implements abstract io.reactivex.rxjava3.subjects.Subject.hasThrowable.

Doc from io.reactivex.rxjava3.subjects.Subject.hasThrowable.

Returns true if the subject has reached a terminal state through an error event.

The method is thread-safe.

Returns:boolean

true if the subject has reached a terminal state through an error event

Annotations
@Override
@CheckReturnValue
hasValueback to summary
public boolean hasValue()

Returns true if the subject has any value.

The method is thread-safe.

Returns:boolean

true if the subject has any value

Annotations
@CheckReturnValue
onCompleteback to summary
public void onComplete()

Implements io.reactivex.rxjava3.core.Observer.onComplete.

Doc from io.reactivex.rxjava3.core.Observer.onComplete.

Notifies the Observer that the Observable has finished sending push-based notifications.

The Observable will not call this method if it calls onError.

Annotations
@SuppressWarnings:unchecked
@Override
onErrorback to summary
public void onError(Throwable t)
Annotations
@SuppressWarnings:unchecked
@Override
onNextback to summary
public void onNext(T t)

Implements io.reactivex.rxjava3.core.Observer.onNext.

Doc from io.reactivex.rxjava3.core.Observer.onNext.

Provides the Observer with a new item to observe.

The Observable may call this method 0 or more times.

The Observable will not call this method again after it calls either onComplete or onError.

Parameters
t:T

the item emitted by the Observable

Annotations
@Override
onSubscribeback to summary
public void onSubscribe(Disposable d)
Annotations
@Override
removeback to summary
pack-priv void remove(AsyncSubject.AsyncDisposable<T> ps)

Atomically removes the given subscriber if it is subscribed to the subject.

Parameters
ps:AsyncSubject.AsyncDisposable<T>

the subject to remove

Annotations
@SuppressWarnings:unchecked
subscribeActualback to summary
protected void subscribeActual(Observer<? super T> observer)
Annotations
@Override
io.reactivex.rxjava3.subjects back to summary

pack-priv final Class AsyncSubject.AsyncDisposable<T>

extends DeferredScalarDisposable<T>
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final AsyncSubject<T>
private static final long
Inherited from io.reactivex.rxjava3.internal.observers.DeferredScalarDisposable:
downstreamvalue

Constructor Summary

AccessConstructor and Description
pack-priv
AsyncDisposable(Observer<? super T> actual, AsyncSubject<T> parent)

Method Summary

Modifier and TypeMethod and Description
public void
pack-priv void
pack-priv void
Inherited from io.reactivex.rxjava3.internal.observers.DeferredScalarDisposable:
clearcompletecompleteerrorisDisposedisEmptypollrequestFusiontryDispose

Field Detail

parentback to summary
pack-priv final AsyncSubject<T> parent
serialVersionUIDback to summary
private static final long serialVersionUID

Hides io.reactivex.rxjava3.internal.observers.DeferredScalarDisposable.serialVersionUID.

Constructor Detail

AsyncDisposableback to summary
pack-priv AsyncDisposable(Observer<? super T> actual, AsyncSubject<T> parent)

Method Detail

disposeback to summary
public void dispose()

Overrides io.reactivex.rxjava3.internal.observers.DeferredScalarDisposable.dispose.

Implements io.reactivex.rxjava3.disposables.Disposable.dispose.

Doc from io.reactivex.rxjava3.disposables.Disposable.dispose.

Dispose the resource, the operation should be idempotent.

Annotations
@Override
onCompleteback to summary
pack-priv void onComplete()
onErrorback to summary
pack-priv void onError(Throwable t)