Observers and terminal events to current
or late Observers.
This subject does not have a public constructor by design; a new empty instance of this
PublishSubject 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 a PublishSubject is an io., it does not support backpressure.
When this PublishSubject is terminated via onError(Throwable) or onComplete(),
late io.s only receive the respective terminal event.
Unlike a BehaviorSubject, a PublishSubject doesn't retain/cache items, therefore, a new
Observer won't receive any past items.
Even though PublishSubject 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 PublishSubject 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).
This PublishSubject supports the standard state-peeking methods hasComplete(), hasThrowable(),
getThrowable() and hasObservers().
PublishSubject does not operate by default on a particular io.reactivex.rxjava3.core.Scheduler and
the Observers get notified on the thread the respective onXXX methods were invoked.onError(Throwable) is called, the PublishSubject 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 PublishSubject when the onError()
was called, the global error handler is not invoked.
Example usage:
PublishSubject<Object> subject = PublishSubject.create();
// observer1 will receive all onNext and onComplete events
subject.subscribe(observer1);
subject.onNext("one");
subject.onNext("two");
// observer2 will only receive "three" and onComplete
subject.subscribe(observer2);
subject.onNext("three");
subject.onComplete();
// late Observers only receive the terminal event
subject.test().assertEmpty();
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | PublishSubject.PublishDisposable<
the value type T>Wraps the actual subscriber, tracks its requests and makes cancellation to remove itself from the current subscribers array. |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv static final PublishSubject. | EMPTY
An empty subscribers array to avoid allocating it all the time. |
| pack-priv Throwable | error
The error, write before terminating and read after checking subscribers. |
| pack-priv final AtomicReference | subscribers
The array of currently subscribed subscribers. |
| pack-priv static final PublishSubject. | TERMINATED
The terminated indicator for the subscribers array. |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv boolean | Returns: true if successful, false if the subject has terminatedthe subscriber to add psTries to add the given subscriber to the subscribers array atomically or returns false if the subject has terminated. |
| public static < the value type T> @NonNull PublishSubject | |
| public @Nullable Throwable | getThrowable()
Implements abstract io. |
| public boolean | hasComplete()
Implements abstract io. |
| public boolean | hasObservers()
Implements abstract io. |
| public boolean | hasThrowable()
Implements abstract io. |
| public void | onComplete()
Implements io. 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. Observer with a new item to observe.
|
| public void | |
| pack-priv void | remove(PublishSubject.
the subject to remove psAtomically removes the given subscriber if it is subscribed to the subject. |
| protected void |
| EMPTY | back to summary |
|---|---|
| pack-priv static final PublishSubject. An empty subscribers array to avoid allocating it all the time.
| |
| error | back to summary |
|---|---|
| pack-priv Throwable error The error, write before terminating and read after checking subscribers. | |
| subscribers | back to summary |
|---|---|
| pack-priv final AtomicReference<PublishSubject. The array of currently subscribed subscribers. | |
| TERMINATED | back to summary |
|---|---|
| pack-priv static final PublishSubject. The terminated indicator for the subscribers array.
| |
| PublishSubject | back to summary |
|---|---|
| pack-priv PublishSubject() Constructs a PublishSubject.
| |
| add | back to summary |
|---|---|
| pack-priv boolean add(PublishSubject. Tries to add the given subscriber to the subscribers array atomically or returns false if the subject has terminated.
| |
| create | back to summary |
|---|---|
| public static <T> @NonNull PublishSubject Constructs a PublishSubject.
| |
| getThrowable | back to summary |
|---|---|
| public @Nullable Throwable getThrowable() Implements abstract io. Doc from io. Returns the error that caused the Subject to terminate or null if the Subject hasn't terminated yet. The method is thread-safe. | |
| hasComplete | back to summary |
|---|---|
| public boolean hasComplete() Implements abstract io. Doc from io. Returns true if the subject has reached a terminal state through a complete event. The method is thread-safe.
| |
| hasObservers | back to summary |
|---|---|
| public boolean hasObservers() Implements abstract io. Doc from io. Returns true if the subject has any Observers. The method is thread-safe.
| |
| hasThrowable | back to summary |
|---|---|
| public boolean hasThrowable() Implements abstract io. Doc from io. Returns true if the subject has reached a terminal state through an error event. The method is thread-safe.
| |
| onComplete | back to summary |
|---|---|
| public void onComplete() Implements io. Doc from io. Notifies the
The
| |
| onError | back to summary |
|---|---|
public void onError(Throwable t)
| |
| onNext | back to summary |
|---|---|
| public void onNext(T t) Implements io. Doc from io. Provides the
The
The
| |
| onSubscribe | back to summary |
|---|---|
public void onSubscribe(Disposable d)
| |
| remove | back to summary |
|---|---|
| pack-priv void remove(PublishSubject. Atomically removes the given subscriber if it is subscribed to the subject.
| |
| subscribeActual | back to summary |
|---|---|
protected void subscribeActual(Observer<? super T> t)
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final Observer | downstream
The actual subscriber. |
| pack-priv final PublishSubject | parent
The subject state. |
| private static final long |
| Access | Constructor and Description |
|---|---|
| pack-priv | PublishDisposable(Observer<? super T>
the actual subscriber actual, PublishSubject<T> the parent PublishProcessor parent)Constructs a PublishSubscriber, wraps the actual subscriber and the state. |
| Modifier and Type | Method and Description |
|---|---|
| public void | dispose()
Implements io. |
| public boolean | isDisposed()
Implements io. |
| public void | |
| public void | |
| public void |
| downstream | back to summary |
|---|---|
| pack-priv final Observer<? super T> downstream The actual subscriber. | |
| parent | back to summary |
|---|---|
| pack-priv final PublishSubject<T> parent The subject state. | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Hides java. | |
| PublishDisposable | back to summary |
|---|---|
| pack-priv PublishDisposable(Observer<? super T> actual, PublishSubject<T> parent) Constructs a PublishSubscriber, wraps the actual subscriber and the state.
| |
| dispose | back to summary |
|---|---|
| public void dispose() Implements io. Doc from io. Dispose the resource, the operation should be idempotent.
| |
| isDisposed | back to summary |
|---|---|
| public boolean isDisposed() Implements io. Doc from io. Returns true if this resource has been disposed.
| |
| onComplete | back to summary |
|---|---|
| public void onComplete() | |
| onError | back to summary |
|---|---|
| public void onError(Throwable t) | |
| onNext | back to summary |
|---|---|
| public void onNext(T t) | |