hu.akarnokd.rxjava2.internal.operators.OperatorDebounceTimed Maven / Gradle / Ivy
/**
* Copyright 2015 David Karnok and Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package hu.akarnokd.rxjava2.internal.operators;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.*;
import org.reactivestreams.*;
import hu.akarnokd.rxjava2.Observable.Operator;
import hu.akarnokd.rxjava2.Scheduler;
import hu.akarnokd.rxjava2.Scheduler.Worker;
import hu.akarnokd.rxjava2.disposables.Disposable;
import hu.akarnokd.rxjava2.internal.subscriptions.SubscriptionHelper;
import hu.akarnokd.rxjava2.internal.util.BackpressureHelper;
import hu.akarnokd.rxjava2.plugins.RxJavaPlugins;
import hu.akarnokd.rxjava2.subscribers.SerializedSubscriber;
public final class OperatorDebounceTimed implements Operator {
final long timeout;
final TimeUnit unit;
final Scheduler scheduler;
public OperatorDebounceTimed(long timeout, TimeUnit unit, Scheduler scheduler) {
this.timeout = timeout;
this.unit = unit;
this.scheduler = scheduler;
}
@Override
public Subscriber super T> apply(Subscriber super T> t) {
return new DebounceTimedSubscriber(
new SerializedSubscriber(t),
timeout, unit, scheduler.createWorker());
}
static final class DebounceTimedSubscriber extends AtomicLong
implements Subscriber, Subscription {
/** */
private static final long serialVersionUID = -9102637559663639004L;
final Subscriber super T> actual;
final long timeout;
final TimeUnit unit;
final Scheduler.Worker worker;
Subscription s;
volatile Disposable timer;
@SuppressWarnings("rawtypes")
static final AtomicReferenceFieldUpdater TIMER =
AtomicReferenceFieldUpdater.newUpdater(DebounceTimedSubscriber.class, Disposable.class, "timer");
static final Disposable CANCELLED = new Disposable() {
@Override
public void dispose() { }
};
static final Disposable NEW_TIMER = new Disposable() {
@Override
public void dispose() { }
};
volatile long index;
boolean done;
public DebounceTimedSubscriber(Subscriber super T> actual, long timeout, TimeUnit unit, Worker worker) {
this.actual = actual;
this.timeout = timeout;
this.unit = unit;
this.worker = worker;
}
public void disposeTimer() {
Disposable d = timer;
if (d != CANCELLED) {
d = TIMER.getAndSet(this, CANCELLED);
if (d != CANCELLED && d != null) {
d.dispose();
}
}
}
@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.validateSubscription(this.s, s)) {
return;
}
this.s = s;
actual.onSubscribe(this);
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(T t) {
if (done) {
return;
}
long idx = index + 1;
index = idx;
Disposable d = timer;
if (d != null) {
d.dispose();
}
DebounceEmitter de = new DebounceEmitter(t, idx, this);
if (!TIMER.compareAndSet(this, d, de)) {
return;
}
d = worker.schedule(de, timeout, unit);
de.setResource(d);
}
@Override
public void onError(Throwable t) {
if (done) {
RxJavaPlugins.onError(t);
return;
}
done = true;
disposeTimer();
actual.onError(t);
}
@Override
public void onComplete() {
if (done) {
return;
}
done = true;
Disposable d = timer;
if (d != CANCELLED) {
@SuppressWarnings("unchecked")
DebounceEmitter de = (DebounceEmitter)d;
de.emit();
disposeTimer();
worker.dispose();
actual.onComplete();
}
}
@Override
public void request(long n) {
if (SubscriptionHelper.validateRequest(n)) {
return;
}
BackpressureHelper.add(this, n);
}
@Override
public void cancel() {
disposeTimer();
worker.dispose();
s.cancel();
}
void emit(long idx, T t, DebounceEmitter emitter) {
if (idx == index) {
long r = get();
if (r != 0L) {
actual.onNext(t);
if (r != Long.MAX_VALUE) {
decrementAndGet();
}
emitter.dispose();
} else {
cancel();
actual.onError(new IllegalStateException("Could not deliver value due to lack of requests"));
}
}
}
}
static final class DebounceEmitter extends AtomicReference implements Runnable, Disposable {
/** */
private static final long serialVersionUID = 6812032969491025141L;
static final Disposable DISPOSED = new Disposable() {
@Override
public void dispose() { }
};
final T value;
final long idx;
final DebounceTimedSubscriber parent;
volatile int once;
@SuppressWarnings("rawtypes")
static final AtomicIntegerFieldUpdater ONCE =
AtomicIntegerFieldUpdater.newUpdater(DebounceEmitter.class, "once");
public DebounceEmitter(T value, long idx, DebounceTimedSubscriber parent) {
this.value = value;
this.idx = idx;
this.parent = parent;
}
@Override
public void run() {
emit();
}
void emit() {
if (ONCE.compareAndSet(this, 0, 1)) {
parent.emit(idx, value, this);
}
}
@Override
public void dispose() {
Disposable d = get();
if (d != DISPOSED) {
d = getAndSet(DISPOSED);
if (d != DISPOSED && d != null) {
d.dispose();
}
}
}
public void setResource(Disposable d) {
for (;;) {
Disposable a = get();
if (a == DISPOSED) {
d.dispose();
return;
}
if (compareAndSet(a, d)) {
return;
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy