-
Notifications
You must be signed in to change notification settings - Fork 7.6k
1.x: Add cache() to Single #4757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
import rx.functions.*; | ||
import rx.internal.operators.*; | ||
import rx.internal.util.*; | ||
import rx.observables.ConnectableObservable; | ||
import rx.observers.*; | ||
import rx.plugins.RxJavaHooks; | ||
import rx.schedulers.Schedulers; | ||
|
@@ -1269,6 +1270,62 @@ public static <R> Single<R> zip(Iterable<? extends Single<?>> singles, FuncN<? e | |
return SingleOperatorZip.zip(iterableToArray, zipFunction); | ||
} | ||
|
||
/** | ||
* Returns a Single that subscribes to this Single lazily, caches its success or error event | ||
* and replays it to all the downstream subscribers. | ||
* <p> | ||
* <img width="640" height="410" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/cache.png" alt=""> | ||
* <p> | ||
* This is useful when you want a Single to cache its response and you can't control the | ||
* subscribe/unsubscribe behavior of all the {@link Subscriber}s. | ||
* <p> | ||
* The operator subscribes only when the first downstream subscriber subscribes and maintains | ||
* a single subscription towards this Single. In contrast, the operator family of {@link Observable#replay()} | ||
* that return a {@link ConnectableObservable} require an explicit call to {@link ConnectableObservable#connect()}. | ||
* <p> | ||
* <em>Note:</em> You sacrifice the ability to unsubscribe from the origin when you use the {@code cache} | ||
* Observer so be careful not to use this Observer on Observables that emit an infinite or very large number | ||
* of items that will use up memory. | ||
* A possible workaround is to apply `takeUntil` with a predicate or | ||
* another source before (and perhaps after) the application of cache(). | ||
* <pre><code> | ||
* AtomicBoolean shouldStop = new AtomicBoolean(); | ||
* | ||
* source.takeUntil(v -> shouldStop.get()) | ||
* .cache() | ||
* .takeUntil(v -> shouldStop.get()) | ||
* .subscribe(...); | ||
* </code></pre> | ||
* Since the operator doesn't allow clearing the cached values either, the possible workaround is | ||
* to forget all references to it via {@link Observable#onTerminateDetach()} applied along with the previous | ||
* workaround: | ||
* <pre><code> | ||
* AtomicBoolean shouldStop = new AtomicBoolean(); | ||
* | ||
* source.takeUntil(v -> shouldStop.get()) | ||
* .onTerminateDetach() | ||
* .cache() | ||
* .takeUntil(v -> shouldStop.get()) | ||
* .onTerminateDetach() | ||
* .subscribe(...); | ||
* </code></pre> | ||
* <dl> | ||
* <dt><b>Backpressure:</b></dt> | ||
* <dd>The operator consumes this Single in an unbounded fashion but respects the backpressure | ||
* of each downstream Subscriber individually.</dd> | ||
* <dt><b>Scheduler:</b></dt> | ||
* <dd>{@code cache} does not operate by default on a particular {@link Scheduler}.</dd> | ||
* </dl> | ||
* | ||
* @return a Single that, when first subscribed to, caches its response for the | ||
* benefit of subsequent subscribers | ||
* @see <a href="http://reactivex.io/documentation/operators/replay.html">ReactiveX operators documentation: Replay</a> | ||
*/ | ||
@Experimental | ||
public final Single<T> cache() { | ||
return toObservable().cacheWithInitialCapacity(1).toSingle(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose this is a start. Ideally we would have a dedicated operator implementation that was low-overhead since we know the only thing that needs cached is a single item or complete. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I figured I'd go for the low hanging fruit of just getting a working API out there rather than try and tackle some implementation of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought @JakeWharton wanted a native |
||
} | ||
|
||
/** | ||
* Returns an Observable that emits the item emitted by the source Single, then the item emitted by the | ||
* specified Single. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New operators need
@Experimental
annotation.