Skip to content

Commit 42a13cb

Browse files
committed
3.6 Reworked chapter
1 parent 96dc36d commit 42a13cb

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

Part 3 - Taming the sequence/6. Hot and Cold observables.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Hot and Cold observables
22

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.
44

55
## Cold observables
66

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.
88

99
```java
1010
Observable<Long> cold = Observable.interval(200, TimeUnit.MILLISECONDS);
@@ -28,17 +28,17 @@ Second: 2
2828

2929
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.
3030

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`.
3232

3333
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.
3434

3535
## Hot observables
3636

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.
3838

3939
## Publish
4040

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.
4242

4343
```java
4444
public final ConnectableObservable<T> publish()
@@ -88,7 +88,7 @@ Second: 5
8888

8989
### Disconnecting
9090

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.
9292

9393
```java
9494
ConnectableObservable<Long> connectable = Observable.interval(200, TimeUnit.MILLISECONDS).publish();
@@ -119,7 +119,7 @@ Reconnecting
119119
...
120120
```
121121

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, 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.
123123

124124
If instead of terminating the connection, you want to unsubscribe from the hot observable, you can use the `Subscription` returned by the `subscribe` method.
125125

@@ -129,7 +129,7 @@ Subscription s = connectable.connect();
129129

130130
Subscription s1 = connectable.subscribe(i -> System.out.println("First: " + i));
131131
Thread.sleep(500);
132-
Subscription s2 = connectable.subscribe(i -> System.out.println("Seconds: " + i));
132+
Subscription s2 = connectable.subscribe(i -> System.out.println("Second: " + i));
133133

134134
Thread.sleep(500);
135135
System.out.println("Unsubscribing second");
@@ -140,11 +140,11 @@ Output
140140
First: 0
141141
First: 1
142142
First: 2
143-
Seconds: 2
143+
Second: 2
144144
First: 3
145-
Seconds: 3
145+
Second: 3
146146
First: 4
147-
Seconds: 4
147+
Second: 4
148148
Unsubscribing second
149149
First: 5
150150
First: 6
@@ -245,7 +245,7 @@ ConnectableObservable<T> replay(long time, java.util.concurrent.TimeUnit unit)
245245

246246
They are different ways of providing one or more of 3 parameters: `bufferSize`, `selector` and `time` (plus `unit` for time).
247247
* `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`.
249249
* `selector` will transform the replayed observable, in the same way that `publish(selector)` works.
250250

251251
Here's an example with `bufferSize`
@@ -288,7 +288,7 @@ Output
288288

289289
## cache
290290

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.
292292

293293
```java
294294
public final Observable<T> cache()
@@ -320,7 +320,7 @@ First: 4
320320
Second: 4
321321
```
322322

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.
324324

325325
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.
326326

@@ -347,14 +347,14 @@ Unsubscribed
347347
4
348348
```
349349

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.
351351

352352

353353

354354

355355
## Multicast
356356

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.
358358

359359
![](https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/publishRefCount.png)
360360

0 commit comments

Comments
 (0)