Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/mixed/FlowableConcatMapMaybe.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 MaybeSource}, 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 FlowableConcatMapMaybe<T, R> extends Flowable<R> {
39
40 final Flowable<T> source;
41
42 final Function<? super T, ? extends MaybeSource<? extends R>> mapper;
43
44 final ErrorMode errorMode;
45
46 final int prefetch;
47
48 public FlowableConcatMapMaybe(Flowable<T> source,
49 Function<? super T, ? extends MaybeSource<? 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 ConcatMapMaybeSubscriber<>(s, mapper, prefetch, errorMode));
60 }
61
62 static final class ConcatMapMaybeSubscriber<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 MaybeSource<? extends R>> mapper;
70
71 final AtomicLong requested;
72
73 final ConcatMapMaybeObserver<R> inner;
74
75 long emitted;
76
77 int consumed;
78
79 R item;
80
81 volatile int state;
82
83 /** No inner MaybeSource is running. */
84 static final int STATE_INACTIVE = 0;
85 /** An inner MaybeSource is running but there are no results yet. */
86 static final int STATE_ACTIVE = 1;
87 /** The inner MaybeSource succeeded with a value in {@link #item}. */
88 static final int STATE_RESULT_VALUE = 2;
89
90 ConcatMapMaybeSubscriber(Subscriber<? super R> downstream,
91 Function<? super T, ? extends MaybeSource<? 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 ConcatMapMaybeObserver<>(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 void innerSuccess(R item) {
117 this.item = item;
118 this.state = STATE_RESULT_VALUE;
119 drain();
120 }
121
122 void innerComplete() {
123 this.state = STATE_INACTIVE;
124 drain();
125 }
126
127 void innerError(Throwable ex) {
128 if (errors.tryAddThrowableOrReport(ex)) {
129 if (errorMode != ErrorMode.END) {
130 upstream.cancel();
131 }
132 this.state = STATE_INACTIVE;
133 drain();
134 }
135 }
136
137 @Override
138 void clearValue() {
139 item = null;
140 }
141
142 @Override
143 void disposeInner() {
144 inner.dispose();
145 }
146
147 @Override
148 void drain() {
149 if (getAndIncrement() != 0) {
150 return;
151 }
152
153 int missed = 1;
154 Subscriber<? super R> downstream = this.downstream;
155 ErrorMode errorMode = this.errorMode;
156 SimpleQueue<T> queue = this.queue;
157 AtomicThrowable errors = this.errors;
158 AtomicLong requested = this.requested;
159 int limit = prefetch - (prefetch >> 1);
160 boolean syncFused = this.syncFused;
161
162 for (;;) {
163
164 for (;;) {
165 if (cancelled) {
166 queue.clear();
167 item = null;
168 break;
169 }
170
171 int s = state;
172
173 if (errors.get() != null) {
174 if (errorMode == ErrorMode.IMMEDIATE
175 || (errorMode == ErrorMode.BOUNDARY && s == STATE_INACTIVE)) {
176 queue.clear();
177 item = null;
178 errors.tryTerminateConsumer(downstream);
179 return;
180 }
181 }
182
183 if (s == STATE_INACTIVE) {
184 boolean d = done;
185 T v;
186 try {
187 v = queue.poll();
188 } catch (Throwable ex) {
189 Exceptions.throwIfFatal(ex);
190 upstream.cancel();
191 errors.tryAddThrowableOrReport(ex);
192 errors.tryTerminateConsumer(downstream);
193 return;
194 }
195 boolean empty = v == null;
196
197 if (d && empty) {
198 errors.tryTerminateConsumer(downstream);
199 return;
200 }
201
202 if (empty) {
203 break;
204 }
205
206 if (!syncFused) {
207 int c = consumed + 1;
208 if (c == limit) {
209 consumed = 0;
210 upstream.request(limit);
211 } else {
212 consumed = c;
213 }
214 }
215
216 MaybeSource<? extends R> ms;
217
218 try {
219 ms = Objects.requireNonNull(mapper.apply(v), "The mapper returned a null MaybeSource");
220 } catch (Throwable ex) {
221 Exceptions.throwIfFatal(ex);
222 upstream.cancel();
223 queue.clear();
224 errors.tryAddThrowableOrReport(ex);
225 errors.tryTerminateConsumer(downstream);
226 return;
227 }
228
229 state = STATE_ACTIVE;
230 ms.subscribe(inner);
231 break;
232 } else if (s == STATE_RESULT_VALUE) {
233 long e = emitted;
234 if (e != requested.get()) {
235 R w = item;
236 item = null;
237
238 downstream.onNext(w);
239
240 emitted = e + 1;
241 state = STATE_INACTIVE;
242 } else {
243 break;
244 }
245 } else {
246 break;
247 }
248 }
249
250 missed = addAndGet(-missed);
251 if (missed == 0) {
252 break;
253 }
254 }
255 }
256
257 static final class ConcatMapMaybeObserver<R>
258 extends AtomicReference<Disposable>
259 implements MaybeObserver<R> {
260
261 private static final long serialVersionUID = -3051469169682093892L;
262
263 final ConcatMapMaybeSubscriber<?, R> parent;
264
265 ConcatMapMaybeObserver(ConcatMapMaybeSubscriber<?, R> parent) {
266 this.parent = parent;
267 }
268
269 @Override
270 public void onSubscribe(Disposable d) {
271 DisposableHelper.replace(this, d);
272 }
273
274 @Override
275 public void onSuccess(R t) {
276 parent.innerSuccess(t);
277 }
278
279 @Override
280 public void onError(Throwable e) {
281 parent.innerError(e);
282 }
283
284 @Override
285 public void onComplete() {
286 parent.innerComplete();
287 }
288
289 void dispose() {
290 DisposableHelper.dispose(this);
291 }
292 }
293 }
294 }