Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/mixed/FlowableConcatMapSingle.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.mixed;
15
16 import java.util.Objects;
17 import java.util.concurrent.atomic.*;
18
19 import org.reactivestreams.*;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.disposables.Disposable;
23 import io.reactivex.rxjava3.exceptions.Exceptions;
24 import io.reactivex.rxjava3.functions.Function;
25 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
26 import io.reactivex.rxjava3.internal.util.*;
27 import io.reactivex.rxjava3.operators.SimpleQueue;
28
29 /**
30 * Maps each upstream item into a {@link SingleSource}, subscribes to them one after the other terminates
31 * and relays their success values, optionally delaying any errors till the main and inner sources
32 * terminate.
33 * <p>History: 2.1.11 - experimental
34 * @param <T> the upstream element type
35 * @param <R> the output element type
36 * @since 2.2
37 */
38 public final class FlowableConcatMapSingle<T, R> extends Flowable<R> {
39
40 final Flowable<T> source;
41
42 final Function<? super T, ? extends SingleSource<? extends R>> mapper;
43
44 final ErrorMode errorMode;
45
46 final int prefetch;
47
48 public FlowableConcatMapSingle(Flowable<T> source,
49 Function<? super T, ? extends SingleSource<? extends R>> mapper,
50 ErrorMode errorMode, int prefetch) {
51 this.source = source;
52 this.mapper = mapper;
53 this.errorMode = errorMode;
54 this.prefetch = prefetch;
55 }
56
57 @Override
58 protected void subscribeActual(Subscriber<? super R> s) {
59 source.subscribe(new ConcatMapSingleSubscriber<>(s, mapper, prefetch, errorMode));
60 }
61
62 static final class ConcatMapSingleSubscriber<T, R>
63 extends ConcatMapXMainSubscriber<T> implements Subscription {
64
65 private static final long serialVersionUID = -9140123220065488293L;
66
67 final Subscriber<? super R> downstream;
68
69 final Function<? super T, ? extends SingleSource<? extends R>> mapper;
70
71 final AtomicLong requested;
72
73 final ConcatMapSingleObserver<R> inner;
74
75 long emitted;
76
77 int consumed;
78
79 R item;
80
81 volatile int state;
82
83 /** No inner SingleSource is running. */
84 static final int STATE_INACTIVE = 0;
85 /** An inner SingleSource is running but there are no results yet. */
86 static final int STATE_ACTIVE = 1;
87 /** The inner SingleSource succeeded with a value in {@link #item}. */
88 static final int STATE_RESULT_VALUE = 2;
89
90 ConcatMapSingleSubscriber(Subscriber<? super R> downstream,
91 Function<? super T, ? extends SingleSource<? extends R>> mapper,
92 int prefetch, ErrorMode errorMode) {
93 super(prefetch, errorMode);
94 this.downstream = downstream;
95 this.mapper = mapper;
96 this.requested = new AtomicLong();
97 this.inner = new ConcatMapSingleObserver<>(this);
98 }
99
100 @Override
101 void onSubscribeDownstream() {
102 downstream.onSubscribe(this);
103 }
104
105 @Override
106 public void request(long n) {
107 BackpressureHelper.add(requested, n);
108 drain();
109 }
110
111 @Override
112 public void cancel() {
113 stop();
114 }
115
116 @Override
117 void clearValue() {
118 item = null;
119 }
120
121 @Override
122 void disposeInner() {
123 inner.dispose();
124 }
125
126 void innerSuccess(R item) {
127 this.item = item;
128 this.state = STATE_RESULT_VALUE;
129 drain();
130 }
131
132 void innerError(Throwable ex) {
133 if (errors.tryAddThrowableOrReport(ex)) {
134 if (errorMode != ErrorMode.END) {
135 upstream.cancel();
136 }
137 this.state = STATE_INACTIVE;
138 drain();
139 }
140 }
141
142 @Override
143 void drain() {
144 if (getAndIncrement() != 0) {
145 return;
146 }
147
148 int missed = 1;
149 Subscriber<? super R> downstream = this.downstream;
150 ErrorMode errorMode = this.errorMode;
151 SimpleQueue<T> queue = this.queue;
152 AtomicThrowable errors = this.errors;
153 AtomicLong requested = this.requested;
154 int limit = prefetch - (prefetch >> 1);
155 boolean syncFused = this.syncFused;
156
157 for (;;) {
158
159 for (;;) {
160 if (cancelled) {
161 queue.clear();
162 item = null;
163 break;
164 }
165
166 int s = state;
167
168 if (errors.get() != null) {
169 if (errorMode == ErrorMode.IMMEDIATE
170 || (errorMode == ErrorMode.BOUNDARY && s == STATE_INACTIVE)) {
171 queue.clear();
172 item = null;
173 errors.tryTerminateConsumer(downstream);
174 return;
175 }
176 }
177
178 if (s == STATE_INACTIVE) {
179 boolean d = done;
180 T v;
181 try {
182 v = queue.poll();
183 } catch (Throwable ex) {
184 Exceptions.throwIfFatal(ex);
185 upstream.cancel();
186 errors.tryAddThrowableOrReport(ex);
187 errors.tryTerminateConsumer(downstream);
188 return;
189 }
190 boolean empty = v == null;
191
192 if (d && empty) {
193 errors.tryTerminateConsumer(downstream);
194 return;
195 }
196
197 if (empty) {
198 break;
199 }
200
201 if (!syncFused) {
202 int c = consumed + 1;
203 if (c == limit) {
204 consumed = 0;
205 upstream.request(limit);
206 } else {
207 consumed = c;
208 }
209 }
210
211 SingleSource<? extends R> ss;
212
213 try {
214 ss = Objects.requireNonNull(mapper.apply(v), "The mapper returned a null SingleSource");
215 } catch (Throwable ex) {
216 Exceptions.throwIfFatal(ex);
217 upstream.cancel();
218 queue.clear();
219 errors.tryAddThrowableOrReport(ex);
220 errors.tryTerminateConsumer(downstream);
221 return;
222 }
223
224 state = STATE_ACTIVE;
225 ss.subscribe(inner);
226 break;
227 } else if (s == STATE_RESULT_VALUE) {
228 long e = emitted;
229 if (e != requested.get()) {
230 R w = item;
231 item = null;
232
233 downstream.onNext(w);
234
235 emitted = e + 1;
236 state = STATE_INACTIVE;
237 } else {
238 break;
239 }
240 } else {
241 break;
242 }
243 }
244
245 missed = addAndGet(-missed);
246 if (missed == 0) {
247 break;
248 }
249 }
250 }
251
252 static final class ConcatMapSingleObserver<R>
253 extends AtomicReference<Disposable>
254 implements SingleObserver<R> {
255
256 private static final long serialVersionUID = -3051469169682093892L;
257
258 final ConcatMapSingleSubscriber<?, R> parent;
259
260 ConcatMapSingleObserver(ConcatMapSingleSubscriber<?, R> parent) {
261 this.parent = parent;
262 }
263
264 @Override
265 public void onSubscribe(Disposable d) {
266 DisposableHelper.replace(this, d);
267 }
268
269 @Override
270 public void onSuccess(R t) {
271 parent.innerSuccess(t);
272 }
273
274 @Override
275 public void onError(Throwable e) {
276 parent.innerError(e);
277 }
278
279 void dispose() {
280 DisposableHelper.dispose(this);
281 }
282 }
283 }
284 }