io.reactivex.observable.internal.operators.ObservableTimeout Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava3-observable Show documentation
Show all versions of rxjava3-observable Show documentation
rxjava3-observable 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.observable.internal.operators;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
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.observable.*;
import io.reactivex.observable.internal.disposables.ObserverFullArbiter;
import io.reactivex.observable.internal.observers.FullArbiterObserver;
import io.reactivex.observable.observers.*;
public final class ObservableTimeout extends AbstractObservableWithUpstream {
final ObservableSource firstTimeoutIndicator;
final Function super T, ? extends ObservableSource> itemTimeoutIndicator;
final ObservableSource extends T> other;
public ObservableTimeout(
ObservableSource source,
ObservableSource firstTimeoutIndicator,
Function super T, ? extends ObservableSource> itemTimeoutIndicator,
ObservableSource extends T> other) {
super(source);
this.firstTimeoutIndicator = firstTimeoutIndicator;
this.itemTimeoutIndicator = itemTimeoutIndicator;
this.other = other;
}
@Override
public void subscribeActual(Observer super T> t) {
if (other == null) {
source.subscribe(new TimeoutObserver(
new SerializedObserver(t),
firstTimeoutIndicator, itemTimeoutIndicator));
} else {
source.subscribe(new TimeoutOtherObserver(
t, firstTimeoutIndicator, itemTimeoutIndicator, other));
}
}
static final class TimeoutObserver
extends AtomicReference
implements Observer, Disposable, OnTimeout {
private static final long serialVersionUID = 2672739326310051084L;
final Observer super T> actual;
final ObservableSource firstTimeoutIndicator;
final Function super T, ? extends ObservableSource> itemTimeoutIndicator;
Disposable s;
volatile long index;
TimeoutObserver(Observer super T> actual,
ObservableSource firstTimeoutIndicator,
Function super T, ? extends ObservableSource> itemTimeoutIndicator) {
this.actual = actual;
this.firstTimeoutIndicator = firstTimeoutIndicator;
this.itemTimeoutIndicator = itemTimeoutIndicator;
}
@Override
public void onSubscribe(Disposable s) {
if (DisposableHelper.validate(this.s, s)) {
this.s = s;
Observer super T> a = actual;
ObservableSource p = firstTimeoutIndicator;
if (p != null) {
TimeoutInnerObserver tis = new TimeoutInnerObserver(this, 0);
if (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 = get();
if (d != null) {
d.dispose();
}
ObservableSource p;
try {
p = ObjectHelper.requireNonNull(itemTimeoutIndicator.apply(t), "The ObservableSource returned is null");
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
dispose();
actual.onError(e);
return;
}
TimeoutInnerObserver tis = new TimeoutInnerObserver(this, idx);
if (compareAndSet(d, tis)) {
p.subscribe(tis);
}
}
@Override
public void onError(Throwable t) {
DisposableHelper.dispose(this);
actual.onError(t);
}
@Override
public void onComplete() {
DisposableHelper.dispose(this);
actual.onComplete();
}
@Override
public void dispose() {
if (DisposableHelper.dispose(this)) {
s.dispose();
}
}
@Override
public boolean isDisposed() {
return s.isDisposed();
}
@Override
public void timeout(long idx) {
if (idx == index) {
dispose();
actual.onError(new TimeoutException());
}
}
@Override
public void innerError(Throwable e) {
s.dispose();
actual.onError(e);
}
}
interface OnTimeout {
void timeout(long index);
void innerError(Throwable e);
}
static final class TimeoutInnerObserver extends DisposableObserver