Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableFlatMapCompletable.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.flowable;
15
16 import java.util.Objects;
17 import java.util.concurrent.atomic.AtomicReference;
18
19 import org.reactivestreams.*;
20
21 import io.reactivex.rxjava3.annotations.Nullable;
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.disposables.*;
24 import io.reactivex.rxjava3.exceptions.Exceptions;
25 import io.reactivex.rxjava3.functions.Function;
26 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
27 import io.reactivex.rxjava3.internal.subscriptions.*;
28 import io.reactivex.rxjava3.internal.util.AtomicThrowable;
29
30 /**
31 * Maps a sequence of values into CompletableSources and awaits their termination.
32 * @param <T> the value type
33 */
34 public final class FlowableFlatMapCompletable<T> extends AbstractFlowableWithUpstream<T, T> {
35
36 final Function<? super T, ? extends CompletableSource> mapper;
37
38 final int maxConcurrency;
39
40 final boolean delayErrors;
41
42 public FlowableFlatMapCompletable(Flowable<T> source,
43 Function<? super T, ? extends CompletableSource> mapper, boolean delayErrors,
44 int maxConcurrency) {
45 super(source);
46 this.mapper = mapper;
47 this.delayErrors = delayErrors;
48 this.maxConcurrency = maxConcurrency;
49 }
50
51 @Override
52 protected void subscribeActual(Subscriber<? super T> subscriber) {
53 source.subscribe(new FlatMapCompletableMainSubscriber<>(subscriber, mapper, delayErrors, maxConcurrency));
54 }
55
56 static final class FlatMapCompletableMainSubscriber<T> extends BasicIntQueueSubscription<T>
57 implements FlowableSubscriber<T> {
58 private static final long serialVersionUID = 8443155186132538303L;
59
60 final Subscriber<? super T> downstream;
61
62 final AtomicThrowable errors;
63
64 final Function<? super T, ? extends CompletableSource> mapper;
65
66 final boolean delayErrors;
67
68 final CompositeDisposable set;
69
70 final int maxConcurrency;
71
72 Subscription upstream;
73
74 volatile boolean cancelled;
75
76 FlatMapCompletableMainSubscriber(Subscriber<? super T> subscriber,
77 Function<? super T, ? extends CompletableSource> mapper, boolean delayErrors,
78 int maxConcurrency) {
79 this.downstream = subscriber;
80 this.mapper = mapper;
81 this.delayErrors = delayErrors;
82 this.errors = new AtomicThrowable();
83 this.set = new CompositeDisposable();
84 this.maxConcurrency = maxConcurrency;
85 this.lazySet(1);
86 }
87
88 @Override
89 public void onSubscribe(Subscription s) {
90 if (SubscriptionHelper.validate(this.upstream, s)) {
91 this.upstream = s;
92
93 downstream.onSubscribe(this);
94
95 int m = maxConcurrency;
96 if (m == Integer.MAX_VALUE) {
97 s.request(Long.MAX_VALUE);
98 } else {
99 s.request(m);
100 }
101 }
102 }
103
104 @Override
105 public void onNext(T value) {
106 CompletableSource cs;
107
108 try {
109 cs = Objects.requireNonNull(mapper.apply(value), "The mapper returned a null CompletableSource");
110 } catch (Throwable ex) {
111 Exceptions.throwIfFatal(ex);
112 upstream.cancel();
113 onError(ex);
114 return;
115 }
116
117 getAndIncrement();
118
119 InnerConsumer inner = new InnerConsumer();
120
121 if (!cancelled && set.add(inner)) {
122 cs.subscribe(inner);
123 }
124 }
125
126 @Override
127 public void onError(Throwable e) {
128 if (errors.tryAddThrowableOrReport(e)) {
129 if (delayErrors) {
130 if (decrementAndGet() == 0) {
131 errors.tryTerminateConsumer(downstream);
132 } else {
133 if (maxConcurrency != Integer.MAX_VALUE) {
134 upstream.request(1);
135 }
136 }
137 } else {
138 cancelled = true;
139 upstream.cancel();
140 set.dispose();
141 errors.tryTerminateConsumer(downstream);
142 }
143 }
144 }
145
146 @Override
147 public void onComplete() {
148 if (decrementAndGet() == 0) {
149 errors.tryTerminateConsumer(downstream);
150 } else {
151 if (maxConcurrency != Integer.MAX_VALUE) {
152 upstream.request(1);
153 }
154 }
155 }
156
157 @Override
158 public void cancel() {
159 cancelled = true;
160 upstream.cancel();
161 set.dispose();
162 errors.tryTerminateAndReport();
163 }
164
165 @Override
166 public void request(long n) {
167 // ignored, no values emitted
168 }
169
170 @Nullable
171 @Override
172 public T poll() {
173 return null; // always empty
174 }
175
176 @Override
177 public boolean isEmpty() {
178 return true; // always empty
179 }
180
181 @Override
182 public void clear() {
183 // nothing to clear
184 }
185
186 @Override
187 public int requestFusion(int mode) {
188 return mode & ASYNC;
189 }
190
191 void innerComplete(InnerConsumer inner) {
192 set.delete(inner);
193 onComplete();
194 }
195
196 void innerError(InnerConsumer inner, Throwable e) {
197 set.delete(inner);
198 onError(e);
199 }
200
201 final class InnerConsumer extends AtomicReference<Disposable> implements CompletableObserver, Disposable {
202 private static final long serialVersionUID = 8606673141535671828L;
203
204 @Override
205 public void onSubscribe(Disposable d) {
206 DisposableHelper.setOnce(this, d);
207 }
208
209 @Override
210 public void onComplete() {
211 innerComplete(this);
212 }
213
214 @Override
215 public void onError(Throwable e) {
216 innerError(this, e);
217 }
218
219 @Override
220 public void dispose() {
221 DisposableHelper.dispose(this);
222 }
223
224 @Override
225 public boolean isDisposed() {
226 return DisposableHelper.isDisposed(get());
227 }
228 }
229 }
230 }