Skip to content
This repository was archived by the owner on Nov 18, 2024. It is now read-only.

Commit 026abd9

Browse files
committed
Linked to examples
1 parent 27777d5 commit 026abd9

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

Part 3 - Taming the sequence/1. Side effects.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# PART 3 - Taming the sequence
2-
\
2+
33
So far we've learned how to create observables and how to extract relevant data from observables. In this chapter we will go beyond what is necessary for simple examples and discuss more advanced functionality, as well as some good practices for using Rx in bigger applications.
44

55
# Side effects
@@ -40,7 +40,7 @@ Observable<String> indexed =
4040
});
4141
indexed.subscribe(w -> System.out.println(index.getCount() + ": " + w));
4242
```
43-
Output
43+
[Output](/tests/java/itrx/chapter3/sideeffects/SideEffectExample.java)
4444
```
4545
1: No
4646
2: side
@@ -62,7 +62,7 @@ Observable<String> indexed =
6262
indexed.subscribe(w -> System.out.println("1st observer: " + index.getCount() + ": " + w));
6363
indexed.subscribe(w -> System.out.println("2nd observer: " + index.getCount() + ": " + w));
6464
```
65-
Output
65+
[Output](/tests/java/itrx/chapter3/sideeffects/SideEffectExample.java)
6666
```
6767
1st observer: 1: No
6868
1st observer: 2: side
@@ -102,7 +102,7 @@ Observable<Indexed<String>> indexed =
102102
indexed.subscribe(w -> System.out.println("1st observer: " + w.index + ": " + w.item));
103103
indexed.subscribe(w -> System.out.println("2nd observer: " + w.index + ": " + w.item));
104104
```
105-
Output
105+
[Output](/tests/java/itrx/chapter3/sideeffects/SideEffectExample.java)
106106
```
107107
1st observer: 1: No
108108
1st observer: 2: side
@@ -144,7 +144,7 @@ values
144144
.map(s -> s.toUpperCase())
145145
.subscribe(new PrintSubscriber("Process"));
146146
```
147-
Output
147+
[Output](/tests/java/itrx/chapter3/sideeffects/DoOnExample.java)
148148
```
149149
Log: side
150150
Process: SIDE
@@ -171,7 +171,7 @@ service()
171171
.filter(s -> s.length() > 5)
172172
.subscribe(new PrintSubscriber("Process"));
173173
```
174-
Output
174+
[Output](/tests/java/itrx/chapter3/sideeffects/DoOnExample.java)
175175
```
176176
Log: First
177177
Log: Second
@@ -216,7 +216,7 @@ subject.onNext(2);
216216
subject.onNext(3);
217217
subject.onCompleted();
218218
```
219-
Output
219+
[Output](/tests/java/itrx/chapter3/sideeffects/DoOnExample.java)
220220
```
221221
New subscription
222222
1st: 0
@@ -286,6 +286,8 @@ public Observable<String> getValues() {
286286

287287
Now we have properly protected our `Subject`. This protection is not only against malicious attacks but also against mistakes. We have mentioned before that subjects should be avoided when alternatives exist, and now we've seen examples of why. Subjects introduce state to our observables. Calls to `onNext`, `onCompleted` and `onError` alter the sequence that consumers will see. Observables that are constructed with any of the factory methods or operators exposed on `Observable` are immutable, provided that we don't introduce side-effects ourselves, as we saw in [Issues with side effects](/Part%203%20-%20Taming%20the%20sequence/1.%20Side%20effects.md#issues-with-side-effects).
288288

289+
You can find the full code of the examples discussed [here](/tests/java/itrx/chapter3/sideeffects/AsObservableExample.java)
290+
289291
## Mutable elements cannot be protected
290292

291293
As one might expect, an Rx pipeline forwards references to objects and doesn't create copies (unless we do so ourselves in the functions we supply). Modifications to the objects will be visible to every position in the pipeline that uses them. Consider the following mutable class:
@@ -312,7 +314,7 @@ Observable<Data> data = Observable.just(
312314
data.subscribe(d -> d.name = "Garbage");
313315
data.subscribe(d -> System.out.println(d.id + ": " + d.name));
314316
```
315-
Output
317+
[Output](/tests/java/itrx/chapter3/sideeffects/MutablePipelineExample.java)
316318
```
317319
1: Garbage
318320
2: Garbage

0 commit comments

Comments
 (0)