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

rx.operators.OperationCache 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 java.util.concurrent.atomic.AtomicBoolean;

import rx.Observable;
import rx.Observable.OnSubscribeFunc;
import rx.Observer;
import rx.Subscription;
import rx.subjects.ReplaySubject;

/**
 * This method has similar behavior to {@link Observable#replay()} except that this auto-subscribes
 * to the source Observable rather than returning a connectable Observable.
 * 

* *

* This is useful with an Observable that you want to cache responses when you can't control the * subscribe/unsubscribe behavior of all the Observers. *

* NOTE: You sacrifice the ability to unsubscribe from the origin when you use this operator, so be * careful not to use this operator on Observables that emit infinite or very large numbers of * items, as this will use up memory. */ public class OperationCache { public static OnSubscribeFunc cache(final Observable source) { return new OnSubscribeFunc() { final AtomicBoolean subscribed = new AtomicBoolean(false); private final ReplaySubject cache = ReplaySubject.create(); @Override public Subscription onSubscribe(Observer observer) { if (subscribed.compareAndSet(false, true)) { // subscribe to the source once source.subscribe(cache); /* * Note that we will never unsubscribe from 'source' as we want to receive and cache all of its values. * * This means this should never be used on an infinite or very large sequence, similar to toList(). */ } return cache.subscribe(observer); } }; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy