io.reactivex.rxjava3.subjects.AsyncSubject Maven / Gradle / Ivy
Show all versions of rxjava Show documentation
/*
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package io.reactivex.rxjava3.subjects;
import java.util.concurrent.atomic.AtomicReference;
import io.reactivex.rxjava3.annotations.*;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.internal.observers.DeferredScalarDisposable;
import io.reactivex.rxjava3.internal.util.ExceptionHelper;
import 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
* {@code AsyncSubject} can be created via the {@link #create()} method.
*
* Since a {@code Subject} is conceptionally derived from the {@code Processor} type in the Reactive Streams specification,
* {@code null}s are not allowed (Rule 2.13)
* as parameters to {@link #onNext(Object)} and {@link #onError(Throwable)}. Such calls will result in a
* {@link NullPointerException} being thrown and the subject's state is not changed.
*
* Since an {@code AsyncSubject} is an {@link io.reactivex.rxjava3.core.Observable}, it does not support backpressure.
*
* When this {@code AsyncSubject} is terminated via {@link #onError(Throwable)}, the
* last observed item (if any) is cleared and late {@link io.reactivex.rxjava3.core.Observer}s only receive
* the {@code onError} event.
*
* The {@code AsyncSubject} caches the latest item internally and it emits this item only when {@code onComplete} is called.
* Therefore, it is not recommended to use this {@code Subject} with infinite or never-completing sources.
*
* Even though {@code AsyncSubject} implements the {@code Observer} interface, calling
* {@code onSubscribe} is not required (Rule 2.12)
* if the subject is used as a standalone source. However, calling {@code onSubscribe}
* after the {@code AsyncSubject} reached its terminal state will result in the
* given {@code Disposable} being disposed immediately.
*
* Calling {@link #onNext(Object)}, {@link #onError(Throwable)} and {@link #onComplete()}
* is required to be serialized (called from the same thread or called non-overlappingly from different threads
* through external means of serialization). The {@link #toSerialized()} method available to all {@code Subject}s
* provides such serialization and also protects against reentrance (i.e., when a downstream {@code Observer}
* consuming this subject also wants to call {@link #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 {@code AsyncSubject} supports the standard state-peeking methods {@link #hasComplete()}, {@link #hasThrowable()},
* {@link #getThrowable()} and {@link #hasObservers()} as well as means to read the very last observed value -
* after this {@code AsyncSubject} has been completed - in a non-blocking and thread-safe
* manner via {@link #hasValue()} or {@link #getValue()}.
*
* - Scheduler:
* - {@code AsyncSubject} does not operate by default on a particular {@link io.reactivex.rxjava3.core.Scheduler} and
* the {@code Observer}s get notified on the thread where the terminating {@code onError} or {@code onComplete}
* methods were invoked.
* - Error handling:
* - When the {@link #onError(Throwable)} is called, the {@code AsyncSubject} enters into a terminal state
* and emits the same {@code Throwable} instance to the last set of {@code Observer}s. During this emission,
* if one or more {@code Observer}s dispose their respective {@code Disposable}s, the
* {@code Throwable} is delivered to the global error handler via
* {@link io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable)} (multiple times if multiple {@code Observer}s
* cancel at once).
* If there were no {@code Observer}s subscribed to this {@code AsyncSubject} when the {@code 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);
*
* @param the value type
*/
public final class AsyncSubject extends Subject {
@SuppressWarnings("rawtypes")
static final AsyncDisposable[] EMPTY = new AsyncDisposable[0];
@SuppressWarnings("rawtypes")
static final AsyncDisposable[] TERMINATED = new AsyncDisposable[0];
final AtomicReference[]> subscribers;
/** Write before updating subscribers, read after reading subscribers as TERMINATED. */
Throwable error;
/** Write before updating subscribers, read after reading subscribers as TERMINATED. */
T value;
/**
* Creates a new AsyncProcessor.
* @param the value type to be received and emitted
* @return the new AsyncProcessor instance
*/
@CheckReturnValue
@NonNull
public static AsyncSubject create() {
return new AsyncSubject<>();
}
/**
* Constructs an AsyncSubject.
* @since 2.0
*/
@SuppressWarnings("unchecked")
AsyncSubject() {
this.subscribers = new AtomicReference<>(EMPTY);
}
@Override
public void onSubscribe(Disposable d) {
if (subscribers.get() == TERMINATED) {
d.dispose();
}
}
@Override
public void onNext(T t) {
ExceptionHelper.nullCheck(t, "onNext called with a null value.");
if (subscribers.get() == TERMINATED) {
return;
}
value = t;
}
@SuppressWarnings("unchecked")
@Override
public void onError(Throwable t) {
ExceptionHelper.nullCheck(t, "onError called with a null Throwable.");
if (subscribers.get() == TERMINATED) {
RxJavaPlugins.onError(t);
return;
}
value = null;
error = t;
for (AsyncDisposable as : subscribers.getAndSet(TERMINATED)) {
as.onError(t);
}
}
@SuppressWarnings("unchecked")
@Override
public void onComplete() {
if (subscribers.get() == TERMINATED) {
return;
}
T v = value;
AsyncDisposable[] array = subscribers.getAndSet(TERMINATED);
if (v == null) {
for (AsyncDisposable as : array) {
as.onComplete();
}
} else {
for (AsyncDisposable as : array) {
as.complete(v);
}
}
}
@Override
@CheckReturnValue
public boolean hasObservers() {
return subscribers.get().length != 0;
}
@Override
@CheckReturnValue
public boolean hasThrowable() {
return subscribers.get() == TERMINATED && error != null;
}
@Override
@CheckReturnValue
public boolean hasComplete() {
return subscribers.get() == TERMINATED && error == null;
}
@Override
@CheckReturnValue
public Throwable getThrowable() {
return subscribers.get() == TERMINATED ? error : null;
}
@Override
protected void subscribeActual(Observer super T> observer) {
AsyncDisposable as = new AsyncDisposable<>(observer, this);
observer.onSubscribe(as);
if (add(as)) {
if (as.isDisposed()) {
remove(as);
}
} else {
Throwable ex = error;
if (ex != null) {
observer.onError(ex);
} else {
T v = value;
if (v != null) {
as.complete(v);
} else {
as.onComplete();
}
}
}
}
/**
* Tries to add the given subscriber to the subscribers array atomically
* or returns false if the subject has terminated.
* @param ps the subscriber to add
* @return true if successful, false if the subject has terminated
*/
boolean add(AsyncDisposable ps) {
for (;;) {
AsyncDisposable[] a = subscribers.get();
if (a == TERMINATED) {
return false;
}
int n = a.length;
@SuppressWarnings("unchecked")
AsyncDisposable[] b = new AsyncDisposable[n + 1];
System.arraycopy(a, 0, b, 0, n);
b[n] = ps;
if (subscribers.compareAndSet(a, b)) {
return true;
}
}
}
/**
* Atomically removes the given subscriber if it is subscribed to the subject.
* @param ps the subject to remove
*/
@SuppressWarnings("unchecked")
void remove(AsyncDisposable ps) {
for (;;) {
AsyncDisposable[] a = subscribers.get();
int n = a.length;
if (n == 0) {
return;
}
int j = -1;
for (int i = 0; i < n; i++) {
if (a[i] == ps) {
j = i;
break;
}
}
if (j < 0) {
return;
}
AsyncDisposable[] b;
if (n == 1) {
b = EMPTY;
} else {
b = new AsyncDisposable[n - 1];
System.arraycopy(a, 0, b, 0, j);
System.arraycopy(a, j + 1, b, j, n - j - 1);
}
if (subscribers.compareAndSet(a, b)) {
return;
}
}
}
/**
* Returns true if the subject has any value.
* The method is thread-safe.
* @return true if the subject has any value
*/
@CheckReturnValue
public boolean hasValue() {
return subscribers.get() == TERMINATED && value != null;
}
/**
* Returns a single value the Subject currently has or null if no such value exists.
*
The method is thread-safe.
* @return a single value the Subject currently has or null if no such value exists
*/
@Nullable
@CheckReturnValue
public T getValue() {
return subscribers.get() == TERMINATED ? value : null;
}
static final class AsyncDisposable extends DeferredScalarDisposable {
private static final long serialVersionUID = 5629876084736248016L;
final AsyncSubject parent;
AsyncDisposable(Observer super T> actual, AsyncSubject parent) {
super(actual);
this.parent = parent;
}
@Override
public void dispose() {
if (super.tryDispose()) {
parent.remove(this);
}
}
void onComplete() {
if (!isDisposed()) {
downstream.onComplete();
}
}
void onError(Throwable t) {
if (isDisposed()) {
RxJavaPlugins.onError(t);
} else {
downstream.onError(t);
}
}
}
}