Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservablePublish.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 java.util.concurrent.atomic.*;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.exceptions.Exceptions;
21 import io.reactivex.rxjava3.functions.Consumer;
22 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
23 import io.reactivex.rxjava3.internal.fuseable.HasUpstreamObservableSource;
24 import io.reactivex.rxjava3.internal.util.ExceptionHelper;
25 import io.reactivex.rxjava3.observables.ConnectableObservable;
26 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
27
28 /**
29 * Shares a single underlying connection to the upstream ObservableSource
30 * and multicasts events to all subscribed observers until the upstream
31 * completes or the connection is disposed.
32 * <p>
33 * The difference to ObservablePublish is that when the upstream terminates,
34 * late observers will receive that terminal event until the connection is
35 * disposed and the ConnectableObservable is reset to its fresh state.
36 *
37 * @param <T> the element type
38 * @since 2.2.10
39 */
40 public final class ObservablePublish<T> extends ConnectableObservable<T>
41 implements HasUpstreamObservableSource<T> {
42
43 final ObservableSource<T> source;
44
45 final AtomicReference<PublishConnection<T>> current;
46
47 public ObservablePublish(ObservableSource<T> source) {
48 this.source = source;
49 this.current = new AtomicReference<>();
50 }
51
52 @Override
53 public void connect(Consumer<? super Disposable> connection) {
54 boolean doConnect = false;
55 PublishConnection<T> conn;
56
57 for (;;) {
58 conn = current.get();
59
60 if (conn == null || conn.isDisposed()) {
61 PublishConnection<T> fresh = new PublishConnection<>(current);
62 if (!current.compareAndSet(conn, fresh)) {
63 continue;
64 }
65 conn = fresh;
66 }
67
68 doConnect = !conn.connect.get() && conn.connect.compareAndSet(false, true);
69 break;
70 }
71
72 try {
73 connection.accept(conn);
74 } catch (Throwable ex) {
75 Exceptions.throwIfFatal(ex);
76 throw ExceptionHelper.wrapOrThrow(ex);
77 }
78
79 if (doConnect) {
80 source.subscribe(conn);
81 }
82 }
83
84 @Override
85 protected void subscribeActual(Observer<? super T> observer) {
86 PublishConnection<T> conn;
87
88 for (;;) {
89 conn = current.get();
90 // we don't create a fresh connection if the current is terminated
91 if (conn == null) {
92 PublishConnection<T> fresh = new PublishConnection<>(current);
93 if (!current.compareAndSet(conn, fresh)) {
94 continue;
95 }
96 conn = fresh;
97 }
98 break;
99 }
100
101 InnerDisposable<T> inner = new InnerDisposable<>(observer, conn);
102 observer.onSubscribe(inner);
103 if (conn.add(inner)) {
104 if (inner.isDisposed()) {
105 conn.remove(inner);
106 }
107 return;
108 }
109 // Late observers will be simply terminated
110 Throwable error = conn.error;
111 if (error != null) {
112 observer.onError(error);
113 } else {
114 observer.onComplete();
115 }
116 }
117
118 @Override
119 public void reset() {
120 PublishConnection<T> conn = current.get();
121 if (conn != null && conn.isDisposed()) {
122 current.compareAndSet(conn, null);
123 }
124 }
125
126 @Override
127 public ObservableSource<T> source() {
128 return source;
129 }
130
131 static final class PublishConnection<T>
132 extends AtomicReference<InnerDisposable<T>[]>
133 implements Observer<T>, Disposable {
134
135 private static final long serialVersionUID = -3251430252873581268L;
136
137 final AtomicBoolean connect;
138
139 final AtomicReference<PublishConnection<T>> current;
140
141 final AtomicReference<Disposable> upstream;
142
143 @SuppressWarnings("rawtypes")
144 static final InnerDisposable[] EMPTY = new InnerDisposable[0];
145
146 @SuppressWarnings("rawtypes")
147 static final InnerDisposable[] TERMINATED = new InnerDisposable[0];
148
149 Throwable error;
150
151 @SuppressWarnings("unchecked")
152 PublishConnection(AtomicReference<PublishConnection<T>> current) {
153 this.connect = new AtomicBoolean();
154 this.current = current;
155 this.upstream = new AtomicReference<>();
156 lazySet(EMPTY);
157 }
158
159 @SuppressWarnings("unchecked")
160 @Override
161 public void dispose() {
162 getAndSet(TERMINATED);
163 current.compareAndSet(this, null);
164 DisposableHelper.dispose(upstream);
165 }
166
167 @Override
168 public boolean isDisposed() {
169 return get() == TERMINATED;
170 }
171
172 @Override
173 public void onSubscribe(Disposable d) {
174 DisposableHelper.setOnce(upstream, d);
175 }
176
177 @Override
178 public void onNext(T t) {
179 for (InnerDisposable<T> inner : get()) {
180 inner.downstream.onNext(t);
181 }
182 }
183
184 @Override
185 @SuppressWarnings("unchecked")
186 public void onError(Throwable e) {
187 if (upstream.get() != DisposableHelper.DISPOSED) {
188 error = e;
189 upstream.lazySet(DisposableHelper.DISPOSED);
190 for (InnerDisposable<T> inner : getAndSet(TERMINATED)) {
191 inner.downstream.onError(e);
192 }
193 } else {
194 RxJavaPlugins.onError(e);
195 }
196 }
197
198 @Override
199 @SuppressWarnings("unchecked")
200 public void onComplete() {
201 upstream.lazySet(DisposableHelper.DISPOSED);
202 for (InnerDisposable<T> inner : getAndSet(TERMINATED)) {
203 inner.downstream.onComplete();
204 }
205 }
206
207 public boolean add(InnerDisposable<T> inner) {
208 for (;;) {
209 InnerDisposable<T>[] a = get();
210 if (a == TERMINATED) {
211 return false;
212 }
213 int n = a.length;
214 @SuppressWarnings("unchecked")
215 InnerDisposable<T>[] b = new InnerDisposable[n + 1];
216 System.arraycopy(a, 0, b, 0, n);
217 b[n] = inner;
218 if (compareAndSet(a, b)) {
219 return true;
220 }
221 }
222 }
223
224 @SuppressWarnings("unchecked")
225 public void remove(InnerDisposable<T> inner) {
226 for (;;) {
227 InnerDisposable<T>[] a = get();
228 int n = a.length;
229 if (n == 0) {
230 return;
231 }
232
233 int j = -1;
234 for (int i = 0; i < n; i++) {
235 if (a[i] == inner) {
236 j = i;
237 break;
238 }
239 }
240
241 if (j < 0) {
242 return;
243 }
244 InnerDisposable<T>[] b = EMPTY;
245 if (n != 1) {
246 b = new InnerDisposable[n - 1];
247 System.arraycopy(a, 0, b, 0, j);
248 System.arraycopy(a, j + 1, b, j, n - j - 1);
249 }
250 if (compareAndSet(a, b)) {
251 return;
252 }
253 }
254 }
255 }
256
257 /**
258 * Intercepts the dispose signal from the downstream and
259 * removes itself from the connection's observers array
260 * at most once.
261 * @param <T> the element type
262 */
263 static final class InnerDisposable<T>
264 extends AtomicReference<PublishConnection<T>>
265 implements Disposable {
266
267 private static final long serialVersionUID = 7463222674719692880L;
268
269 final Observer<? super T> downstream;
270
271 InnerDisposable(Observer<? super T> downstream, PublishConnection<T> parent) {
272 this.downstream = downstream;
273 lazySet(parent);
274 }
275
276 @Override
277 public void dispose() {
278 PublishConnection<T> p = getAndSet(null);
279 if (p != null) {
280 p.remove(this);
281 }
282 }
283
284 @Override
285 public boolean isDisposed() {
286 return get() == null;
287 }
288 }
289 }
290