| 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.flowable; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.*; |
| 17 |
|
| 18 |
import org.reactivestreams.*; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.exceptions.MissingBackpressureException; |
| 22 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 23 |
import io.reactivex.rxjava3.internal.util.BackpressureHelper; |
| 24 |
import io.reactivex.rxjava3.subscribers.SerializedSubscriber; |
| 25 |
|
| 26 |
public final class FlowableSamplePublisher<T> extends Flowable<T> { |
| 27 |
final Publisher<T> source; |
| 28 |
final Publisher<?> other; |
| 29 |
|
| 30 |
final boolean emitLast; |
| 31 |
|
| 32 |
public FlowableSamplePublisher(Publisher<T> source, Publisher<?> other, boolean emitLast) { |
| 33 |
this.source = source; |
| 34 |
this.other = other; |
| 35 |
this.emitLast = emitLast; |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
protected void subscribeActual(Subscriber<? super T> s) { |
| 40 |
SerializedSubscriber<T> serial = new SerializedSubscriber<>(s); |
| 41 |
if (emitLast) { |
| 42 |
source.subscribe(new SampleMainEmitLast<>(serial, other)); |
| 43 |
} else { |
| 44 |
source.subscribe(new SampleMainNoLast<>(serial, other)); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
abstract static class SamplePublisherSubscriber<T> extends AtomicReference<T> implements FlowableSubscriber<T>, Subscription { |
| 49 |
|
| 50 |
private static final long serialVersionUID = -3517602651313910099L; |
| 51 |
|
| 52 |
final Subscriber<? super T> downstream; |
| 53 |
final Publisher<?> sampler; |
| 54 |
|
| 55 |
final AtomicLong requested = new AtomicLong(); |
| 56 |
|
| 57 |
final AtomicReference<Subscription> other = new AtomicReference<>(); |
| 58 |
|
| 59 |
Subscription upstream; |
| 60 |
|
| 61 |
SamplePublisherSubscriber(Subscriber<? super T> actual, Publisher<?> other) { |
| 62 |
this.downstream = actual; |
| 63 |
this.sampler = other; |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public void onSubscribe(Subscription s) { |
| 68 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 69 |
this.upstream = s; |
| 70 |
downstream.onSubscribe(this); |
| 71 |
if (other.get() == null) { |
| 72 |
sampler.subscribe(new SamplerSubscriber<>(this)); |
| 73 |
s.request(Long.MAX_VALUE); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void onNext(T t) { |
| 81 |
lazySet(t); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public void onError(Throwable t) { |
| 86 |
SubscriptionHelper.cancel(other); |
| 87 |
downstream.onError(t); |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public void onComplete() { |
| 92 |
SubscriptionHelper.cancel(other); |
| 93 |
completion(); |
| 94 |
} |
| 95 |
|
| 96 |
void setOther(Subscription o) { |
| 97 |
SubscriptionHelper.setOnce(other, o, Long.MAX_VALUE); |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public void request(long n) { |
| 102 |
if (SubscriptionHelper.validate(n)) { |
| 103 |
BackpressureHelper.add(requested, n); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public void cancel() { |
| 109 |
SubscriptionHelper.cancel(other); |
| 110 |
upstream.cancel(); |
| 111 |
} |
| 112 |
|
| 113 |
public void error(Throwable e) { |
| 114 |
upstream.cancel(); |
| 115 |
downstream.onError(e); |
| 116 |
} |
| 117 |
|
| 118 |
public void complete() { |
| 119 |
upstream.cancel(); |
| 120 |
completion(); |
| 121 |
} |
| 122 |
|
| 123 |
void emit() { |
| 124 |
T value = getAndSet(null); |
| 125 |
if (value != null) { |
| 126 |
long r = requested.get(); |
| 127 |
if (r != 0L) { |
| 128 |
downstream.onNext(value); |
| 129 |
BackpressureHelper.produced(requested, 1); |
| 130 |
} else { |
| 131 |
cancel(); |
| 132 |
downstream.onError(new MissingBackpressureException("Couldn't emit value due to lack of requests!")); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
abstract void completion(); |
| 138 |
|
| 139 |
abstract void run(); |
| 140 |
} |
| 141 |
|
| 142 |
static final class SamplerSubscriber<T> implements FlowableSubscriber<Object> { |
| 143 |
final SamplePublisherSubscriber<T> parent; |
| 144 |
SamplerSubscriber(SamplePublisherSubscriber<T> parent) { |
| 145 |
this.parent = parent; |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public void onSubscribe(Subscription s) { |
| 151 |
parent.setOther(s); |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public void onNext(Object t) { |
| 156 |
parent.run(); |
| 157 |
} |
| 158 |
|
| 159 |
@Override |
| 160 |
public void onError(Throwable t) { |
| 161 |
parent.error(t); |
| 162 |
} |
| 163 |
|
| 164 |
@Override |
| 165 |
public void onComplete() { |
| 166 |
parent.complete(); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
static final class SampleMainNoLast<T> extends SamplePublisherSubscriber<T> { |
| 171 |
|
| 172 |
private static final long serialVersionUID = -3029755663834015785L; |
| 173 |
|
| 174 |
SampleMainNoLast(Subscriber<? super T> actual, Publisher<?> other) { |
| 175 |
super(actual, other); |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
void completion() { |
| 180 |
downstream.onComplete(); |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
void run() { |
| 185 |
emit(); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
static final class SampleMainEmitLast<T> extends SamplePublisherSubscriber<T> { |
| 190 |
|
| 191 |
private static final long serialVersionUID = -3029755663834015785L; |
| 192 |
|
| 193 |
final AtomicInteger wip; |
| 194 |
|
| 195 |
volatile boolean done; |
| 196 |
|
| 197 |
SampleMainEmitLast(Subscriber<? super T> actual, Publisher<?> other) { |
| 198 |
super(actual, other); |
| 199 |
this.wip = new AtomicInteger(); |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
void completion() { |
| 204 |
done = true; |
| 205 |
if (wip.getAndIncrement() == 0) { |
| 206 |
emit(); |
| 207 |
downstream.onComplete(); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
@Override |
| 212 |
void run() { |
| 213 |
if (wip.getAndIncrement() == 0) { |
| 214 |
do { |
| 215 |
boolean d = done; |
| 216 |
emit(); |
| 217 |
if (d) { |
| 218 |
downstream.onComplete(); |
| 219 |
return; |
| 220 |
} |
| 221 |
} while (wip.decrementAndGet() != 0); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
} |