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

rx.internal.operators.OnSubscribeMulticastSelector 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.internal.operators;

import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.Subscriber;
import rx.Subscription;
import rx.functions.Action1;
import rx.functions.Func0;
import rx.functions.Func1;
import rx.observables.ConnectableObservable;
import rx.observers.SafeSubscriber;
import rx.subjects.Subject;

/**
 * Returns an observable sequence that contains the elements of a sequence
 * produced by multicasting the source sequence within a selector function.
 *
 * @see MSDN: Observable.Multicast
 *
 * @param  the input value type
 * @param  the intermediate type
 * @param  the result type
 */
public final class OnSubscribeMulticastSelector implements OnSubscribe {
    final Observable source;
    final Func0> subjectFactory;
    final Func1, ? extends Observable> resultSelector;
    
    public OnSubscribeMulticastSelector(Observable source,
            Func0> subjectFactory,
            Func1, ? extends Observable> resultSelector) {
        this.source = source;
        this.subjectFactory = subjectFactory;
        this.resultSelector = resultSelector;
    }
    
    @Override
    public void call(Subscriber child) {
        Observable observable;
        ConnectableObservable connectable;
        try {
            Subject subject = subjectFactory.call();
            
            connectable = new OperatorMulticast(source, subject);
            
            observable = resultSelector.call(connectable);
        } catch (Throwable t) {
            child.onError(t);
            return;
        }
        
        final SafeSubscriber s = new SafeSubscriber(child);
        
        observable.unsafeSubscribe(s);
        
        connectable.connect(new Action1() {
            @Override
            public void call(Subscription t1) {
                s.add(t1);
            }
        });
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy