Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableDoFinally.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 org.reactivestreams.*;
17
18 import io.reactivex.rxjava3.annotations.Nullable;
19 import io.reactivex.rxjava3.core.*;
20 import io.reactivex.rxjava3.exceptions.Exceptions;
21 import io.reactivex.rxjava3.functions.Action;
22 import io.reactivex.rxjava3.internal.subscriptions.*;
23 import io.reactivex.rxjava3.operators.ConditionalSubscriber;
24 import io.reactivex.rxjava3.operators.QueueSubscription;
25 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
26
27 /**
28 * Execute an action after an onError, onComplete or a cancel event.
29 * <p>History: 2.0.1 - experimental
30 * @param <T> the value type
31 * @since 2.1
32 */
33 public final class FlowableDoFinally<T> extends AbstractFlowableWithUpstream<T, T> {
34
35 final Action onFinally;
36
37 public FlowableDoFinally(Flowable<T> source, Action onFinally) {
38 super(source);
39 this.onFinally = onFinally;
40 }
41
42 @Override
43 protected void subscribeActual(Subscriber<? super T> s) {
44 if (s instanceof ConditionalSubscriber) {
45 source.subscribe(new DoFinallyConditionalSubscriber<>((ConditionalSubscriber<? super T>) s, onFinally));
46 } else {
47 source.subscribe(new DoFinallySubscriber<>(s, onFinally));
48 }
49 }
50
51 static final class DoFinallySubscriber<T> extends BasicIntQueueSubscription<T> implements FlowableSubscriber<T> {
52
53 private static final long serialVersionUID = 4109457741734051389L;
54
55 final Subscriber<? super T> downstream;
56
57 final Action onFinally;
58
59 Subscription upstream;
60
61 QueueSubscription<T> qs;
62
63 boolean syncFused;
64
65 DoFinallySubscriber(Subscriber<? super T> actual, Action onFinally) {
66 this.downstream = actual;
67 this.onFinally = onFinally;
68 }
69
70 @SuppressWarnings("unchecked")
71 @Override
72 public void onSubscribe(Subscription s) {
73 if (SubscriptionHelper.validate(this.upstream, s)) {
74 this.upstream = s;
75 if (s instanceof QueueSubscription) {
76 this.qs = (QueueSubscription<T>)s;
77 }
78
79 downstream.onSubscribe(this);
80 }
81 }
82
83 @Override
84 public void onNext(T t) {
85 downstream.onNext(t);
86 }
87
88 @Override
89 public void onError(Throwable t) {
90 downstream.onError(t);
91 runFinally();
92 }
93
94 @Override
95 public void onComplete() {
96 downstream.onComplete();
97 runFinally();
98 }
99
100 @Override
101 public void cancel() {
102 upstream.cancel();
103 runFinally();
104 }
105
106 @Override
107 public void request(long n) {
108 upstream.request(n);
109 }
110
111 @Override
112 public int requestFusion(int mode) {
113 QueueSubscription<T> qs = this.qs;
114 if (qs != null && (mode & BOUNDARY) == 0) {
115 int m = qs.requestFusion(mode);
116 if (m != NONE) {
117 syncFused = m == SYNC;
118 }
119 return m;
120 }
121 return NONE;
122 }
123
124 @Override
125 public void clear() {
126 qs.clear();
127 }
128
129 @Override
130 public boolean isEmpty() {
131 return qs.isEmpty();
132 }
133
134 @Nullable
135 @Override
136 public T poll() throws Throwable {
137 T v = qs.poll();
138 if (v == null && syncFused) {
139 runFinally();
140 }
141 return v;
142 }
143
144 void runFinally() {
145 if (compareAndSet(0, 1)) {
146 try {
147 onFinally.run();
148 } catch (Throwable ex) {
149 Exceptions.throwIfFatal(ex);
150 RxJavaPlugins.onError(ex);
151 }
152 }
153 }
154 }
155
156 static final class DoFinallyConditionalSubscriber<T> extends BasicIntQueueSubscription<T> implements ConditionalSubscriber<T> {
157
158 private static final long serialVersionUID = 4109457741734051389L;
159
160 final ConditionalSubscriber<? super T> downstream;
161
162 final Action onFinally;
163
164 Subscription upstream;
165
166 QueueSubscription<T> qs;
167
168 boolean syncFused;
169
170 DoFinallyConditionalSubscriber(ConditionalSubscriber<? super T> actual, Action onFinally) {
171 this.downstream = actual;
172 this.onFinally = onFinally;
173 }
174
175 @SuppressWarnings("unchecked")
176 @Override
177 public void onSubscribe(Subscription s) {
178 if (SubscriptionHelper.validate(this.upstream, s)) {
179 this.upstream = s;
180 if (s instanceof QueueSubscription) {
181 this.qs = (QueueSubscription<T>)s;
182 }
183
184 downstream.onSubscribe(this);
185 }
186 }
187
188 @Override
189 public void onNext(T t) {
190 downstream.onNext(t);
191 }
192
193 @Override
194 public boolean tryOnNext(T t) {
195 return downstream.tryOnNext(t);
196 }
197
198 @Override
199 public void onError(Throwable t) {
200 downstream.onError(t);
201 runFinally();
202 }
203
204 @Override
205 public void onComplete() {
206 downstream.onComplete();
207 runFinally();
208 }
209
210 @Override
211 public void cancel() {
212 upstream.cancel();
213 runFinally();
214 }
215
216 @Override
217 public void request(long n) {
218 upstream.request(n);
219 }
220
221 @Override
222 public int requestFusion(int mode) {
223 QueueSubscription<T> qs = this.qs;
224 if (qs != null && (mode & BOUNDARY) == 0) {
225 int m = qs.requestFusion(mode);
226 if (m != NONE) {
227 syncFused = m == SYNC;
228 }
229 return m;
230 }
231 return NONE;
232 }
233
234 @Override
235 public void clear() {
236 qs.clear();
237 }
238
239 @Override
240 public boolean isEmpty() {
241 return qs.isEmpty();
242 }
243
244 @Nullable
245 @Override
246 public T poll() throws Throwable {
247 T v = qs.poll();
248 if (v == null && syncFused) {
249 runFinally();
250 }
251 return v;
252 }
253
254 void runFinally() {
255 if (compareAndSet(0, 1)) {
256 try {
257 onFinally.run();
258 } catch (Throwable ex) {
259 Exceptions.throwIfFatal(ex);
260 RxJavaPlugins.onError(ex);
261 }
262 }
263 }
264 }
265 }