|
| 1 | +/** |
| 2 | + * Copyright 2014 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package rx.internal.operators; |
| 17 | + |
| 18 | +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
| 19 | + |
| 20 | +import rx.Observable; |
| 21 | +import rx.Observable.OnSubscribe; |
| 22 | +import rx.Subscriber; |
| 23 | +import rx.subjects.ReplaySubject; |
| 24 | +import rx.subjects.Subject; |
| 25 | + |
| 26 | +/** |
| 27 | + * This method has similar behavior to {@link Observable#replay()} except that this auto-subscribes |
| 28 | + * to the source Observable rather than returning a connectable Observable. |
| 29 | + * <p> |
| 30 | + * <img width="640" src="https://github.com/ReactiveX/RxJava/wiki/images/rx-operators/cache.png" alt=""> |
| 31 | + * <p> |
| 32 | + * This is useful with an Observable that you want to cache responses when you can't control the |
| 33 | + * subscribe/unsubscribe behavior of all the Observers. |
| 34 | + * <p> |
| 35 | + * <em>Note:</em> You sacrifice the ability to unsubscribe from the origin when you use this operator, so be |
| 36 | + * careful not to use this operator on Observables that emit infinite or very large numbers of |
| 37 | + * items, as this will use up memory. |
| 38 | + * |
| 39 | + * @param <T> |
| 40 | + * the cached value type |
| 41 | + */ |
| 42 | +public final class OnSubscribeCache<T> implements OnSubscribe<T> { |
| 43 | + protected final Observable<? extends T> source; |
| 44 | + protected final Subject<? super T, ? extends T> cache; |
| 45 | + volatile int sourceSubscribed; |
| 46 | + @SuppressWarnings("rawtypes") |
| 47 | + static final AtomicIntegerFieldUpdater<OnSubscribeCache> SRC_SUBSCRIBED_UPDATER |
| 48 | + = AtomicIntegerFieldUpdater.newUpdater(OnSubscribeCache.class, "sourceSubscribed"); |
| 49 | + |
| 50 | + public OnSubscribeCache(Observable<? extends T> source) { |
| 51 | + this(source, ReplaySubject.<T> create()); |
| 52 | + } |
| 53 | + |
| 54 | + public OnSubscribeCache(Observable<? extends T> source, int capacity) { |
| 55 | + this(source, ReplaySubject.<T> create(capacity)); |
| 56 | + } |
| 57 | + |
| 58 | + /* accessible to tests */OnSubscribeCache(Observable<? extends T> source, Subject<? super T, ? extends T> cache) { |
| 59 | + this.source = source; |
| 60 | + this.cache = cache; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void call(Subscriber<? super T> s) { |
| 65 | + if (SRC_SUBSCRIBED_UPDATER.compareAndSet(this, 0, 1)) { |
| 66 | + source.subscribe(cache); |
| 67 | + /* |
| 68 | + * Note that we will never unsubscribe from 'source' unless we receive `onCompleted` or `onError`, |
| 69 | + * as we want to receive and cache all of its values. |
| 70 | + * |
| 71 | + * This means this should never be used on an infinite or very large sequence, similar to toList(). |
| 72 | + */ |
| 73 | + } |
| 74 | + cache.unsafeSubscribe(s); |
| 75 | + } |
| 76 | +} |
0 commit comments