|
31 | 31 | * <p>
|
32 | 32 | * <img width="640" height="415" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/S.BehaviorSubject.png" alt="">
|
33 | 33 | * <p>
|
| 34 | + * This subject does not have a public constructor by design; a new empty instance of this |
| 35 | + * {@code BehaviorSubject} can be created via the {@link #create()} method and |
| 36 | + * a new non-empty instance can be created via {@link #createDefault(Object)} (named as such to avoid |
| 37 | + * overload resolution conflict with {@code Observable.create} that creates an Observable, not a {@code BehaviorSubject}). |
| 38 | + * <p> |
| 39 | + * Since the {@code Subject} is conceptionally derived from the {@code Processor} type in the Reactive Streams specification, |
| 40 | + * {@code null}s are not allowed (<a href="https://github.com/reactive-streams/reactive-streams-jvm#2.13">Rule 2.13</a>) as |
| 41 | + * default initial values in {@link #createDefault(Object)} or as parameters to {@link #onNext(Object)} and |
| 42 | + * {@link #onError(Throwable)}. |
| 43 | + * <p> |
| 44 | + * Since a {@code BehaviorSubject} is an {@link io.reactivex.Observable}, it does not support backpressure. |
| 45 | + * <p> |
| 46 | + * When this {@code BehaviorSubject} is terminated via {@link #onError(Throwable)} or {@link #onComplete()}, the |
| 47 | + * last observed item (if any) is cleared and late {@link io.reactivex.Observer}s only receive |
| 48 | + * the respective terminal event. |
| 49 | + * <p> |
| 50 | + * The {@code BehaviorSubject} does not support clearing its cached value (to appear empty again), however, the |
| 51 | + * effect can be achieved by using a special item and making sure {@code Observer}s subscribe through a |
| 52 | + * filter whose predicate filters out this special item: |
| 53 | + * <pre><code> |
| 54 | + * BehaviorSubject<Integer> subject = BehaviorSubject.create(); |
| 55 | + * |
| 56 | + * final Integer EMPTY = Integer.MIN_VALUE; |
| 57 | + * |
| 58 | + * Observable<Integer> observable = subject.filter(v -> v != EMPTY); |
| 59 | + * |
| 60 | + * TestObserver<Integer> to1 = observable.test(); |
| 61 | + * |
| 62 | + * observable.onNext(1); |
| 63 | + * // this will "clear" the cache |
| 64 | + * observable.onNext(EMPTY); |
| 65 | + * |
| 66 | + * TestObserver<Integer> to2 = observable.test(); |
| 67 | + * |
| 68 | + * subject.onNext(2); |
| 69 | + * subject.onComplete(); |
| 70 | + * |
| 71 | + * // to1 received both non-empty items |
| 72 | + * to1.assertResult(1, 2); |
| 73 | + * |
| 74 | + * // to2 received only 2 even though the current item was EMPTY |
| 75 | + * // when it got subscribed |
| 76 | + * to2.assertResult(2); |
| 77 | + * |
| 78 | + * // Observers coming after the subject was terminated receive |
| 79 | + * // no items and only the onComplete event in this case. |
| 80 | + * observable.test().assertResult(); |
| 81 | + * </code></pre> |
| 82 | + * <p> |
| 83 | + * Even though {@code BehaviorSubject} implements the {@code Observer} interface, calling |
| 84 | + * {@code onSubscribe} is not required (<a href="https://github.com/reactive-streams/reactive-streams-jvm#2.12">Rule 2.12</a>) |
| 85 | + * if the subject is used as a standalone source. However, calling {@code onSubscribe} |
| 86 | + * after the {@code BehaviorSubjecct} reached its terminal state will result in the |
| 87 | + * given {@code Disposable} being disposed immediately. |
| 88 | + * <p> |
| 89 | + * Calling {@link #onNext(Object)}, {@link #onError(Throwable)} and {@link #onComplete()} |
| 90 | + * is still required to be serialized (called from the same thread or called non-overlappingly from different threads |
| 91 | + * through external means of serialization). The {@link #toSerialized()} method available to all {@code Subject}s |
| 92 | + * provides such serialization and also protects against reentrance (i.e., when a downstream {@code Observer} |
| 93 | + * consuming this subject also wants to call {@link #onNext(Object)} on this subject recursively). |
| 94 | + * <p> |
| 95 | + * This {@code BehaviorSubject} supports the standard state-peeking methods {@link #hasComplete()}, {@link #hasThrowable()}, |
| 96 | + * {@link #getThrowable()} and {@link #hasObservers()} as well as means to read the latest observed value |
| 97 | + * in a non-blocking and thread-safe manner via {@link #hasValue()}, {@link #getValue()}, |
| 98 | + * {@link #getValues()} or {@link #getValues(Object[])}. |
| 99 | + * <dl> |
| 100 | + * <dt><b>Scheduler:</b></dt> |
| 101 | + * <dd>{@code BehaviorSubject} does not operate by default on a particular {@link io.reactivex.Scheduler} and |
| 102 | + * the {@code Observer}s get notified on the thread the respective {@code onXXX} methods were invoked.</dd> |
| 103 | + * <dt><b>Error handling:</b></dt> |
| 104 | + * <dd>When the {@link #onError(Throwable)} is called, the {@code BehaviorSubject} enters into a terminal state |
| 105 | + * and emits the same {@code Throwable} instance to the last set of {@code Observer}s. During this emission, |
| 106 | + * if one or more {@code Observer}s dispose their respective {@code Disposable}s, the |
| 107 | + * {@code Throwable} is delivered to the global error handler via |
| 108 | + * {@link io.reactivex.plugins.RxJavaPlugins#onError(Throwable)} (multiple times if multiple {@code Observer}s |
| 109 | + * cancel at once). |
| 110 | + * If there were no {@code Observer}s subscribed to this {@code BehaviorSubject} when the {@code onError()} |
| 111 | + * was called, the global error handler is not invoked. |
| 112 | + * </dd> |
| 113 | + * </dl> |
| 114 | + * <p> |
34 | 115 | * Example usage:
|
35 | 116 | * <pre> {@code
|
36 | 117 |
|
|
0 commit comments