| 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.subscribers; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.AtomicReference; |
| 17 |
|
| 18 |
import org.reactivestreams.Subscription; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.FlowableSubscriber; |
| 21 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 22 |
import io.reactivex.rxjava3.internal.util.QueueDrainHelper; |
| 23 |
import io.reactivex.rxjava3.operators.QueueSubscription; |
| 24 |
import io.reactivex.rxjava3.operators.SimpleQueue; |
| 25 |
|
| 26 |
/** |
| 27 |
* Subscriber that can fuse with the upstream and calls a support interface |
| 28 |
* whenever an event is available. |
| 29 |
* |
| 30 |
* @param <T> the value type |
| 31 |
*/ |
| 32 |
public final class InnerQueuedSubscriber<T> |
| 33 |
extends AtomicReference<Subscription> |
| 34 |
implements FlowableSubscriber<T>, Subscription { |
| 35 |
|
| 36 |
private static final long serialVersionUID = 22876611072430776L; |
| 37 |
|
| 38 |
final InnerQueuedSubscriberSupport<T> parent; |
| 39 |
|
| 40 |
final int prefetch; |
| 41 |
|
| 42 |
final int limit; |
| 43 |
|
| 44 |
volatile SimpleQueue<T> queue; |
| 45 |
|
| 46 |
volatile boolean done; |
| 47 |
|
| 48 |
long produced; |
| 49 |
|
| 50 |
int fusionMode; |
| 51 |
|
| 52 |
public InnerQueuedSubscriber(InnerQueuedSubscriberSupport<T> parent, int prefetch) { |
| 53 |
this.parent = parent; |
| 54 |
this.prefetch = prefetch; |
| 55 |
this.limit = prefetch - (prefetch >> 2); |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public void onSubscribe(Subscription s) { |
| 60 |
if (SubscriptionHelper.setOnce(this, s)) { |
| 61 |
if (s instanceof QueueSubscription) { |
| 62 |
@SuppressWarnings("unchecked") |
| 63 |
QueueSubscription<T> qs = (QueueSubscription<T>) s; |
| 64 |
|
| 65 |
int m = qs.requestFusion(QueueSubscription.ANY); |
| 66 |
if (m == QueueSubscription.SYNC) { |
| 67 |
fusionMode = m; |
| 68 |
queue = qs; |
| 69 |
done = true; |
| 70 |
parent.innerComplete(this); |
| 71 |
return; |
| 72 |
} |
| 73 |
if (m == QueueSubscription.ASYNC) { |
| 74 |
fusionMode = m; |
| 75 |
queue = qs; |
| 76 |
QueueDrainHelper.request(s, prefetch); |
| 77 |
return; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
queue = QueueDrainHelper.createQueue(prefetch); |
| 82 |
|
| 83 |
QueueDrainHelper.request(s, prefetch); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public void onNext(T t) { |
| 89 |
if (fusionMode == QueueSubscription.NONE) { |
| 90 |
parent.innerNext(this, t); |
| 91 |
} else { |
| 92 |
parent.drain(); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public void onError(Throwable t) { |
| 98 |
parent.innerError(this, t); |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public void onComplete() { |
| 103 |
parent.innerComplete(this); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void request(long n) { |
| 108 |
if (fusionMode != QueueSubscription.SYNC) { |
| 109 |
long p = produced + n; |
| 110 |
if (p >= limit) { |
| 111 |
produced = 0L; |
| 112 |
get().request(p); |
| 113 |
} else { |
| 114 |
produced = p; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public void cancel() { |
| 121 |
SubscriptionHelper.cancel(this); |
| 122 |
} |
| 123 |
|
| 124 |
public boolean isDone() { |
| 125 |
return done; |
| 126 |
} |
| 127 |
|
| 128 |
public void setDone() { |
| 129 |
this.done = true; |
| 130 |
} |
| 131 |
|
| 132 |
public SimpleQueue<T> queue() { |
| 133 |
return queue; |
| 134 |
} |
| 135 |
} |