-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathFlowableWithLatestFromMany.java
303 lines (250 loc) · 9.22 KB
/
FlowableWithLatestFromMany.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
/**
* 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.internal.operators.flowable;
import java.util.Arrays;
import java.util.concurrent.atomic.*;
import org.reactivestreams.*;
import io.reactivex.*;
import io.reactivex.annotations.*;
import io.reactivex.exceptions.Exceptions;
import io.reactivex.functions.Function;
import io.reactivex.internal.functions.ObjectHelper;
import io.reactivex.internal.fuseable.ConditionalSubscriber;
import io.reactivex.internal.subscriptions.*;
import io.reactivex.internal.util.*;
import io.reactivex.plugins.RxJavaPlugins;
/**
* Combines a main sequence of values with the latest from multiple other sequences via
* a selector function.
*
* @param <T> the main sequence's type
* @param <R> the output type
*/
public final class FlowableWithLatestFromMany<T, R> extends AbstractFlowableWithUpstream<T, R> {
@Nullable
final Publisher<?>[] otherArray;
@Nullable
final Iterable<? extends Publisher<?>> otherIterable;
final Function<? super Object[], R> combiner;
public FlowableWithLatestFromMany(@NonNull Flowable<T> source, @NonNull Publisher<?>[] otherArray, Function<? super Object[], R> combiner) {
super(source);
this.otherArray = otherArray;
this.otherIterable = null;
this.combiner = combiner;
}
public FlowableWithLatestFromMany(@NonNull Flowable<T> source, @NonNull Iterable<? extends Publisher<?>> otherIterable, @NonNull Function<? super Object[], R> combiner) {
super(source);
this.otherArray = null;
this.otherIterable = otherIterable;
this.combiner = combiner;
}
@Override
protected void subscribeActual(Subscriber<? super R> s) {
Publisher<?>[] others = otherArray;
int n = 0;
if (others == null) {
others = new Publisher[8];
try {
for (Publisher<?> p : otherIterable) {
if (n == others.length) {
others = Arrays.copyOf(others, n + (n >> 1));
}
others[n++] = p;
}
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
EmptySubscription.error(ex, s);
return;
}
} else {
n = others.length;
}
if (n == 0) {
new FlowableMap<T, R>(source, new SingletonArrayFunc()).subscribeActual(s);
return;
}
WithLatestFromSubscriber<T, R> parent = new WithLatestFromSubscriber<T, R>(s, combiner, n);
s.onSubscribe(parent);
parent.subscribe(others, n);
source.subscribe(parent);
}
static final class WithLatestFromSubscriber<T, R>
extends AtomicInteger
implements ConditionalSubscriber<T>, Subscription {
private static final long serialVersionUID = 1577321883966341961L;
final Subscriber<? super R> downstream;
final Function<? super Object[], R> combiner;
final WithLatestInnerSubscriber[] subscribers;
final AtomicReferenceArray<Object> values;
final AtomicReference<Subscription> upstream;
final AtomicLong requested;
final AtomicThrowable error;
volatile boolean done;
WithLatestFromSubscriber(Subscriber<? super R> actual, Function<? super Object[], R> combiner, int n) {
this.downstream = actual;
this.combiner = combiner;
WithLatestInnerSubscriber[] s = new WithLatestInnerSubscriber[n];
for (int i = 0; i < n; i++) {
s[i] = new WithLatestInnerSubscriber(this, i);
}
this.subscribers = s;
this.values = new AtomicReferenceArray<Object>(n);
this.upstream = new AtomicReference<Subscription>();
this.requested = new AtomicLong();
this.error = new AtomicThrowable();
}
void subscribe(Publisher<?>[] others, int n) {
WithLatestInnerSubscriber[] subscribers = this.subscribers;
AtomicReference<Subscription> upstream = this.upstream;
for (int i = 0; i < n; i++) {
if (upstream.get() == SubscriptionHelper.CANCELLED) {
return;
}
others[i].subscribe(subscribers[i]);
}
}
@Override
public void onSubscribe(Subscription s) {
SubscriptionHelper.deferredSetOnce(this.upstream, requested, s);
}
@Override
public void onNext(T t) {
if (!tryOnNext(t) && !done) {
upstream.get().request(1);
}
}
@Override
public boolean tryOnNext(T t) {
if (done) {
return false;
}
AtomicReferenceArray<Object> ara = values;
int n = ara.length();
Object[] objects = new Object[n + 1];
objects[0] = t;
for (int i = 0; i < n; i++) {
Object o = ara.get(i);
if (o == null) {
// somebody hasn't signalled yet, skip this T
return false;
}
objects[i + 1] = o;
}
R v;
try {
v = ObjectHelper.requireNonNull(combiner.apply(objects), "The combiner returned a null value");
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
cancel();
onError(ex);
return false;
}
HalfSerializer.onNext(downstream, v, this, error);
return true;
}
@Override
public void onError(Throwable t) {
if (done) {
RxJavaPlugins.onError(t);
return;
}
done = true;
cancelAllBut(-1);
HalfSerializer.onError(downstream, t, this, error);
}
@Override
public void onComplete() {
if (!done) {
done = true;
cancelAllBut(-1);
HalfSerializer.onComplete(downstream, this, error);
}
}
@Override
public void request(long n) {
SubscriptionHelper.deferredRequest(upstream, requested, n);
}
@Override
public void cancel() {
SubscriptionHelper.cancel(upstream);
for (WithLatestInnerSubscriber s : subscribers) {
s.dispose();
}
}
void innerNext(int index, Object o) {
values.set(index, o);
}
void innerError(int index, Throwable t) {
done = true;
SubscriptionHelper.cancel(upstream);
cancelAllBut(index);
HalfSerializer.onError(downstream, t, this, error);
}
void innerComplete(int index, boolean nonEmpty) {
if (!nonEmpty) {
done = true;
SubscriptionHelper.cancel(upstream);
cancelAllBut(index);
HalfSerializer.onComplete(downstream, this, error);
}
}
void cancelAllBut(int index) {
WithLatestInnerSubscriber[] subscribers = this.subscribers;
for (int i = 0; i < subscribers.length; i++) {
if (i != index) {
subscribers[i].dispose();
}
}
}
}
static final class WithLatestInnerSubscriber
extends AtomicReference<Subscription>
implements FlowableSubscriber<Object> {
private static final long serialVersionUID = 3256684027868224024L;
final WithLatestFromSubscriber<?, ?> parent;
final int index;
boolean hasValue;
WithLatestInnerSubscriber(WithLatestFromSubscriber<?, ?> parent, int index) {
this.parent = parent;
this.index = index;
}
@Override
public void onSubscribe(Subscription s) {
SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
}
@Override
public void onNext(Object t) {
if (!hasValue) {
hasValue = true;
}
parent.innerNext(index, t);
}
@Override
public void onError(Throwable t) {
parent.innerError(index, t);
}
@Override
public void onComplete() {
parent.innerComplete(index, hasValue);
}
void dispose() {
SubscriptionHelper.cancel(this);
}
}
final class SingletonArrayFunc implements Function<T, R> {
@Override
public R apply(T t) throws Exception {
return ObjectHelper.requireNonNull(combiner.apply(new Object[] { t }), "The combiner returned a null value");
}
}
}