Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/mixed/FlowableSwitchMapCompletable.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.AtomicReference;
18
19 import org.reactivestreams.Subscription;
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.subscriptions.SubscriptionHelper;
27 import io.reactivex.rxjava3.internal.util.AtomicThrowable;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29
30 /**
31 * Maps the upstream values into {@link CompletableSource}s, subscribes to the newer one while
32 * disposing the subscription to the previous {@code CompletableSource}, thus keeping at most one
33 * active {@code CompletableSource} running.
34 * <p>History: 2.1.11 - experimental
35 * @param <T> the upstream value type
36 * @since 2.2
37 */
38 public final class FlowableSwitchMapCompletable<T> extends Completable {
39
40 final Flowable<T> source;
41
42 final Function<? super T, ? extends CompletableSource> mapper;
43
44 final boolean delayErrors;
45
46 public FlowableSwitchMapCompletable(Flowable<T> source,
47 Function<? super T, ? extends CompletableSource> mapper, boolean delayErrors) {
48 this.source = source;
49 this.mapper = mapper;
50 this.delayErrors = delayErrors;
51 }
52
53 @Override
54 protected void subscribeActual(CompletableObserver observer) {
55 source.subscribe(new SwitchMapCompletableObserver<>(observer, mapper, delayErrors));
56 }
57
58 static final class SwitchMapCompletableObserver<T> implements FlowableSubscriber<T>, Disposable {
59
60 final CompletableObserver downstream;
61
62 final Function<? super T, ? extends CompletableSource> mapper;
63
64 final boolean delayErrors;
65
66 final AtomicThrowable errors;
67
68 final AtomicReference<SwitchMapInnerObserver> inner;
69
70 static final SwitchMapInnerObserver INNER_DISPOSED = new SwitchMapInnerObserver(null);
71
72 volatile boolean done;
73
74 Subscription upstream;
75
76 SwitchMapCompletableObserver(CompletableObserver downstream,
77 Function<? super T, ? extends CompletableSource> mapper, boolean delayErrors) {
78 this.downstream = downstream;
79 this.mapper = mapper;
80 this.delayErrors = delayErrors;
81 this.errors = new AtomicThrowable();
82 this.inner = new AtomicReference<>();
83 }
84
85 @Override
86 public void onSubscribe(Subscription s) {
87 if (SubscriptionHelper.validate(upstream, s)) {
88 this.upstream = s;
89 downstream.onSubscribe(this);
90 s.request(Long.MAX_VALUE);
91 }
92 }
93
94 @Override
95 public void onNext(T t) {
96 CompletableSource c;
97
98 try {
99 c = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null CompletableSource");
100 } catch (Throwable ex) {
101 Exceptions.throwIfFatal(ex);
102 upstream.cancel();
103 onError(ex);
104 return;
105 }
106
107 SwitchMapInnerObserver o = new SwitchMapInnerObserver(this);
108
109 for (;;) {
110 SwitchMapInnerObserver current = inner.get();
111 if (current == INNER_DISPOSED) {
112 break;
113 }
114 if (inner.compareAndSet(current, o)) {
115 if (current != null) {
116 current.dispose();
117 }
118 c.subscribe(o);
119 break;
120 }
121 }
122 }
123
124 @Override
125 public void onError(Throwable t) {
126 if (errors.tryAddThrowableOrReport(t)) {
127 if (delayErrors) {
128 onComplete();
129 } else {
130 disposeInner();
131 errors.tryTerminateConsumer(downstream);
132 }
133 }
134 }
135
136 @Override
137 public void onComplete() {
138 done = true;
139 if (inner.get() == null) {
140 errors.tryTerminateConsumer(downstream);
141 }
142 }
143
144 void disposeInner() {
145 SwitchMapInnerObserver o = inner.getAndSet(INNER_DISPOSED);
146 if (o != null && o != INNER_DISPOSED) {
147 o.dispose();
148 }
149 }
150
151 @Override
152 public void dispose() {
153 upstream.cancel();
154 disposeInner();
155 errors.tryTerminateAndReport();
156 }
157
158 @Override
159 public boolean isDisposed() {
160 return inner.get() == INNER_DISPOSED;
161 }
162
163 void innerError(SwitchMapInnerObserver sender, Throwable error) {
164 if (inner.compareAndSet(sender, null)) {
165 if (errors.tryAddThrowableOrReport(error)) {
166 if (delayErrors) {
167 if (done) {
168 errors.tryTerminateConsumer(downstream);
169 }
170 } else {
171 upstream.cancel();
172 disposeInner();
173 errors.tryTerminateConsumer(downstream);
174 }
175 }
176 } else {
177 RxJavaPlugins.onError(error);
178 }
179 }
180
181 void innerComplete(SwitchMapInnerObserver sender) {
182 if (inner.compareAndSet(sender, null)) {
183 if (done) {
184 errors.tryTerminateConsumer(downstream);
185 }
186 }
187 }
188
189 static final class SwitchMapInnerObserver extends AtomicReference<Disposable>
190 implements CompletableObserver {
191
192 private static final long serialVersionUID = -8003404460084760287L;
193
194 final SwitchMapCompletableObserver<?> parent;
195
196 SwitchMapInnerObserver(SwitchMapCompletableObserver<?> parent) {
197 this.parent = parent;
198 }
199
200 @Override
201 public void onSubscribe(Disposable d) {
202 DisposableHelper.setOnce(this, d);
203 }
204
205 @Override
206 public void onError(Throwable e) {
207 parent.innerError(this, e);
208 }
209
210 @Override
211 public void onComplete() {
212 parent.innerComplete(this);
213 }
214
215 void dispose() {
216 DisposableHelper.dispose(this);
217 }
218 }
219 }
220 }