Skip to content

Commit 19a659f

Browse files
brechtbilliet0x-r4bbit
authored andcommitted
Update 2016-06-16-cold-vs-hot-observables.md
1 parent e9b7b34 commit 19a659f

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

Diff for: _posts/2016-06-16-cold-vs-hot-observables.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ this.contacts = http.get('contacts.json')
387387

388388
The `share()` will publish the first value of the stream on the first subscription. The first async pipe will trigger that subscription and get that initial value. The second async pipe however will subscribe after that value has already been emitted and therefore miss that value.
389389

390-
The solution for this problem is the `.shareReplay(1)` operator, which will keep track of the previous value of the stream. That way all the async pipes will get the last value. Just like the `share()` operator is a shortcut for `publish().refCount()`, the `shareReplay(1)` operator is a shortcut for `publishReplay(1).refCount()`. We can see the difference between `share()` and `shareReplay(1)` in [the following plunk](http://plnkr.co/edit/q9xfvjzHauRBsA8o4nob?p=preview).
390+
The solution for this problem is the `.shareReplay(1)` operator, which will keep track of the previous value of the stream. That way all the async pipes will get the last value. Just like the `share()` operator is a shortcut for `publish().refCount()`, the `shareReplay(1)` operator is a shortcut for `publishReplay(1).refCount()`. We can see the difference between `share()` and `shareReplay(1)` in [the following plunk](http://plnkr.co/edit/PATRQvsWYWmwvcPiaOZJ?p=preview).
391391
We should always pass `1` as the parameter value to the shareReplay function. Otherwise RxJS will keep track of all the values of that observable, when we only need the last one.
392392

393393
It's important to note that this problem will only occur when using streams with initial values.

0 commit comments

Comments
 (0)