Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/mixed/ConcatMapXMainObserver.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.concurrent.atomic.AtomicInteger;
17
18 import io.reactivex.rxjava3.core.Observer;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
21 import io.reactivex.rxjava3.internal.util.*;
22 import io.reactivex.rxjava3.operators.QueueDisposable;
23 import io.reactivex.rxjava3.operators.QueueFuseable;
24 import io.reactivex.rxjava3.operators.SimpleQueue;
25 import io.reactivex.rxjava3.operators.SpscLinkedArrayQueue;
26
27 /**
28 * Base class for implementing concatMapX main observers.
29 *
30 * @param <T> the upstream value type
31 * @since 3.0.10
32 */
33 public abstract class ConcatMapXMainObserver<T> extends AtomicInteger
34 implements Observer<T>, Disposable {
35
36 private static final long serialVersionUID = -3214213361171757852L;
37
38 final AtomicThrowable errors;
39
40 final int prefetch;
41
42 final ErrorMode errorMode;
43
44 SimpleQueue<T> queue;
45
46 Disposable upstream;
47
48 volatile boolean done;
49
50 volatile boolean disposed;
51
52 public ConcatMapXMainObserver(int prefetch, ErrorMode errorMode) {
53 this.errorMode = errorMode;
54 this.errors = new AtomicThrowable();
55 this.prefetch = prefetch;
56 }
57
58 @Override
59 public final void onSubscribe(Disposable d) {
60 if (DisposableHelper.validate(upstream, d)) {
61 upstream = d;
62 if (d instanceof QueueDisposable) {
63 @SuppressWarnings("unchecked")
64 QueueDisposable<T> qd = (QueueDisposable<T>)d;
65 int mode = qd.requestFusion(QueueFuseable.ANY | QueueFuseable.BOUNDARY);
66 if (mode == QueueFuseable.SYNC) {
67 queue = qd;
68 done = true;
69
70 onSubscribeDownstream();
71
72 drain();
73 return;
74 }
75 else if (mode == QueueFuseable.ASYNC) {
76 queue = qd;
77
78 onSubscribeDownstream();
79
80 return;
81 }
82 }
83
84 queue = new SpscLinkedArrayQueue<>(prefetch);
85 onSubscribeDownstream();
86 }
87 }
88
89 @Override
90 public final void onNext(T t) {
91 // In async fusion mode, t is a drain indicator
92 if (t != null) {
93 queue.offer(t);
94 }
95 drain();
96 }
97
98 @Override
99 public final void onError(Throwable t) {
100 if (errors.tryAddThrowableOrReport(t)) {
101 if (errorMode == ErrorMode.IMMEDIATE) {
102 disposeInner();
103 }
104 done = true;
105 drain();
106 }
107 }
108
109 @Override
110 public final void onComplete() {
111 done = true;
112 drain();
113 }
114
115 @Override
116 public final void dispose() {
117 disposed = true;
118 upstream.dispose();
119 disposeInner();
120 errors.tryTerminateAndReport();
121 if (getAndIncrement() == 0) {
122 queue.clear();
123 clearValue();
124 }
125 }
126
127 @Override
128 public final boolean isDisposed() {
129 return disposed;
130 }
131
132 /**
133 * Override this to clear values when the downstream disposes.
134 */
135 void clearValue() {
136 }
137
138 /**
139 * Typically, this should be {@code downstream.onSubscribe(this)}.
140 */
141 abstract void onSubscribeDownstream();
142
143 /**
144 * Typically, this should be {@code inner.dispose()}.
145 */
146 abstract void disposeInner();
147
148 /**
149 * Implement the serialized inner subscribing and value emission here.
150 */
151 abstract void drain();
152 }