Observers.
This subject does not have a public constructor by design; a new empty instance of this
ReplaySubject can be created via the following create methods that
allow specifying the retention policy for items:
create() - creates an empty, unbounded ReplaySubject that
caches all items and the terminal event it receives.
create(int) - creates an empty, unbounded ReplaySubject
with a hint about how many total items one expects to retain.
createWithSize(int) - creates an empty, size-bound ReplaySubject
that retains at most the given number of the latest item it receives.
createWithTime(long, TimeUnit, Scheduler) - creates an empty, time-bound
ReplaySubject that retains items no older than the specified time amount.
createWithTimeAndSize(long, TimeUnit, Scheduler, int) - creates an empty,
time- and size-bound ReplaySubject that retains at most the given number
items that are also not older than the specified time amount.
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 ReplaySubject is an io., it does not support backpressure.
When this ReplaySubject is terminated via onError(Throwable) or onComplete(),
late io.s will receive the retained/cached items first (if any) followed by the respective
terminal event. If the ReplaySubject has a time-bound, the age of the retained/cached items are still considered
when replaying and thus it may result in no items being emitted before the terminal event.
Once an Observer has subscribed, it will receive items continuously from that point on. Bounds only affect how
many past items a new Observer will receive before it catches up with the live event feed.
Even though ReplaySubject 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 ReplaySubject 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 ReplaySubject supports the standard state-peeking methods hasComplete(), hasThrowable(),
getThrowable() and hasObservers() as well as means to read the retained/cached items
in a non-blocking and thread-safe manner via hasValue(), getValue(),
getValues() or getValues(Object[]).
Note that due to concurrency requirements, a size- and time-bounded ReplaySubject may hold strong references to more
source emissions than specified while it isn't terminated yet. Use the cleanupBuffer() to allow
such inaccessible items to be cleaned up by GC once no consumer references it anymore.
ReplaySubject 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.
Time-bound ReplaySubjects use the given Scheduler in their create methods
as time source to timestamp of items received for the age checks.onError(Throwable) is called, the ReplaySubject 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 ReplaySubject when the onError()
was called, the global error handler is not invoked.
Example usage:
ReplaySubject<Object> subject = ReplaySubject.create();
subject.onNext("one");
subject.onNext("two");
subject.onNext("three");
subject.onComplete();
// both of the following will get the onNext/onComplete calls from above
subject.subscribe(observer1);
subject.subscribe(observer2);
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class | |
| pack-priv static interface | ReplaySubject.ReplayBuffer<
the value type T>Abstraction over a buffer that receives events and replays them to individual Observers. |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class | |
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final ReplaySubject. | |
| pack-priv boolean | |
| pack-priv static final ReplaySubject. | |
| private static final Object[] | EMPTY_ARRAY
An empty array to avoid allocation in getValues(). |
| pack-priv final AtomicReference | |
| pack-priv static final ReplaySubject. |
| Access | Constructor and Description |
|---|---|
| pack-priv | ReplaySubject(ReplaySubject.
the ReplayBuffer instance, not null (not verified) bufferConstructs a ReplayProcessor with the given custom ReplayBuffer instance. |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv boolean | |
| public void | cleanupBuffer()
Makes sure the item cached by the head node in a bounded ReplaySubject is released (as it is never part of a replay). |
| public static < the type of items observed and emitted by the Subject T> @NonNull ReplaySubject | |
| public static < the type of items observed and emitted by the Subject T> @NonNull ReplaySubject | Returns: the created subjectthe initial buffer capacity capacityHint)Creates an unbounded replay subject with the specified initial buffer capacity. |
| pack-priv static < the type of items observed and emitted by the Subject T> ReplaySubject | Returns: the created subjectCreates an unbounded replay subject with the bounded-implementation for testing purposes. |
| public static < the type of items observed and emitted by the Subject T> @NonNull ReplaySubject | Returns: the created subjectthe maximum number of buffered items maxSize)Creates a size-bounded replay subject. |
| public static < the type of items observed and emitted by the Subject T> @NonNull ReplaySubject | |
| public static < the type of items observed and emitted by the Subject T> @NonNull ReplaySubject | Returns: the created subjectthe maximum age of the contained items maxAge, @NonNull TimeUnit the time unit of unit, @NonNull Scheduler timethe scheduler, int Scheduler that provides the current timethe maximum number of buffered items maxSize)Creates a time- and size-bounded replay subject. |
| public @Nullable Throwable | getThrowable()
Implements abstract io. |
| public T | Returns: a single value the Subject currently has or null if no such value existsReturns a single value the Subject currently has or null if no such value exists. |
| public Object[] | Returns: the array containing the snapshot of all values of the SubjectReturns an Object array containing snapshot all values of the Subject. |
| public T[] | Returns: the given array if the values fit into it or a new array containing all valuesthe target array to copy values into if it fits array)Returns a typed array containing a snapshot of all values of the Subject. |
| public boolean | hasComplete()
Implements abstract io. |
| public boolean | hasObservers()
Implements abstract io. |
| public boolean | hasThrowable()
Implements abstract io. |
| public boolean | |
| pack-priv int | |
| 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 | |
| pack-priv int | |
| protected void | |
| pack-priv ReplaySubject. |