hu.akarnokd.rxjava2.internal.operators.OperatorTimeout 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.TimeoutException;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import org.reactivestreams.*;
import hu.akarnokd.rxjava2.Observable.Operator;
import hu.akarnokd.rxjava2.disposables.Disposable;
import hu.akarnokd.rxjava2.functions.*;
import hu.akarnokd.rxjava2.internal.subscribers.*;
import hu.akarnokd.rxjava2.internal.subscriptions.*;
import hu.akarnokd.rxjava2.plugins.RxJavaPlugins;
import hu.akarnokd.rxjava2.subscribers.SerializedSubscriber;
public final class OperatorTimeout implements Operator {
final Supplier extends Publisher> firstTimeoutSelector;
final Function super T, ? extends Publisher> timeoutSelector;
final Publisher extends T> other;
public OperatorTimeout(Supplier extends Publisher> firstTimeoutSelector,
Function super T, ? extends Publisher> timeoutSelector, Publisher extends T> other) {
this.firstTimeoutSelector = firstTimeoutSelector;
this.timeoutSelector = timeoutSelector;
this.other = other;
}
@Override
public Subscriber super T> apply(Subscriber super T> t) {
if (other == null) {
return new TimeoutSubscriber(
new SerializedSubscriber(t),
firstTimeoutSelector, timeoutSelector);
}
return new TimeoutOtherSubscriber(t, firstTimeoutSelector, timeoutSelector, other);
}
static final class TimeoutSubscriber implements Subscriber, Subscription, OnTimeout {
final Subscriber super T> actual;
final Supplier extends Publisher> firstTimeoutSelector;
final Function super T, ? extends Publisher> timeoutSelector;
Subscription s;
volatile boolean cancelled;
volatile long index;
volatile Disposable timeout;
@SuppressWarnings("rawtypes")
static final AtomicReferenceFieldUpdater TIMEOUT =
AtomicReferenceFieldUpdater.newUpdater(TimeoutSubscriber.class, Disposable.class, "timeout");
static final Disposable CANCELLED = new Disposable() {
@Override
public void dispose() { }
};
public TimeoutSubscriber(Subscriber super T> actual,
Supplier extends Publisher> firstTimeoutSelector,
Function super T, ? extends Publisher> timeoutSelector) {
this.actual = actual;
this.firstTimeoutSelector = firstTimeoutSelector;
this.timeoutSelector = timeoutSelector;
}
@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.validateSubscription(this.s, s)) {
return;
}
this.s = s;
if (cancelled) {
return;
}
Subscriber super T> a = actual;
Publisher p;
if (firstTimeoutSelector != null) {
try {
p = firstTimeoutSelector.get();
} catch (Exception ex) {
cancel();
EmptySubscription.error(ex, a);
return;
}
if (p == null) {
cancel();
EmptySubscription.error(new NullPointerException("The first timeout publisher is null"), a);
return;
}
TimeoutInnerSubscriber tis = new TimeoutInnerSubscriber(this, 0);
if (TIMEOUT.compareAndSet(this, null, tis)) {
a.onSubscribe(s);
p.subscribe(tis);
}
} else {
a.onSubscribe(s);
}
}
@Override
public void onNext(T t) {
long idx = index + 1;
index = idx;
actual.onNext(t);
Disposable d = timeout;
if (d != null) {
d.dispose();
}
Publisher p;
try {
p = timeoutSelector.apply(t);
} catch (Throwable e) {
cancel();
actual.onError(e);
return;
}
if (p == null) {
cancel();
actual.onError(new NullPointerException("The publisher returned is null"));
return;
}
TimeoutInnerSubscriber tis = new TimeoutInnerSubscriber(this, idx);
if (TIMEOUT.compareAndSet(this, d, tis)) {
p.subscribe(tis);
}
}
@Override
public void onError(Throwable t) {
cancel();
actual.onError(t);
}
@Override
public void onComplete() {
cancel();
actual.onComplete();
}
@Override
public void request(long n) {
s.request(n);
}
@Override
public void cancel() {
cancelled = true;
s.cancel();
Disposable d = timeout;
if (d != CANCELLED) {
d = TIMEOUT.getAndSet(this, CANCELLED);
if (d != CANCELLED && d != null) {
d.dispose();
}
}
}
@Override
public void timeout(long idx) {
if (idx == index) {
cancel();
actual.onError(new TimeoutException());
}
}
}
interface OnTimeout {
void timeout(long index);
void onError(Throwable e);
}
static final class TimeoutInnerSubscriber extends DisposableSubscriber
© 2015 - 2025 Weber Informatics LLC | Privacy Policy