|
21 | 21 | import rx.functions.*;
|
22 | 22 | import rx.internal.operators.*;
|
23 | 23 | import rx.internal.util.*;
|
| 24 | +import rx.observables.ConnectableObservable; |
24 | 25 | import rx.observers.*;
|
25 | 26 | import rx.plugins.RxJavaHooks;
|
26 | 27 | import rx.schedulers.Schedulers;
|
@@ -1269,6 +1270,62 @@ public static <R> Single<R> zip(Iterable<? extends Single<?>> singles, FuncN<? e
|
1269 | 1270 | return SingleOperatorZip.zip(iterableToArray, zipFunction);
|
1270 | 1271 | }
|
1271 | 1272 |
|
| 1273 | + /** |
| 1274 | + * Returns a Single that subscribes to this Single lazily, caches its success or error event |
| 1275 | + * and replays it to all the downstream subscribers. |
| 1276 | + * <p> |
| 1277 | + * <img width="640" height="410" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/cache.png" alt=""> |
| 1278 | + * <p> |
| 1279 | + * This is useful when you want a Single to cache its response and you can't control the |
| 1280 | + * subscribe/unsubscribe behavior of all the {@link Subscriber}s. |
| 1281 | + * <p> |
| 1282 | + * The operator subscribes only when the first downstream subscriber subscribes and maintains |
| 1283 | + * a single subscription towards this Single. In contrast, the operator family of {@link Observable#replay()} |
| 1284 | + * that return a {@link ConnectableObservable} require an explicit call to {@link ConnectableObservable#connect()}. |
| 1285 | + * <p> |
| 1286 | + * <em>Note:</em> You sacrifice the ability to unsubscribe from the origin when you use the {@code cache} |
| 1287 | + * Observer so be careful not to use this Observer on Observables that emit an infinite or very large number |
| 1288 | + * of items that will use up memory. |
| 1289 | + * A possible workaround is to apply `takeUntil` with a predicate or |
| 1290 | + * another source before (and perhaps after) the application of cache(). |
| 1291 | + * <pre><code> |
| 1292 | + * AtomicBoolean shouldStop = new AtomicBoolean(); |
| 1293 | + * |
| 1294 | + * source.takeUntil(v -> shouldStop.get()) |
| 1295 | + * .cache() |
| 1296 | + * .takeUntil(v -> shouldStop.get()) |
| 1297 | + * .subscribe(...); |
| 1298 | + * </code></pre> |
| 1299 | + * Since the operator doesn't allow clearing the cached values either, the possible workaround is |
| 1300 | + * to forget all references to it via {@link Observable#onTerminateDetach()} applied along with the previous |
| 1301 | + * workaround: |
| 1302 | + * <pre><code> |
| 1303 | + * AtomicBoolean shouldStop = new AtomicBoolean(); |
| 1304 | + * |
| 1305 | + * source.takeUntil(v -> shouldStop.get()) |
| 1306 | + * .onTerminateDetach() |
| 1307 | + * .cache() |
| 1308 | + * .takeUntil(v -> shouldStop.get()) |
| 1309 | + * .onTerminateDetach() |
| 1310 | + * .subscribe(...); |
| 1311 | + * </code></pre> |
| 1312 | + * <dl> |
| 1313 | + * <dt><b>Backpressure:</b></dt> |
| 1314 | + * <dd>The operator consumes this Single in an unbounded fashion but respects the backpressure |
| 1315 | + * of each downstream Subscriber individually.</dd> |
| 1316 | + * <dt><b>Scheduler:</b></dt> |
| 1317 | + * <dd>{@code cache} does not operate by default on a particular {@link Scheduler}.</dd> |
| 1318 | + * </dl> |
| 1319 | + * |
| 1320 | + * @return a Single that, when first subscribed to, caches its response for the |
| 1321 | + * benefit of subsequent subscribers |
| 1322 | + * @see <a href="http://reactivex.io/documentation/operators/replay.html">ReactiveX operators documentation: Replay</a> |
| 1323 | + */ |
| 1324 | + @Experimental |
| 1325 | + public final Single<T> cache() { |
| 1326 | + return toObservable().cacheWithInitialCapacity(1).toSingle(); |
| 1327 | + } |
| 1328 | + |
1272 | 1329 | /**
|
1273 | 1330 | * Returns an Observable that emits the item emitted by the source Single, then the item emitted by the
|
1274 | 1331 | * specified Single.
|
|
0 commit comments