combining-streams.intro.md 3.6 KB
Newer Older
micha's avatar
init  
micha committed
1
2
3
# Combining Stream and Behavior

![Reactive Architecture and UX Patterns - Combining Stream and Behavior](./assets/images/Reactive-architecture-and-ux-patterns_angular_combining-streams-and-behavior_michael-hladky.png)
4
5
6
7
8
9
10
11
12
13
14

This set of lessons are all about combination operators. 
As this course focuses on real-life use cases,  
we will use these operators in the context of state derivation, data fetching and update behavior.

We will start with some snippets of code people normally use.  
With our examples we will run into some problems with that simple approaches. 

As we go, we learn how to solve them, 
introduce more robust patterns and finally know all the does and donts of deriving state with combination operators. 

micha's avatar
init  
micha committed
15
16
![Reactive Architecture and UX Patterns - Combination Operators](./assets/images/Reactive-architecture-and-ux-patterns_angular_combination-operators-dark_michael-hladky.png)

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
## Combination Operators - Operator Matrix

To get a better understanding of the operators in that group we create a matrix.
The operator matrix is something I came up with to show the relations of the different 
operators to each other and the similarities in behavior. 

| creation       | creation       |  operator         | operator    |
|----------------|----------------|-------------------|-------------|
| combineLatest  | forkJoin       | combineLatestWith | combineAll  |
| zip            |                | zipWith           | zipAll      |
|                |                | withLatestFrom    |             |

## Combination Operators - Algebraic approach

Another learning too I use, to teach people a more efficient learning of operators is the "Algebraic approach".
I named it that way because it borrows concepts and thinking from one of the broad parts of mathematics, [Algebra](https://en.wikipedia.org/wiki/Algebra).

> Literally translated Algebra means "reunion of broken parts".

Understanding the different operators in terms of their "broken parts" is unequally more efficient and intuitive than learning them one by one based on their name.

The list of broken parts from the above set looks like this:
`combineLatest`, `forkJoin`, `zip`, `withLatestFrom`, `With`, `All` 

If we understand every of those "broken parts" we are intuitively able to understand their "reunion", meaning the operators itself.

## Combination Operators - Exercise walk through

Within this set of lessons we will walk through the following exercises:
micha's avatar
init  
micha committed
46

micha's avatar
micha committed
47
- [ ] We start we a simple setup where we derive data in our component directly over HTTP requests by using `forkJoin`
48
  - with this architecture we realize, we quickly run into the problem of over-fetching
micha's avatar
micha committed
49
- [ ] To solve it we refactor the give HTTP service to get more control over when we fetch the data
50
  - this reviles one of the special behaviours of `frokJoin` and we need to rethink it usage
micha's avatar
micha committed
51
- [ ] We learn the difference of `forkJoin` and `combineLatest` 
52
  - this knowledge helps us to refactor the service and component.
micha's avatar
micha committed
53
- [ ] As we go we start to introduce more features into our UI
54
   - again we run into a problem, this time over-rendering.
micha's avatar
micha committed
55
- [ ] To understand the problem we learn about the terms `Normalized` and `Denormalized` data 
56
  -  by using `zip` for our calculation we are able to solve the problem of over-rendering
micha's avatar
micha committed
57
- [ ] As it was quite technical so far we learn about `withLatestFrom` with a more playful example 
58
  - by doing so we understand the concept of `promary` and `secondary` streams
micha's avatar
micha committed
59
- [ ] With a fresh and open mine we think about those concepts in combination with a UX Pattern called `opt-in updates`
60
  - to give a better experience to our users we implement this pattern in our example
micha's avatar
init  
micha committed
61