rx.operators.OperationRefCount 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.Observer;
import rx.Subscription;
import rx.functions.Action0;
import rx.observables.ConnectableObservable;
import rx.subscriptions.Subscriptions;
/**
* Returns an observable sequence that stays connected to the source as long
* as there is at least one subscription to the observable sequence.
*/
public final class OperationRefCount {
public static Observable.OnSubscribeFunc refCount(ConnectableObservable connectableObservable) {
return new RefCount(connectableObservable);
}
private static class RefCount implements Observable.OnSubscribeFunc {
private final ConnectableObservable innerConnectableObservable;
private final Object gate = new Object();
private int count = 0;
private Subscription connection = null;
public RefCount(ConnectableObservable innerConnectableObservable) {
this.innerConnectableObservable = innerConnectableObservable;
}
@Override
public Subscription onSubscribe(Observer super T> observer) {
final Subscription subscription = innerConnectableObservable.subscribe(observer);
synchronized (gate) {
if (count++ == 0) {
connection = innerConnectableObservable.connect();
}
}
return Subscriptions.create(new Action0() {
@Override
public void call() {
synchronized (gate) {
if (--count == 0) {
connection.unsubscribe();
connection = null;
}
}
subscription.unsubscribe();
}
});
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy