io.reactivex.flowable.internal.operators.FlowableTimeout Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava3-flowable Show documentation
Show all versions of rxjava3-flowable Show documentation
rxjava3-flowable developed by David Karnok
The newest version!
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* 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 io.reactivex.flowable.internal.operators;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import org.reactivestreams.*;
import hu.akarnokd.reactivestreams.extensions.RelaxedSubscriber;
import io.reactivex.common.*;
import io.reactivex.common.exceptions.Exceptions;
import io.reactivex.common.functions.Function;
import io.reactivex.common.internal.disposables.DisposableHelper;
import io.reactivex.common.internal.functions.ObjectHelper;
import io.reactivex.flowable.Flowable;
import io.reactivex.flowable.internal.subscribers.FullArbiterSubscriber;
import io.reactivex.flowable.internal.subscriptions.*;
import io.reactivex.flowable.subscribers.*;
public final class FlowableTimeout extends AbstractFlowableWithUpstream {
final Publisher firstTimeoutIndicator;
final Function super T, ? extends Publisher> itemTimeoutIndicator;
final Publisher extends T> other;
public FlowableTimeout(
Flowable source,
Publisher firstTimeoutIndicator,
Function super T, ? extends Publisher> itemTimeoutIndicator,
Publisher extends T> other) {
super(source);
this.firstTimeoutIndicator = firstTimeoutIndicator;
this.itemTimeoutIndicator = itemTimeoutIndicator;
this.other = other;
}
@Override
protected void subscribeActual(Subscriber super T> s) {
if (other == null) {
source.subscribe(new TimeoutSubscriber(
new SerializedSubscriber(s),
firstTimeoutIndicator, itemTimeoutIndicator));
} else {
source.subscribe(new TimeoutOtherSubscriber(
s, firstTimeoutIndicator, itemTimeoutIndicator, other));
}
}
static final class TimeoutSubscriber implements RelaxedSubscriber, Subscription, OnTimeout {
final Subscriber super T> actual;
final Publisher firstTimeoutIndicator;
final Function super T, ? extends Publisher> itemTimeoutIndicator;
Subscription s;
volatile boolean cancelled;
volatile long index;
final AtomicReference timeout = new AtomicReference();
TimeoutSubscriber(Subscriber super T> actual,
Publisher firstTimeoutIndicator,
Function super T, ? extends Publisher> itemTimeoutIndicator) {
this.actual = actual;
this.firstTimeoutIndicator = firstTimeoutIndicator;
this.itemTimeoutIndicator = itemTimeoutIndicator;
}
@Override
public void onSubscribe(Subscription s) {
if (!SubscriptionHelper.validate(this.s, s)) {
return;
}
this.s = s;
if (cancelled) {
return;
}
Subscriber super T> a = actual;
Publisher p = firstTimeoutIndicator;
if (p != null) {
TimeoutInnerSubscriber tis = new TimeoutInnerSubscriber(this, 0);
if (timeout.compareAndSet(null, tis)) {
a.onSubscribe(this);
p.subscribe(tis);
}
} else {
a.onSubscribe(this);
}
}
@Override
public void onNext(T t) {
long idx = index + 1;
index = idx;
actual.onNext(t);
Disposable d = timeout.get();
if (d != null) {
d.dispose();
}
Publisher p;
try {
p = ObjectHelper.requireNonNull(itemTimeoutIndicator.apply(t), "The publisher returned is null");
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
cancel();
actual.onError(e);
return;
}
TimeoutInnerSubscriber tis = new TimeoutInnerSubscriber(this, idx);
if (timeout.compareAndSet(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();
DisposableHelper.dispose(timeout);
}
@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