| 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.core.Scheduler.Worker; |
| 23 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 24 |
import io.reactivex.rxjava3.exceptions.MissingBackpressureException; |
| 25 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 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 FlowableDebounceTimed<T> extends AbstractFlowableWithUpstream<T, T> { |
| 32 |
final long timeout; |
| 33 |
final TimeUnit unit; |
| 34 |
final Scheduler scheduler; |
| 35 |
|
| 36 |
public FlowableDebounceTimed(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> extends AtomicLong |
| 51 |
implements FlowableSubscriber<T>, Subscription { |
| 52 |
|
| 53 |
private static final long serialVersionUID = -9102637559663639004L; |
| 54 |
final Subscriber<? super T> downstream; |
| 55 |
final long timeout; |
| 56 |
final TimeUnit unit; |
| 57 |
final Scheduler.Worker worker; |
| 58 |
|
| 59 |
Subscription upstream; |
| 60 |
|
| 61 |
Disposable timer; |
| 62 |
|
| 63 |
volatile long index; |
| 64 |
|
| 65 |
boolean done; |
| 66 |
|
| 67 |
DebounceTimedSubscriber(Subscriber<? super T> actual, long timeout, TimeUnit unit, Worker worker) { |
| 68 |
this.downstream = actual; |
| 69 |
this.timeout = timeout; |
| 70 |
this.unit = unit; |
| 71 |
this.worker = worker; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public void onSubscribe(Subscription s) { |
| 76 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 77 |
this.upstream = s; |
| 78 |
downstream.onSubscribe(this); |
| 79 |
s.request(Long.MAX_VALUE); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public void onNext(T t) { |
| 85 |
if (done) { |
| 86 |
return; |
| 87 |
} |
| 88 |
long idx = index + 1; |
| 89 |
index = idx; |
| 90 |
|
| 91 |
Disposable d = timer; |
| 92 |
if (d != null) { |
| 93 |
d.dispose(); |
| 94 |
} |
| 95 |
|
| 96 |
DebounceEmitter<T> de = new DebounceEmitter<>(t, idx, this); |
| 97 |
timer = de; |
| 98 |
d = worker.schedule(de, timeout, unit); |
| 99 |
de.setResource(d); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void onError(Throwable t) { |
| 104 |
if (done) { |
| 105 |
RxJavaPlugins.onError(t); |
| 106 |
return; |
| 107 |
} |
| 108 |
done = true; |
| 109 |
Disposable d = timer; |
| 110 |
if (d != null) { |
| 111 |
d.dispose(); |
| 112 |
} |
| 113 |
downstream.onError(t); |
| 114 |
worker.dispose(); |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public void onComplete() { |
| 119 |
if (done) { |
| 120 |
return; |
| 121 |
} |
| 122 |
done = true; |
| 123 |
|
| 124 |
Disposable d = timer; |
| 125 |
if (d != null) { |
| 126 |
d.dispose(); |
| 127 |
} |
| 128 |
|
| 129 |
@SuppressWarnings("unchecked") |
| 130 |
DebounceEmitter<T> de = (DebounceEmitter<T>)d; |
| 131 |
if (de != null) { |
| 132 |
de.emit(); |
| 133 |
} |
| 134 |
|
| 135 |
downstream.onComplete(); |
| 136 |
worker.dispose(); |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public void request(long n) { |
| 141 |
if (SubscriptionHelper.validate(n)) { |
| 142 |
BackpressureHelper.add(this, n); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public void cancel() { |
| 148 |
upstream.cancel(); |
| 149 |
worker.dispose(); |
| 150 |
} |
| 151 |
|
| 152 |
void emit(long idx, T t, DebounceEmitter<T> emitter) { |
| 153 |
if (idx == index) { |
| 154 |
long r = get(); |
| 155 |
if (r != 0L) { |
| 156 |
downstream.onNext(t); |
| 157 |
BackpressureHelper.produced(this, 1); |
| 158 |
|
| 159 |
emitter.dispose(); |
| 160 |
} else { |
| 161 |
cancel(); |
| 162 |
downstream.onError(new MissingBackpressureException("Could not deliver value due to lack of requests")); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
static final class DebounceEmitter<T> extends AtomicReference<Disposable> implements Runnable, Disposable { |
| 169 |
|
| 170 |
private static final long serialVersionUID = 6812032969491025141L; |
| 171 |
|
| 172 |
final T value; |
| 173 |
final long idx; |
| 174 |
final DebounceTimedSubscriber<T> parent; |
| 175 |
|
| 176 |
final AtomicBoolean once = new AtomicBoolean(); |
| 177 |
|
| 178 |
DebounceEmitter(T value, long idx, DebounceTimedSubscriber<T> parent) { |
| 179 |
this.value = value; |
| 180 |
this.idx = idx; |
| 181 |
this.parent = parent; |
| 182 |
} |
| 183 |
|
| 184 |
@Override |
| 185 |
public void run() { |
| 186 |
emit(); |
| 187 |
} |
| 188 |
|
| 189 |
void emit() { |
| 190 |
if (once.compareAndSet(false, true)) { |
| 191 |
parent.emit(idx, value, this); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
@Override |
| 196 |
public void dispose() { |
| 197 |
DisposableHelper.dispose(this); |
| 198 |
} |
| 199 |
|
| 200 |
@Override |
| 201 |
public boolean isDisposed() { |
| 202 |
return get() == DisposableHelper.DISPOSED; |
| 203 |
} |
| 204 |
|
| 205 |
public void setResource(Disposable d) { |
| 206 |
DisposableHelper.replace(this, d); |
| 207 |
} |
| 208 |
} |
| 209 |
} |