Slide 1

Slide 1 text

Hallo

Slide 2

Slide 2 text

UI Model {…} APPLICATION STATE MANAGEMENT ? sync

Slide 3

Slide 3 text

BIDIRECTIONAL (two way) data binding

Slide 4

Slide 4 text

UNIDIRECTIONAL data flow

Slide 5

Slide 5 text

Redux

Slide 6

Slide 6 text

single IMMUTABLE the STATE tree &

Slide 7

Slide 7 text

Ciro Nunes FRONTEND ENGINEER @cironunesdev

Slide 8

Slide 8 text

Belo Horizonte

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

AGENDA

Slide 11

Slide 11 text

1. Problems with mutable objects AGENDA

Slide 12

Slide 12 text

1. Problems with mutable objects 2. Using immutable objects AGENDA

Slide 13

Slide 13 text

1. Problems with mutable objects 2. Using immutable objects 3. The single immutable state tree AGENDA

Slide 14

Slide 14 text

MUTABLE OBJECTS make TRACKING hard CHANGES

Slide 15

Slide 15 text

@Component({+ ++selector:+'my3person',+ ++template:+`+ +++{{+person.name+}}+lives+at+{{+address.street+}},+{{+address.city+}}+ ++`+ })+ export+class+MyPerson+{+ ++@Input()+person:+{+name:+string+};+ ++@Input()+address:+{+city:+string,+street:+string+}+ } my-person.component.ts

Slide 16

Slide 16 text

@Component({+ ++template:+`+ +++{{+person.name+}}+lives+at+{{+address.street+}},+{{+address.city+}}+{{+zipCode+}}+ ++`+ })+ export+class+MyPerson+{+ ++@Input()+person:+{+name:+string+};+ ++@Input()+address:+{+city:+string,+street:+string+};+ ++zipCode:+number;+ ++ngOnChanges(inputChanges)+{+ ++++if+(inputChanges.address)+{+ ++++++this.zipCode+=+this.zipCodes(this.address);+ ++++}+ ++}+ } my-greeting.component.ts

Slide 17

Slide 17 text

PROBLEMS WITH MUTABLE OBJECTS

Slide 18

Slide 18 text

1. Other components can change it not propagating the change PROBLEMS WITH MUTABLE OBJECTS

Slide 19

Slide 19 text

MUTABLE OBJECTS can LEAD issues PERFORMANCE to

Slide 20

Slide 20 text

@Component({+ ++selector:+'my3person',+ ++template:+`+ +++{{+person.name+}}+lives+at+{{+address.street+}},+{{+address.city+}}+{{+zipCode+}}+ ++`+ })+ export+class+MyPerson+{+ ++@Input()+person:+{+name:+string+};+ ++@Input()+address:+{+city:+string,+street:+string+};+ ++zipCode:+number;+ ++ngOnChanges(inputChanges)+{+ ++++if+(inputChanges.address)+{+ ++++++this.zipCode+=+this.zipCodes(this.address);+ ++++}+ ++}+ } my-greeting.component.ts

Slide 21

Slide 21 text

1. Other components can change it not propagating the change PROBLEMS WITH MUTABLE OBJECTS

Slide 22

Slide 22 text

1. Other components can change it not propagating the change PROBLEMS WITH MUTABLE OBJECTS 2. The framework needs to be conservative

Slide 23

Slide 23 text

IMMUTABLE OBJECTS win THE for

Slide 24

Slide 24 text

IMMUTABLE OBJECTS references NEW creates

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

let+person+=+Immutable.Map({+ ++name:+'Ciro',+ ++address:+{+ ++++street:+'Gardenia',+ ++++city:+'Belo+Horizonte'+ ++}+ });+ function+changeAddressStreet(person,+newStreet)+{+ ++return+person.setIn(['address'],+{+ ++++street:+newStreet,+ ++++city:+person.toJS().address.city+ ++});+ }+ let+newAddress+=+changeAddressStreet(person,+'New+Address'); pure-function—sample.ts

Slide 27

Slide 27 text

let+person+=+Immutable.Map({+ ++name:+'Ciro',+ ++address:+{+ ++++street:+'Gardenia',+ ++++city:+'Belo+Horizonte'+ ++}+ });+ function+changeAddressStreet(person,+newStreet)+{+ ++return+person.setIn(['address'],+{+ ++++street:+newStreet,+ ++++city:+person.toJS().address.city+ ++});+ }+ let+newAddress+=+changeAddressStreet(person,+'New+Address’); pure-function—sample.ts

Slide 28

Slide 28 text

let+person+=+Immutable.Map({+ ++name:+'Ciro',+ ++address:+{+ ++++street:+'Gardenia',+ ++++city:+'Belo+Horizonte'+ ++}+ });+ function+changeAddressStreet(person,+newStreet)+{+ ++return+person.setIn(['address'],+{+ ++++street:+newStreet,+ ++++city:+person.toJS().address.city+ ++});+ }+ let+newAddress+=+changeAddressStreet(person,+'New+Address’); pure-function—sample.ts PURE FUNCTION

Slide 29

Slide 29 text

ADVANTAGES OF IMMUTABLE OBJECTS

Slide 30

Slide 30 text

1. Tracking changes made easy ADVANTAGES OF IMMUTABLE OBJECTS

Slide 31

Slide 31 text

App Todos MyGreeting Todo Todo • person • address • zipcode

Slide 32

Slide 32 text

my-person.component.ts @Component({+ ++selector:+'my3person',+ ++template:+`+ +++{{+person.name+}}+lives+at+{{+address.street+}},+{{+address.city+}}+{{+zipcode+}}+ ++`,+ ++changeDetection:+ChangeDetectionStrategy.OnPush+ })+ export+class+MyPerson+{+ ++@Input()+person:+{+name:+string+};+ ++@Input()+address:+{+city:+string,+street:+string+}+ ++zipCode:+number;+ ++ngOnChanges(inputChanges)+{+ ++++if+(inputChanges.address)+{+ ++++++this.zipCode+=+this.zipCodes(this.address);+ ++++}+ ++}+ }

Slide 33

Slide 33 text

1. Tracking changes made easy ADVANTAGES OF IMMUTABLE OBJECTS

Slide 34

Slide 34 text

1. Tracking changes made easy ADVANTAGES OF IMMUTABLE OBJECTS 2. Able to skip checks, results in better performance

Slide 35

Slide 35 text

single IMMUTABLE the STATE tree &

Slide 36

Slide 36 text

Redux

Slide 37

Slide 37 text

THE SINGLE IMMUTABLE STATE TREE

Slide 38

Slide 38 text

1. Unidirectional data flow THE SINGLE IMMUTABLE STATE TREE

Slide 39

Slide 39 text

1. Unidirectional data flow THE SINGLE IMMUTABLE STATE TREE 2. Changes are made using pure functions (reducers)

Slide 40

Slide 40 text

Actions Reducers Store View REDUX UNIDIRECTIONAL
 DATA FLOW subscribe dispatch

Slide 41

Slide 41 text

Actions Reducers Store View REDUX UNIDIRECTIONAL
 DATA FLOW JS objects subscribe dispatch

Slide 42

Slide 42 text

Actions Reducers Store View REDUX UNIDIRECTIONAL
 DATA FLOW JS objects subscribe dispatch

Slide 43

Slide 43 text

Actions Reducers Store View REDUX UNIDIRECTIONAL
 DATA FLOW JS objects Pure functions subscribe dispatch

Slide 44

Slide 44 text

1. Always produce the same output given the same input PURE FUNCTIONS 2. Don’t produce side-effects (e.g: mutate state)

Slide 45

Slide 45 text

const+initialState+=+{+ ++todos:+[],+ ++currentFilter:+'SHOW_ALL'+ };+ function+rootReducer(state+=+initialState,+action)+{+ ++switch+(action.type)+{+ ++++case+'ADD_TODO':++ ++++++++return+{+ ++++++++++todos:+[...todos,+newTodo],+ ++++++++++currentFilter:+state.currentFilter+ ++++++++}+ ++++++++break;+ ++++default:++ ++++++return+state;+ ++}+ }; root-reducer.ts

Slide 46

Slide 46 text

const+initialState+=+{+ ++todos:+[],+ ++currentFilter:+'SHOW_ALL'+ };+ function+rootReducer(state+=+initialState,+action)+{+ ++switch+(action.type)+{+ ++++case+'ADD_TODO':++ ++++++++return+{+ ++++++++++todos:+[...todos,+newTodo],+ ++++++++++currentFilter:+state.currentFilter+ ++++++++};+ ++++++++break;+ ++++default:++ ++++++return+state;+ ++}+ }; root-reducer.ts

Slide 47

Slide 47 text

Actions Reducers Store View REDUX UNIDIRECTIONAL
 DATA FLOW JS objects Pure functions subscribe dispatch

Slide 48

Slide 48 text

Actions Reducers Store View REDUX UNIDIRECTIONAL
 DATA FLOW JS objects Pure functions Application state subscribe dispatch

Slide 49

Slide 49 text

import+{createStore}+from+'redux';+ import+{rootReducer}+from+'./root3reducer';+ const+appStore+=+createStore(rootReducer);+ appStore.subscribe(callback);+ appStore.dispatch(action);+ appStore.getState(); app-store.ts

Slide 50

Slide 50 text

import+{createStore}+from+'redux';+ import+{rootReducer}+from+'./root3reducer';+ import+{Actions}+from+'./actions';+ const+appStore+=+createStore(rootReducer);+ bootstrap(App,+[+ ++provide('AppStore',+{useValue:+appStore}),+ ++Actions+ ]); app-store.ts

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

OTHER OPTIONS TO REDUX

Slide 53

Slide 53 text

1. ngrx/@store OTHER OPTIONS TO REDUX

Slide 54

Slide 54 text

1. ngrx/@store OTHER OPTIONS TO REDUX 2. Victor Savkin’s implementation using observables

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

IN SUMMARY

Slide 58

Slide 58 text

Mutable objects are bad for modelling application state IN SUMMARY

Slide 59

Slide 59 text

Mutable objects are bad for modelling application state IN SUMMARY Immutable objects makes it easy to track changes

Slide 60

Slide 60 text

Mutable objects are bad for modelling application state IN SUMMARY Immutable objects makes it easy to track changes Angular 2 CD mechanism makes it even faster

Slide 61

Slide 61 text

Mutable objects are bad for modelling application state IN SUMMARY Immutable objects makes it easy to track changes Angular 2 CD mechanism makes it even faster Redux is awesome, but there are other great options

Slide 62

Slide 62 text

THANKS TO @gsans @PascalPrecht

Slide 63

Slide 63 text

Dankjewel