Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableDoFinally.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.observable;
15
16 import io.reactivex.rxjava3.annotations.Nullable;
17 import io.reactivex.rxjava3.core.*;
18 import io.reactivex.rxjava3.disposables.Disposable;
19 import io.reactivex.rxjava3.exceptions.Exceptions;
20 import io.reactivex.rxjava3.functions.Action;
21 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
22 import io.reactivex.rxjava3.internal.observers.BasicIntQueueDisposable;
23 import io.reactivex.rxjava3.operators.QueueDisposable;
24 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
25
26 /**
27 * Execute an action after an onError, onComplete or a dispose event.
28 * <p>History: 2.0.1 - experimental
29 * @param <T> the value type
30 * @since 2.1
31 */
32 public final class ObservableDoFinally<T> extends AbstractObservableWithUpstream<T, T> {
33
34 final Action onFinally;
35
36 public ObservableDoFinally(ObservableSource<T> source, Action onFinally) {
37 super(source);
38 this.onFinally = onFinally;
39 }
40
41 @Override
42 protected void subscribeActual(Observer<? super T> observer) {
43 source.subscribe(new DoFinallyObserver<>(observer, onFinally));
44 }
45
46 static final class DoFinallyObserver<T> extends BasicIntQueueDisposable<T> implements Observer<T> {
47
48 private static final long serialVersionUID = 4109457741734051389L;
49
50 final Observer<? super T> downstream;
51
52 final Action onFinally;
53
54 Disposable upstream;
55
56 QueueDisposable<T> qd;
57
58 boolean syncFused;
59
60 DoFinallyObserver(Observer<? super T> actual, Action onFinally) {
61 this.downstream = actual;
62 this.onFinally = onFinally;
63 }
64
65 @SuppressWarnings("unchecked")
66 @Override
67 public void onSubscribe(Disposable d) {
68 if (DisposableHelper.validate(this.upstream, d)) {
69 this.upstream = d;
70 if (d instanceof QueueDisposable) {
71 this.qd = (QueueDisposable<T>)d;
72 }
73
74 downstream.onSubscribe(this);
75 }
76 }
77
78 @Override
79 public void onNext(T t) {
80 downstream.onNext(t);
81 }
82
83 @Override
84 public void onError(Throwable t) {
85 downstream.onError(t);
86 runFinally();
87 }
88
89 @Override
90 public void onComplete() {
91 downstream.onComplete();
92 runFinally();
93 }
94
95 @Override
96 public void dispose() {
97 upstream.dispose();
98 runFinally();
99 }
100
101 @Override
102 public boolean isDisposed() {
103 return upstream.isDisposed();
104 }
105
106 @Override
107 public int requestFusion(int mode) {
108 QueueDisposable<T> qd = this.qd;
109 if (qd != null && (mode & BOUNDARY) == 0) {
110 int m = qd.requestFusion(mode);
111 if (m != NONE) {
112 syncFused = m == SYNC;
113 }
114 return m;
115 }
116 return NONE;
117 }
118
119 @Override
120 public void clear() {
121 qd.clear();
122 }
123
124 @Override
125 public boolean isEmpty() {
126 return qd.isEmpty();
127 }
128
129 @Nullable
130 @Override
131 public T poll() throws Throwable {
132 T v = qd.poll();
133 if (v == null && syncFused) {
134 runFinally();
135 }
136 return v;
137 }
138
139 void runFinally() {
140 if (compareAndSet(0, 1)) {
141 try {
142 onFinally.run();
143 } catch (Throwable ex) {
144 Exceptions.throwIfFatal(ex);
145 RxJavaPlugins.onError(ex);
146 }
147 }
148 }
149 }
150 }