All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rx.operators.OperatorTimeoutWithSelector Maven / Gradle / Ivy

There is a newer version: 0.20.7
Show newest version
/**
 * 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> firstTimeoutSelector,
            final Func1> timeoutSelector,
            Observable 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);
    }

}