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

rx.internal.operators.OperatorMulticast 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.Subscriber;
import rx.Subscription;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.observables.ConnectableObservable;
import rx.subjects.Subject;
import rx.subscriptions.Subscriptions;

/**
 * Shares a single subscription to a source through a Subject.
 * 
 * @param  the source value type
 * @param  the result value type
 */
public final class OperatorMulticast extends ConnectableObservable {
    final Observable source;
    final Subject subject;
    final Object guard = new Object();
    /** Guarded by guard. */
    Subscription subscription;

    public OperatorMulticast(Observable source, final Subject subject) {
        super(new OnSubscribe() {
                @Override
                public void call(Subscriber subscriber) {
                    subject.unsafeSubscribe(subscriber);
                }
            });
        this.source = source;
        this.subject = subject;
    }

    @Override
    public void connect(Action1 connection) {
        connection.call(Subscriptions.create(new Action0() {
            @Override
            public void call() {
                Subscription s;
                synchronized (guard) {
                    s = subscription;
                    subscription = null;
                }
                if (s != null) {
                    s.unsubscribe();
                }
            }
        }));
        Subscriber s = null;
        synchronized (guard) {
            if (subscription == null) {
                s = new Subscriber() {
                    @Override
                    public void onCompleted() {
                        subject.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        subject.onError(e);
                    }

                    @Override
                    public void onNext(T args) {
                        subject.onNext(args);
                    }
                };
                subscription = s;
            }
        }
        if (s != null) {
            source.unsafeSubscribe(s);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy