| 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.AtomicLong; |
| 18 |
|
| 19 |
import org.reactivestreams.*; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.core.*; |
| 22 |
import io.reactivex.rxjava3.core.Scheduler.Worker; |
| 23 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 24 |
import io.reactivex.rxjava3.exceptions.MissingBackpressureException; |
| 25 |
import io.reactivex.rxjava3.internal.disposables.SequentialDisposable; |
| 26 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 27 |
import io.reactivex.rxjava3.internal.util.BackpressureHelper; |
| 28 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 29 |
import io.reactivex.rxjava3.subscribers.SerializedSubscriber; |
| 30 |
|
| 31 |
public final class FlowableThrottleFirstTimed<T> extends AbstractFlowableWithUpstream<T, T> { |
| 32 |
final long timeout; |
| 33 |
final TimeUnit unit; |
| 34 |
final Scheduler scheduler; |
| 35 |
|
| 36 |
public FlowableThrottleFirstTimed(Flowable<T> source, long timeout, TimeUnit unit, Scheduler scheduler) { |
| 37 |
super(source); |
| 38 |
this.timeout = timeout; |
| 39 |
this.unit = unit; |
| 40 |
this.scheduler = scheduler; |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
protected void subscribeActual(Subscriber<? super T> s) { |
| 45 |
source.subscribe(new DebounceTimedSubscriber<>( |
| 46 |
new SerializedSubscriber<>(s), |
| 47 |
timeout, unit, scheduler.createWorker())); |
| 48 |
} |
| 49 |
|
| 50 |
static final class DebounceTimedSubscriber<T> |
| 51 |
extends AtomicLong |
| 52 |
implements FlowableSubscriber<T>, Subscription, Runnable { |
| 53 |
|
| 54 |
private static final long serialVersionUID = -9102637559663639004L; |
| 55 |
final Subscriber<? super T> downstream; |
| 56 |
final long timeout; |
| 57 |
final TimeUnit unit; |
| 58 |
final Scheduler.Worker worker; |
| 59 |
|
| 60 |
Subscription upstream; |
| 61 |
|
| 62 |
final SequentialDisposable timer = new SequentialDisposable(); |
| 63 |
|
| 64 |
volatile boolean gate; |
| 65 |
|
| 66 |
boolean done; |
| 67 |
|
| 68 |
DebounceTimedSubscriber(Subscriber<? super T> actual, long timeout, TimeUnit unit, Worker worker) { |
| 69 |
this.downstream = actual; |
| 70 |
this.timeout = timeout; |
| 71 |
this.unit = unit; |
| 72 |
this.worker = worker; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public void onSubscribe(Subscription s) { |
| 77 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 78 |
this.upstream = s; |
| 79 |
downstream.onSubscribe(this); |
| 80 |
s.request(Long.MAX_VALUE); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public void onNext(T t) { |
| 86 |
if (done) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
if (!gate) { |
| 91 |
gate = true; |
| 92 |
long r = get(); |
| 93 |
if (r != 0L) { |
| 94 |
downstream.onNext(t); |
| 95 |
BackpressureHelper.produced(this, 1); |
| 96 |
} else { |
| 97 |
done = true; |
| 98 |
cancel(); |
| 99 |
downstream.onError(new MissingBackpressureException("Could not deliver value due to lack of requests")); |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
Disposable d = timer.get(); |
| 104 |
if (d != null) { |
| 105 |
d.dispose(); |
| 106 |
} |
| 107 |
|
| 108 |
timer.replace(worker.schedule(this, timeout, unit)); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public void run() { |
| 114 |
gate = false; |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public void onError(Throwable t) { |
| 119 |
if (done) { |
| 120 |
RxJavaPlugins.onError(t); |
| 121 |
return; |
| 122 |
} |
| 123 |
done = true; |
| 124 |
downstream.onError(t); |
| 125 |
worker.dispose(); |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public void onComplete() { |
| 130 |
if (done) { |
| 131 |
return; |
| 132 |
} |
| 133 |
done = true; |
| 134 |
downstream.onComplete(); |
| 135 |
worker.dispose(); |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public void request(long n) { |
| 140 |
if (SubscriptionHelper.validate(n)) { |
| 141 |
BackpressureHelper.add(this, n); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public void cancel() { |
| 147 |
upstream.cancel(); |
| 148 |
worker.dispose(); |
| 149 |
} |
| 150 |
} |
| 151 |
} |