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
+16-16Lines changed: 16 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
# Hot and Cold observables
2
2
3
-
Observable sequences come in two flavours that have important differences. They are called "hot" and "cold". In this chapter, we will explain what each type is and what that means for you as an Rx developer.
3
+
Observable sequences come in two flavours, called "hot" and "cold", that have important differences. In this chapter, we will explain what each type is and what that means for you as an Rx developer.
4
4
5
5
## Cold observables
6
6
7
-
Cold observables are observables that run their sequence when and if they are subscribed to. They present the sequence from its start to each subscriber. An example of a cold observable would be `Observable.interval`. Regardless of when it is created and when it is subscribed to, it will generate the same sequence for every subscriber.
7
+
Cold observables are observables that run their sequence when and if they are subscribed to. They present the sequence from the start to each subscriber. An example of a cold observable would be `Observable.interval`. Regardless of when it is created and when it is subscribed to, it will generate the same sequence for every subscriber.
The two subscribers don't receive the same value at the same time, even though they are both subscribed to the same observable. They do see the same sequence, except that each of them sees it as having begun when they subscribed.
30
30
31
-
The code samples that we've seen in this guide so far have been cold observables, because cold obserables are easier to reason about. Every observable that is created with the `Observable.create` is a cold observable. That includes all the shorthands that we've seen, such as `just`, `range`, `timer` and `from`.
31
+
The code samples that we've seen in this guide so far have been cold observables, because cold observables are easier to reason about. Every observable that is created with the `Observable.create` is a cold observable. That includes all the shorthands that we've seen, such as `just`, `range`, `timer` and `from`.
32
32
33
33
Cold observables don't necessarily present the same sequence to each subscriber. If, for example, an observable connects to a database and emits the results of a query, the actual value will depend on the state of the database at the time of subscription. It is the fact that a subscriber will receive the whole query from the start that makes this observable cold.
34
34
35
35
## Hot observables
36
36
37
-
Hot observables emit values regardless of subscriptions. They have their own timeline and items are emitted whether someone is listening or not. An example of this is mouse events. A mouse is generating event regardless of whether there is a subscription listening for them. When a subscription is made, the observer receives current events as they happen. You don't receive and you don't want to receive a recap of everything that your mouse has done since you booted your system. When you unsubscribe, it doesn't stop your mouse from generating events either. You're just not receiving them. If you resubscribe, you will again see current events with no recap of what you've missed.
37
+
Hot observables emit values independent of individual subscriptions. They have their own timeline and events occur whether someone is listening or not. An example of this is mouse events. A mouse is generating events regardless of whether there is a subscription listening for them. When a subscription is made, the observer receives current events as they happen. You don't receive and you don't want to receive a recap of everything that the mouse has done since booting the system. When you unsubscribe, it doesn't stop your mouse from generating events either. You're just not receiving them. If you resubscribe, you will again see current events with no recap of what you've missed.
38
38
39
39
## Publish
40
40
41
-
There are ways to transform one into the other. You can transform a cold observable into a hot observable by using `publish()`.
41
+
There are ways to transform cold observables into hot and vice versa. Cold observables become hot with the `publish()` operator.
42
42
43
43
```java
44
44
publicfinalConnectableObservable<T> publish()
@@ -88,7 +88,7 @@ Second: 5
88
88
89
89
### Disconnecting
90
90
91
-
As we saw in `connect`'s signature, this method returns a `Subscription`, just like `Observable.subscribe` does. You can use that reference to terminate the `ConnectableObservable`'s subscription. That will stop events from being pushed to observers if `ConnectableObservable`but it will not unsubscribe them. If you call `connect` again, the `ConnectableObservable` will start a new subscription and the old observers will begin receiving values again.
91
+
As we saw in `connect`'s signature, this method returns a `Subscription`, just like `Observable.subscribe` does. You can use that reference to terminate the `ConnectableObservable`'s subscription. That will stop events from being propagated to observers but it will not unsubscribe them from the `ConnectableObservable`. If you call `connect` again, the `ConnectableObservable` will start a new subscription and the old observers will begin receiving values again.
When you restart by calling `connect` again, a new subscription will be created. If the source observable is cold, that means that the whole sequence is restarted, even though the resulting observable is hot.
122
+
When you restart by calling `connect` again, a new subscription will be created. If the source observable is cold, that means that the whole sequence is restarted.
123
123
124
124
If instead of terminating the connection, you want to unsubscribe from the hot observable, you can use the `Subscription` returned by the `subscribe` method.
125
125
@@ -129,7 +129,7 @@ Subscription s = connectable.connect();
@@ -245,7 +245,7 @@ ConnectableObservable<T> replay(long time, java.util.concurrent.TimeUnit unit)
245
245
246
246
They are different ways of providing one or more of 3 parameters: `bufferSize`, `selector` and `time` (plus `unit` for time).
247
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`.
248
+
*`time`, `unit` determines how old an element can be before being forgotten. Upon subscription, the observable will replay items that are newer than `time`.
249
249
*`selector` will transform the replayed observable, in the same way that `publish(selector)` works.
250
250
251
251
Here's an example with `bufferSize`
@@ -288,7 +288,7 @@ Output
288
288
289
289
## cache
290
290
291
-
The `cache` operator has the same purpose as`replay`, but hides away the `ConnectableObservable` and removes the managing its subscriptions. The internal `ConnectableObservable` is subscribed to when the first observer arrives. Subsequent subscribers have the previous values replayed to them from the cache and don't result in a new subscription tp the source observable.
291
+
The `cache` operator has a similar function to`replay`, but hides away the `ConnectableObservable` and removes the managing of subscriptions. The internal `ConnectableObservable` is subscribed to when the first observer arrives. Subsequent subscribers have the previous values replayed to them from the cache and don't result in a new subscription to the source observable.
292
292
293
293
```java
294
294
publicfinalObservable<T> cache()
@@ -320,7 +320,7 @@ First: 4
320
320
Second: 4
321
321
```
322
322
323
-
In this example, we see that the sequence begins not when the observable was created, but when the first subscriber arrived 500ms after. The second subscribers caught up with earlier values upon subscription and received future values normally.
323
+
In this example, we see that the sequence begins not when the observable was created, but when the first subscriber arrived 500ms later. The second subscribers caught up with earlier values upon subscription and received future values normally.
324
324
325
325
An important thing to note is that the internal `ConnectableObservable` doesn't unsubscribe if all the subscribers go away, like `refCount` would. Once the first subscriber arrives, the source observable will be observed and cached once and for all. This matters because we can't walk away from an infinite observable anymore. Values will continue to cached until the source terminates or we run out of memory. The overload that specifies capacity isn't a solution either, as the capacity is received as a hint for optimisation and won't actually restrict the size of our cache.
326
326
@@ -347,14 +347,14 @@ Unsubscribed
347
347
4
348
348
```
349
349
350
-
In this example, `doOnNext` prints the values as they are produced and cached from the source observable, while `doOnSubscribe` and `doOnUnsubscribe` show the subscribers after the caching. We see that the emission of values begins with the first subscription but disregards the fact that we unsubscribed.
350
+
In this example, `doOnNext` prints the values as they are produced and cached from the source observable, while `doOnSubscribe` and `doOnUnsubscribe` show the subscribers after the caching. We see that the emission of values begins with the first subscription but ignores the fact that we unsubscribed.
351
351
352
352
353
353
354
354
355
355
## Multicast
356
356
357
-
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.
357
+
The `share` method is an alias for `Observable.publish().refCount()`. It allows your subscribers to share a subscription, which is kept for as long as there are subscribers.
0 commit comments