rx.operators.OperatorTimeoutWithSelector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava-core Show documentation
Show all versions of rxjava-core Show documentation
rxjava-core developed by Netflix
/**
* Copyright 2014 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 rx.operators;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.exceptions.Exceptions;
import rx.functions.Func0;
import rx.functions.Func1;
import rx.subscriptions.Subscriptions;
/**
* Returns an Observable that mirrors the source Observable. If either the first
* item emitted by the source Observable or any subsequent item don't arrive
* within time windows defined by provided Observables, switch to the
* other
Observable if provided, or emit a TimeoutException .
*/
public class OperatorTimeoutWithSelector extends
OperatorTimeoutBase {
public OperatorTimeoutWithSelector(
final Func0 extends Observable> firstTimeoutSelector,
final Func1 super T, ? extends Observable> timeoutSelector,
Observable extends T> other) {
super(new FirstTimeoutStub() {
@Override
public Subscription call(
final TimeoutSubscriber timeoutSubscriber,
final Long seqId) {
if (firstTimeoutSelector != null) {
Observable o = null;
try {
o = firstTimeoutSelector.call();
} catch (Throwable t) {
Exceptions.throwIfFatal(t);
timeoutSubscriber.onError(t);
return Subscriptions.empty();
}
return o.subscribe(new Subscriber() {
@Override
public void onCompleted() {
timeoutSubscriber.onTimeout(seqId);
}
@Override
public void onError(Throwable e) {
timeoutSubscriber.onError(e);
}
@Override
public void onNext(U t) {
timeoutSubscriber.onTimeout(seqId);
}
});
} else {
return Subscriptions.empty();
}
}
}, new TimeoutStub() {
@Override
public Subscription call(
final TimeoutSubscriber timeoutSubscriber,
final Long seqId, T value) {
Observable o = null;
try {
o = timeoutSelector.call(value);
} catch (Throwable t) {
Exceptions.throwIfFatal(t);
timeoutSubscriber.onError(t);
return Subscriptions.empty();
}
return o.subscribe(new Subscriber() {
@Override
public void onCompleted() {
timeoutSubscriber.onTimeout(seqId);
}
@Override
public void onError(Throwable e) {
timeoutSubscriber.onError(e);
}
@Override
public void onNext(V t) {
timeoutSubscriber.onTimeout(seqId);
}
});
}
}, other);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy