Slide 1

Slide 1 text

co-IT.eu GmbH We Empower Outstanding Business Solutions Angular at Scale – @ngrx @GregOnNet

Slide 2

Slide 2 text

co-IT.eu GmbH We Empower Outstanding Business Solutions “ Please feel free to ask anything at any time. @GregOnNet

Slide 3

Slide 3 text

AGENDA What are we going to discuss? 1. The WHY, HOW & WHAT of State Management 2. Getting to know the Redux Architecture 3. @ngrx features that help us to survive in enterprise projects

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

WHY, HOW & WHAT OF STATE MANAGEMENT What do we have to deal with? 1. Bad code increases 2. Less features are published in certain period of time 3. Confusion due different approaches compose components

Slide 6

Slide 6 text

WHY, HOW & WHAT OF STATE MANAGEMENT What is done here? 1. Cyclometic Complexity of 8 2. Nested code blocks 3. Mix Domain & Infrastructure 4. No Tests

Slide 7

Slide 7 text

WHY, HOW & WHAT OF STATE MANAGEMENT Component Composition Simple Dialog material.angular.io

Slide 8

Slide 8 text

WHY, HOW & WHAT OF STATE MANAGEMENT The Component Monolith ModalWrapper Modal Stepper Question ButtonGroup Just 5 Files away to know how the component works…

Slide 9

Slide 9 text

WHY, HOW & WHAT OF STATE MANAGEMENT Monoliths Increase Research Time 1. No clear API 2. Deeply nested Components 3. Hard for others to understand

Slide 10

Slide 10 text

WHY, HOW & WHAT OF STATE MANAGEMENT How do we get there? 1. Not enough experience applying software patterns 2. No clue over the whole project 3. Requirements change over time

Slide 11

Slide 11 text

WHY, HOW & WHAT OF STATE MANAGEMENT Think in APIs 1. Provide easy to use APIs 2. Reduce Component Nesting 3. Put a smile on your colleague‘s face

Slide 12

Slide 12 text

WHY, HOW & WHAT OF STATE MANAGEMENT Think in Smart & Dumb Components Provide composition boundaries between UI concerns. “

Slide 13

Slide 13 text

WHY, HOW & WHAT OF STATE MANAGEMENT Dumb Components @Input @Output Container 1. Present Data 2. Handle UI Logic 3. Being reusable Building Blocks 4. No dependency to a data service

Slide 14

Slide 14 text

WHY, HOW & WHAT OF STATE MANAGEMENT Smart Components 1. Orchestrate Components 2. Provide Data from APIs 3. Made for a certain use case Receive Call Container HTTP/WS Redux

Slide 15

Slide 15 text

WHY, HOW & WHAT OF STATE MANAGEMENT Why do we need to care about State Management Patterns? 1. Communicate effectively through deeply nested component tree 2. Publish consistent amount of features over time 3. Built more trust in code base 4. Deliver a product having a great user experience

Slide 16

Slide 16 text

REDUX ARCHITECTURE Centralize State Management Store Component SELECT INTERACT

Slide 17

Slide 17 text

REDUX ARCHITECTURE Five Types Of Application State Data Communication Control Session Location 5 Types of Application State jamesknelson.com/5-types-react-application-state

Slide 18

Slide 18 text

REDUX ARCHITECTURE Flow Type Payload Action State Store Dispatch action to Store Reducer Current state & Action Properties Component in App Some component action (e.g. button clicked) Initial state received Update on Store change event Store change event triggered

Slide 19

Slide 19 text

REDUX ARCHITECTURE How do we communicate between a Component and a Store? { type: 'Unique Type‘, payload: { use: 'data‘, we: 'need‘, to: 'mutate‘, application: 'state‘ } } … an Action

Slide 20

Slide 20 text

REDUX ARCHITECTURE How do we process state mutations? export function reducer(currentState, action) { switch(action.type) { case 'Unique Type‘: return { ...currentState, application: action.payload.application }; } } … with a Reducer

Slide 21

Slide 21 text

REDUX ARCHITECTURE How do we process state mutations? export function reducer(currentState, action) { switch(action.payload) { case 'Unique Type‘: return { ...currentState, application: action.payload.application }; } } Create a new Slice

Slide 22

Slide 22 text

REDUX ARCHITECTURE Unidirectional Data Flow simplified ACTION STATE COMPONENT STORE Dispatch Send To Mutate Render Inspired by @ToddMotto REDUCERS

Slide 23

Slide 23 text

REDUX ARCHITECTURE Unidirectional Data Flow simplified ACTION REDUCERS STATE COMPONENT Inspired by @ToddMotto

Slide 24

Slide 24 text

REDUX ARCHITECTURE Side Effects click input click Action Action Action Component Async Effect Success Action Error Action change Start Action Store change event triggered

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Run Through Drift Along With Redux

Slide 27

Slide 27 text

NGRX What Are We Going To See? 1. How the Store is setup 2. How a Component dispatches an Action 3. How an Action is built 4. How a Reducer processes an Action export function reducer(state = initialState, action: CraftWarshipActions): Slice { switch (action.type) { case CraftWarshipActionTypes.ChooseWarshipPlanSuccess: case CraftWarshipActionTypes.RecoverWarshipPlanSuccess: return { ...state, current: action.payload }; default: return state; } }

Slide 28

Slide 28 text

Selectors A QUERY API FOR VIEWS

Slide 29

Slide 29 text

QUERY API FOR VIEWS Selectors 1. Provide a query API for views 2. Simplify Reducers 3. Memoization

Slide 30

Slide 30 text

A QUERY API FOR REDUX Selectors Instrumentint Memoization Pattern 1 selector calculate 3 selector recalculate Store change event triggered 2 selector cache

Slide 31

Slide 31 text

A QUERY API FOR REDUX Use Selectors import {createFeatureSelector, createSelector } from '@ngrx/store'; export const feature = createFeatureSelector('harbour‘); export const selector = createSelector(feature, slice => slice.); reuse

Slide 32

Slide 32 text

A QUERY API FOR REDUX Combine Selectors export const customers = createSelector(customerSlice, slice => /* */); export const orders = createSelector(ordersSlice, slice => /* */); export const customerOrders = createSelector( customers, orders, (customers, orders) => /* */ );

Slide 33

Slide 33 text

Time Travel Debugging Analyse Life-Lifecycle

Slide 34

Slide 34 text

NGRX Time Travel Debugging github.com/GregOnNet/ngx-battleships.git

Slide 35

Slide 35 text

Lazy Loading Loading Reducers as needed

Slide 36

Slide 36 text

NGRX Load Features Lazily https://github.com/ng-practice/ngrx-aware

Slide 37

Slide 37 text

Redux is a Team Sport

Slide 38

Slide 38 text

CLOSING THOUGHTS Redux Is More Than Just An Architecture. 1. It is a comitment made by the whole developer team. 2. It guides you through the process of modeling application state. 3. It opens a new world analyzing your app. TEAM REQUIREMENTS TOOLING

Slide 39

Slide 39 text

BUSINESS SOLUTIONS Need help? Twitter Workshops Projects workshops.co-IT.eu contact.co-IT.eu @GregOnNet

Slide 40

Slide 40 text

co-IT.eu GmbH We Empower Outstanding Business Solutions Thank you! @GregOnNet