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