You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Part 3 - Taming the sequence/6. Hot and Cold observables.md
+59-1Lines changed: 59 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -56,7 +56,9 @@ There is a variant that takes a selector that transforms a sequence before publi
56
56
```java
57
57
publicfinal<R>Observable<R> publish(Func1<? super Observable<T>,? extends Observable<R>> selector)
58
58
```
59
-
The `selector` can do anything that we've learned to do on observables. This convenience method returns an `Observable<T>` instead of a `ConnectableObservable<T>`, so the connection functionality we are about to discuss does not apply there.
59
+
The `selector` can do anything that we've learned to do on observables. The usefulness of this is that a single subscription is made for the selector, which can be reused as much as needed. Without this overload, reusing the observable could lead to multiple subscriptions. There is no way to guarantee that the subscriptions would happen at the same exact time and therefore see the exact same sequence.
60
+
61
+
This method returns an `Observable<T>` instead of a `ConnectableObservable<T>`, so the connection functionality we are about to discuss does not apply there.
60
62
61
63
### connect
62
64
@@ -229,6 +231,62 @@ Second: 3
229
231
230
232
`replay` returns an `ConnectableObservable` like `publish`, so we can use the same ways to unsubscribe or create a `refCount` observable.
231
233
234
+
There are 8 overloads for `replay`.
235
+
```java
236
+
ConnectableObservable<T> replay()
237
+
<R>Observable<R> replay(Func1<? super Observable<T>,? extends Observable<R>> selector)
238
+
<R>Observable<R> replay(Func1<? super Observable<T>,? extends Observable<R>> selector, int bufferSize)
239
+
<R>Observable<R> replay(Func1<? super Observable<T>,? extends Observable<R>> selector, int bufferSize, long time, java.util.concurrent.TimeUnit unit)
240
+
<R>Observable<R> replay(Func1<? super Observable<T>,? extends Observable<R>> selector, long time, java.util.concurrent.TimeUnit unit)
241
+
ConnectableObservable<T> replay(int bufferSize)
242
+
ConnectableObservable<T> replay(int bufferSize, long time, java.util.concurrent.TimeUnit unit)
243
+
ConnectableObservable<T> replay(long time, java.util.concurrent.TimeUnit unit)
244
+
```
245
+
246
+
They are different ways of providing one or more of 3 parameters: `bufferSize`, `selector` and `time` (plus `unit` for time).
247
+
*`bufferSize` determines the maximum amount of items to be stored and replayed. Upon subscription, the observable will replay the last `bufferSize` number of items. Older items are forgotten. This is useful for conserving memory.
248
+
*`time`, `unit` determines how old an element can be before being forgotten. Uopn subscription, the observable will replay items that are newer than `time`.
249
+
*`selector` will transform the replayed observable, in the same way that `publish(selector)` works.
When we `connect`, the source begins emitting the sequence 0,1,2,3,4 in 1s intervals. We sleep for 4.5s before subscribing, which means that the source has emitted 0,1,2,3. 0 and 1 have fallen off the buffer, so only 2 and 3 are replayed. When 4 is emitted, we receive it normally.
270
+
271
+
When providing a time window, items fall off the buffer based on time
The `share` method is an alias for `Observable.publish().refCount()`. It allows your subscribers to share a subscription. The subscription is kept for as long as there are subscribers.
0 commit comments