-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathAsyncSubject.java
395 lines (357 loc) · 13.7 KB
/
AsyncSubject.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/**
* 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.subjects;
import io.reactivex.annotations.Nullable;
import io.reactivex.annotations.NonNull;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicReference;
import io.reactivex.Observer;
import io.reactivex.annotations.CheckReturnValue;
import io.reactivex.disposables.Disposable;
import io.reactivex.internal.functions.ObjectHelper;
import io.reactivex.internal.observers.DeferredScalarDisposable;
import io.reactivex.plugins.RxJavaPlugins;
/**
* A Subject that emits the very last value followed by a completion event or the received error to Observers.
* <p>
* <img width="640" height="239" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/AsyncSubject.png" alt="">
* <p>
* 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.
* <p>
* Since a {@code Subject} is conceptionally derived from the {@code Processor} type in the Reactive Streams specification,
* {@code null}s are not allowed (<a href="https://github.com/reactive-streams/reactive-streams-jvm#2.13">Rule 2.13</a>)
* 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.
* <p>
* Since an {@code AsyncSubject} is an {@link io.reactivex.Observable}, it does not support backpressure.
* <p>
* When this {@code AsyncSubject} is terminated via {@link #onError(Throwable)}, the
* last observed item (if any) is cleared and late {@link io.reactivex.Observer}s only receive
* the {@code onError} event.
* <p>
* 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.
* <p>
* Even though {@code AsyncSubject} implements the {@code Observer} interface, calling
* {@code onSubscribe} is not required (<a href="https://github.com/reactive-streams/reactive-streams-jvm#2.12">Rule 2.12</a>)
* 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.
* <p>
* 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.
* <p>
* 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()}, {@link #getValue()}, {@link #getValues()} or {@link #getValues(Object[])}.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code AsyncSubject} does not operate by default on a particular {@link io.reactivex.Scheduler} and
* the {@code Observer}s get notified on the thread where the terminating {@code onError} or {@code onComplete}
* methods were invoked.</dd>
* <dt><b>Error handling:</b></dt>
* <dd>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.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.
* </dd>
* </dl>
* <p>
* Example usage:
* <pre><code>
* 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);
* </code></pre>
* @param <T> the value type
*/
public final class AsyncSubject<T> extends Subject<T> {
@SuppressWarnings("rawtypes")
static final AsyncDisposable[] EMPTY = new AsyncDisposable[0];
@SuppressWarnings("rawtypes")
static final AsyncDisposable[] TERMINATED = new AsyncDisposable[0];
final AtomicReference<AsyncDisposable<T>[]> 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 <T> the value type to be received and emitted
* @return the new AsyncProcessor instance
*/
@CheckReturnValue
@NonNull
public static <T> AsyncSubject<T> create() {
return new AsyncSubject<T>();
}
/**
* Constructs an AsyncSubject.
* @since 2.0
*/
@SuppressWarnings("unchecked")
AsyncSubject() {
this.subscribers = new AtomicReference<AsyncDisposable<T>[]>(EMPTY);
}
@Override
public void onSubscribe(Disposable s) {
if (subscribers.get() == TERMINATED) {
s.dispose();
}
}
@Override
public void onNext(T t) {
ObjectHelper.requireNonNull(t, "onNext called with null. Null values are generally not allowed in 2.x operators and sources.");
if (subscribers.get() == TERMINATED) {
return;
}
value = t;
}
@SuppressWarnings("unchecked")
@Override
public void onError(Throwable t) {
ObjectHelper.requireNonNull(t, "onError called with null. Null values are generally not allowed in 2.x operators and sources.");
if (subscribers.get() == TERMINATED) {
RxJavaPlugins.onError(t);
return;
}
value = null;
error = t;
for (AsyncDisposable<T> as : subscribers.getAndSet(TERMINATED)) {
as.onError(t);
}
}
@SuppressWarnings("unchecked")
@Override
public void onComplete() {
if (subscribers.get() == TERMINATED) {
return;
}
T v = value;
AsyncDisposable<T>[] array = subscribers.getAndSet(TERMINATED);
if (v == null) {
for (AsyncDisposable<T> as : array) {
as.onComplete();
}
} else {
for (AsyncDisposable<T> as : array) {
as.complete(v);
}
}
}
@Override
public boolean hasObservers() {
return subscribers.get().length != 0;
}
@Override
public boolean hasThrowable() {
return subscribers.get() == TERMINATED && error != null;
}
@Override
public boolean hasComplete() {
return subscribers.get() == TERMINATED && error == null;
}
@Override
public Throwable getThrowable() {
return subscribers.get() == TERMINATED ? error : null;
}
@Override
protected void subscribeActual(Observer<? super T> s) {
AsyncDisposable<T> as = new AsyncDisposable<T>(s, this);
s.onSubscribe(as);
if (add(as)) {
if (as.isDisposed()) {
remove(as);
}
} else {
Throwable ex = error;
if (ex != null) {
s.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<T> ps) {
for (;;) {
AsyncDisposable<T>[] a = subscribers.get();
if (a == TERMINATED) {
return false;
}
int n = a.length;
@SuppressWarnings("unchecked")
AsyncDisposable<T>[] 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<T> ps) {
for (;;) {
AsyncDisposable<T>[] 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<T>[] 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.
* <p>The method is thread-safe.
* @return true if the subject has any value
*/
public boolean hasValue() {
return subscribers.get() == TERMINATED && value != null;
}
/**
* Returns a single value the Subject currently has or null if no such value exists.
* <p>The method is thread-safe.
* @return a single value the Subject currently has or null if no such value exists
*/
@Nullable
public T getValue() {
return subscribers.get() == TERMINATED ? value : null;
}
/**
* Returns an Object array containing snapshot all values of the Subject.
* <p>The method is thread-safe.
* @return the array containing the snapshot of all values of the Subject
* @deprecated in 2.1.14; put the result of {@link #getValue()} into an array manually, will be removed in 3.x
*/
@Deprecated
public Object[] getValues() {
T v = getValue();
return v != null ? new Object[] { v } : new Object[0];
}
/**
* Returns a typed array containing a snapshot of all values of the Subject.
* <p>The method follows the conventions of Collection.toArray by setting the array element
* after the last value to null (if the capacity permits).
* <p>The method is thread-safe.
* @param array the target array to copy values into if it fits
* @return the given array if the values fit into it or a new array containing all values
* @deprecated in 2.1.14; put the result of {@link #getValue()} into an array manually, will be removed in 3.x
*/
@Deprecated
public T[] getValues(T[] array) {
T v = getValue();
if (v == null) {
if (array.length != 0) {
array[0] = null;
}
return array;
}
if (array.length == 0) {
array = Arrays.copyOf(array, 1);
}
array[0] = v;
if (array.length != 1) {
array[1] = null;
}
return array;
}
static final class AsyncDisposable<T> extends DeferredScalarDisposable<T> {
private static final long serialVersionUID = 5629876084736248016L;
final AsyncSubject<T> parent;
AsyncDisposable(Observer<? super T> actual, AsyncSubject<T> parent) {
super(actual);
this.parent = parent;
}
@Override
public void dispose() {
if (super.tryDispose()) {
parent.remove(this);
}
}
void onComplete() {
if (!isDisposed()) {
actual.onComplete();
}
}
void onError(Throwable t) {
if (isDisposed()) {
RxJavaPlugins.onError(t);
} else {
actual.onError(t);
}
}
}
}