|
24 | 24 | import io.reactivex.plugins.RxJavaPlugins; |
25 | 25 |
|
26 | 26 | /** |
27 | | - * An Subject that emits the very last value followed by a completion event or the received error to Observers. |
28 | | - * |
29 | | - * <p>The implementation of onXXX methods are technically thread-safe but non-serialized calls |
| 27 | + * A Subject that emits the very last value followed by a completion event or the received error to Observers. |
| 28 | + * <p> |
| 29 | + * This subject does not have a public constructor by design; a new empty instance of this |
| 30 | + * {@code AsyncSubject} can be created via the {@link #create()} method. |
| 31 | + * <p> |
| 32 | + * Since a {@code Subject} is conceptionally derived from the {@code Processor} type in the Reactive Streams specification, |
| 33 | + * {@code null}s are not allowed (<a href="https://github.com/reactive-streams/reactive-streams-jvm#2.13">Rule 2.13</a>) |
| 34 | + * as parameters to {@link #onNext(Object)} and {@link #onError(Throwable)}. Such calls will result in a |
| 35 | + * {@link NullPointerException} being thrown and the subject's state is not changed. |
| 36 | + * <p> |
| 37 | + * Since an {@code AsyncSubject} is an {@link io.reactivex.Observable}, it does not support backpressure. |
| 38 | + * <p> |
| 39 | + * When this {@code AsyncSubject} is terminated via {@link #onError(Throwable)}, the |
| 40 | + * last observed item (if any) is cleared and late {@link io.reactivex.Observer}s only receive |
| 41 | + * the {@code onError} event. |
| 42 | + * <p> |
| 43 | + * The {@code AsyncSubject} caches the latest item internally and it emits this item only when {@code onComplete} is called. |
| 44 | + * Therefore, it is not recommended to use this {@code Subject} with infinite or never-completing sources. |
| 45 | + * <p> |
| 46 | + * Even though {@code AsyncSubject} implements the {@code Observer} interface, calling |
| 47 | + * {@code onSubscribe} is not required (<a href="https://github.com/reactive-streams/reactive-streams-jvm#2.12">Rule 2.12</a>) |
| 48 | + * if the subject is used as a standalone source. However, calling {@code onSubscribe} |
| 49 | + * after the {@code AsyncSubject} reached its terminal state will result in the |
| 50 | + * given {@code Disposable} being disposed immediately. |
| 51 | + * <p> |
| 52 | + * Calling {@link #onNext(Object)}, {@link #onError(Throwable)} and {@link #onComplete()} |
| 53 | + * is required to be serialized (called from the same thread or called non-overlappingly from different threads |
| 54 | + * through external means of serialization). The {@link #toSerialized()} method available to all {@code Subject}s |
| 55 | + * provides such serialization and also protects against reentrance (i.e., when a downstream {@code Observer} |
| 56 | + * consuming this subject also wants to call {@link #onNext(Object)} on this subject recursively). |
| 57 | + * The implementation of onXXX methods are technically thread-safe but non-serialized calls |
30 | 58 | * to them may lead to undefined state in the currently subscribed Observers. |
| 59 | + * <p> |
| 60 | + * This {@code AsyncSubject} supports the standard state-peeking methods {@link #hasComplete()}, {@link #hasThrowable()}, |
| 61 | + * {@link #getThrowable()} and {@link #hasObservers()} as well as means to read the very last observed value - |
| 62 | + * after this {@code AsyncSubject} has been completed - in a non-blocking and thread-safe |
| 63 | + * manner via {@link #hasValue()}, {@link #getValue()}, {@link #getValues()} or {@link #getValues(Object[])}. |
| 64 | + * <dl> |
| 65 | + * <dt><b>Scheduler:</b></dt> |
| 66 | + * <dd>{@code AsyncSubject} does not operate by default on a particular {@link io.reactivex.Scheduler} and |
| 67 | + * the {@code Observer}s get notified on the thread where the terminating {@code onError} or {@code onComplete} |
| 68 | + * methods were invoked.</dd> |
| 69 | + * <dt><b>Error handling:</b></dt> |
| 70 | + * <dd>When the {@link #onError(Throwable)} is called, the {@code AsyncSubject} enters into a terminal state |
| 71 | + * and emits the same {@code Throwable} instance to the last set of {@code Observer}s. During this emission, |
| 72 | + * if one or more {@code Observer}s dispose their respective {@code Disposable}s, the |
| 73 | + * {@code Throwable} is delivered to the global error handler via |
| 74 | + * {@link io.reactivex.plugins.RxJavaPlugins#onError(Throwable)} (multiple times if multiple {@code Observer}s |
| 75 | + * cancel at once). |
| 76 | + * If there were no {@code Observer}s subscribed to this {@code AsyncSubject} when the {@code onError()} |
| 77 | + * was called, the global error handler is not invoked. |
| 78 | + * </dd> |
| 79 | + * </dl> |
| 80 | + * <p> |
| 81 | + * Example usage: |
| 82 | + * <pre><code> |
| 83 | + * AsyncSubject<Object> subject = AsyncSubject.create(); |
| 84 | + * |
| 85 | + * TestObserver<Object> to1 = subject.test(); |
| 86 | + * |
| 87 | + * to1.assertEmpty(); |
| 88 | + * |
| 89 | + * subject.onNext(1); |
| 90 | + * |
| 91 | + * // AsyncSubject only emits when onComplete was called. |
| 92 | + * to1.assertEmpty(); |
31 | 93 | * |
| 94 | + * subject.onNext(2); |
| 95 | + * subject.onComplete(); |
| 96 | + * |
| 97 | + * // onComplete triggers the emission of the last cached item and the onComplete event. |
| 98 | + * to1.assertResult(2); |
| 99 | + * |
| 100 | + * TestObserver<Object> to2 = subject.test(); |
| 101 | + * |
| 102 | + * // late Observers receive the last cached item too |
| 103 | + * to2.assertResult(2); |
| 104 | + * </code></pre> |
32 | 105 | * @param <T> the value type |
33 | 106 | */ |
34 | | - |
35 | 107 | public final class AsyncSubject<T> extends Subject<T> { |
36 | 108 |
|
37 | 109 | @SuppressWarnings("rawtypes") |
|
0 commit comments