| 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.TimeUnit; |
| 17 |
import java.util.concurrent.atomic.*; |
| 18 |
|
| 19 |
import org.reactivestreams.*; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.core.*; |
| 22 |
import io.reactivex.rxjava3.exceptions.MissingBackpressureException; |
| 23 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 24 |
import io.reactivex.rxjava3.internal.util.BackpressureHelper; |
| 25 |
|
| 26 |
/** |
| 27 |
* Emits the next or latest item when the given time elapses. |
| 28 |
* <p> |
| 29 |
* The operator emits the next item, then starts a timer. When the timer fires, |
| 30 |
* it tries to emit the latest item from upstream. If there was no upstream item, |
| 31 |
* in the meantime, the next upstream item is emitted immediately and the |
| 32 |
* timed process repeats. |
| 33 |
* <p>History: 2.1.14 - experimental |
| 34 |
* @param <T> the upstream and downstream value type |
| 35 |
* @since 2.2 |
| 36 |
*/ |
| 37 |
public final class FlowableThrottleLatest<T> extends AbstractFlowableWithUpstream<T, T> { |
| 38 |
|
| 39 |
final long timeout; |
| 40 |
|
| 41 |
final TimeUnit unit; |
| 42 |
|
| 43 |
final Scheduler scheduler; |
| 44 |
|
| 45 |
final boolean emitLast; |
| 46 |
|
| 47 |
public FlowableThrottleLatest(Flowable<T> source, |
| 48 |
long timeout, TimeUnit unit, Scheduler scheduler, |
| 49 |
boolean emitLast) { |
| 50 |
super(source); |
| 51 |
this.timeout = timeout; |
| 52 |
this.unit = unit; |
| 53 |
this.scheduler = scheduler; |
| 54 |
this.emitLast = emitLast; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
protected void subscribeActual(Subscriber<? super T> s) { |
| 59 |
source.subscribe(new ThrottleLatestSubscriber<>(s, timeout, unit, scheduler.createWorker(), emitLast)); |
| 60 |
} |
| 61 |
|
| 62 |
static final class ThrottleLatestSubscriber<T> |
| 63 |
extends AtomicInteger |
| 64 |
implements FlowableSubscriber<T>, Subscription, Runnable { |
| 65 |
|
| 66 |
private static final long serialVersionUID = -8296689127439125014L; |
| 67 |
|
| 68 |
final Subscriber<? super T> downstream; |
| 69 |
|
| 70 |
final long timeout; |
| 71 |
|
| 72 |
final TimeUnit unit; |
| 73 |
|
| 74 |
final Scheduler.Worker worker; |
| 75 |
|
| 76 |
final boolean emitLast; |
| 77 |
|
| 78 |
final AtomicReference<T> latest; |
| 79 |
|
| 80 |
final AtomicLong requested; |
| 81 |
|
| 82 |
Subscription upstream; |
| 83 |
|
| 84 |
volatile boolean done; |
| 85 |
Throwable error; |
| 86 |
|
| 87 |
volatile boolean cancelled; |
| 88 |
|
| 89 |
volatile boolean timerFired; |
| 90 |
|
| 91 |
long emitted; |
| 92 |
|
| 93 |
boolean timerRunning; |
| 94 |
|
| 95 |
ThrottleLatestSubscriber(Subscriber<? super T> downstream, |
| 96 |
long timeout, TimeUnit unit, Scheduler.Worker worker, |
| 97 |
boolean emitLast) { |
| 98 |
this.downstream = downstream; |
| 99 |
this.timeout = timeout; |
| 100 |
this.unit = unit; |
| 101 |
this.worker = worker; |
| 102 |
this.emitLast = emitLast; |
| 103 |
this.latest = new AtomicReference<>(); |
| 104 |
this.requested = new AtomicLong(); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public void onSubscribe(Subscription s) { |
| 109 |
if (SubscriptionHelper.validate(upstream, s)) { |
| 110 |
upstream = s; |
| 111 |
downstream.onSubscribe(this); |
| 112 |
s.request(Long.MAX_VALUE); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public void onNext(T t) { |
| 118 |
latest.set(t); |
| 119 |
drain(); |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public void onError(Throwable t) { |
| 124 |
error = t; |
| 125 |
done = true; |
| 126 |
drain(); |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public void onComplete() { |
| 131 |
done = true; |
| 132 |
drain(); |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public void request(long n) { |
| 137 |
if (SubscriptionHelper.validate(n)) { |
| 138 |
BackpressureHelper.add(requested, n); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public void cancel() { |
| 144 |
cancelled = true; |
| 145 |
upstream.cancel(); |
| 146 |
worker.dispose(); |
| 147 |
if (getAndIncrement() == 0) { |
| 148 |
latest.lazySet(null); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public void run() { |
| 154 |
timerFired = true; |
| 155 |
drain(); |
| 156 |
} |
| 157 |
|
| 158 |
void drain() { |
| 159 |
if (getAndIncrement() != 0) { |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
int missed = 1; |
| 164 |
|
| 165 |
AtomicReference<T> latest = this.latest; |
| 166 |
AtomicLong requested = this.requested; |
| 167 |
Subscriber<? super T> downstream = this.downstream; |
| 168 |
|
| 169 |
for (;;) { |
| 170 |
|
| 171 |
for (;;) { |
| 172 |
if (cancelled) { |
| 173 |
latest.lazySet(null); |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
boolean d = done; |
| 178 |
|
| 179 |
if (d && error != null) { |
| 180 |
latest.lazySet(null); |
| 181 |
downstream.onError(error); |
| 182 |
worker.dispose(); |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
T v = latest.get(); |
| 187 |
boolean empty = v == null; |
| 188 |
|
| 189 |
if (d) { |
| 190 |
if (!empty && emitLast) { |
| 191 |
v = latest.getAndSet(null); |
| 192 |
long e = emitted; |
| 193 |
if (e != requested.get()) { |
| 194 |
emitted = e + 1; |
| 195 |
downstream.onNext(v); |
| 196 |
downstream.onComplete(); |
| 197 |
} else { |
| 198 |
downstream.onError(new MissingBackpressureException( |
| 199 |
"Could not emit final value due to lack of requests")); |
| 200 |
} |
| 201 |
} else { |
| 202 |
latest.lazySet(null); |
| 203 |
downstream.onComplete(); |
| 204 |
} |
| 205 |
worker.dispose(); |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
if (empty) { |
| 210 |
if (timerFired) { |
| 211 |
timerRunning = false; |
| 212 |
timerFired = false; |
| 213 |
} |
| 214 |
break; |
| 215 |
} |
| 216 |
|
| 217 |
if (!timerRunning || timerFired) { |
| 218 |
v = latest.getAndSet(null); |
| 219 |
long e = emitted; |
| 220 |
if (e != requested.get()) { |
| 221 |
downstream.onNext(v); |
| 222 |
emitted = e + 1; |
| 223 |
} else { |
| 224 |
upstream.cancel(); |
| 225 |
downstream.onError(new MissingBackpressureException( |
| 226 |
"Could not emit value due to lack of requests")); |
| 227 |
worker.dispose(); |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
timerFired = false; |
| 232 |
timerRunning = true; |
| 233 |
worker.schedule(this, timeout, unit); |
| 234 |
} else { |
| 235 |
break; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
missed = addAndGet(-missed); |
| 240 |
if (missed == 0) { |
| 241 |
break; |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
} |