
io.reactivex.internal.operators.observable.ObservableTimeout Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava Show documentation
Show all versions of rxjava Show documentation
Reactive Extensions for Java
/**
* Copyright 2016 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 io.reactivex.internal.operators.observable;
import io.reactivex.internal.functions.ObjectHelper;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import io.reactivex.*;
import io.reactivex.disposables.Disposable;
import io.reactivex.exceptions.Exceptions;
import io.reactivex.functions.Function;
import io.reactivex.internal.disposables.*;
import io.reactivex.internal.observers.FullArbiterObserver;
import io.reactivex.observers.*;
import io.reactivex.plugins.RxJavaPlugins;
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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy