Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Angular@Scale with ngrx-ducks

Angular@Scale with ngrx-ducks

This presentation is a shortened version of [1] since this talk contains an introduction to my library ngrx-ducks [2].

[1] https://speakerdeck.com/gregonnet/angular-at-scale-redux-and-at-ngrx
[2] https://github.com/co-it/co-it

Gregor Woiwode

September 11, 2018
Tweet

More Decks by Gregor Woiwode

Other Decks in Programming

Transcript

  1. Topics For Today AGENDA - Common Pitfalls occurring in larger

    Teams - The Why, How & What of State Management - Redux Architecture - ngrx features managing State
  2. Increasing Code Complexity Common Pitfalls - Cyclometic Complexity of 15

    - Nested Code Blocks - Domain & Infrastructure mixed - No Tests
  3. Component Monolith PITFALLS - No clear API - Deeply nested

    Components - Hard for others to understand - Hard to maintain
  4. - Reduce Component Nesting - Think in APIs - Put

    a smile on your colleagues’ face Component Monolith PITFALLS
  5. Learnings Common Pitfalls - Applying valuable patterns is hard -

    It is nearly impossible to know the whole Project you are working on - It is often ignored that requirements change over time
  6. Problems of Front-End Development Common Pitfalls Share State across components

    Internal state management becomes difficult Distant leaf components need access to state But intermediatary components don‘t need them
  7. Why do we need to care about State Management Patterns?

    WHY, HOW & WHAT OF STATE MANAGEMENT - Communicate effectively through deeply nested component tree - Publish consistent amount of features over time - Built more trust in code base - Deliver a product having a great user experience
  8. REDUX ARCHITECTURE Unidirectional Data Flow simplified ACTION STATE COMPONENT STORE

    Dispatch Send To Mutate Render Inspired by @ToddMotto REDUCERS
  9. 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
  10. 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
  11. 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
  12. REDUX ARCHITECTURE Side Effects click input click Action Action Action

    Component Async Effect Success Action Error Action change Start Action Store change event triggered
  13. REDUX ARCHITECTURE @Effect @Injectable() export class NotesEffects { @Effect() loadAll

    = this.actions$.pipe( ofType(NotesActionTypes.LoadAllNotes), switchMap(() => this.notes.getAll()), map(notes => new LoadNotesSuccessful(notes)) ); constructor(private actions$: Actions, private notes: NotesService) {} }
  14. 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: CraftWarshipAct switch (action.type) { case CraftWarshipActionTypes.ChooseWarshipPlanSuccess: case CraftWarshipActionTypes.RecoverWarshipPlanSuccess: return { ...state, current: action.payload }; default: return state; } }
  15. QUERY API FOR VIEWS Selectors 1. Provide a query API

    for views 2. Simplify Reducers 3. Memoization
  16. A QUERY API FOR REDUX Selectors Instrumentint Memoization Pattern 1

    selector calculate 3 selector recalculate Store change event triggered 2 selector cache
  17. A QUERY API FOR REDUX Use Selectors import {createFeatureSelector, createSelector

    } from '@ngrx/store'; export const feature = createFeatureSelector<HarbourSlice>('harbour‘); export const selector = createSelector(feature, slice => slice.<property>); reuse
  18. 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) => /* */ );
  19. 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
  20. PROPERTY PATTERN Review Less Files to maintain Scales good for

    many entities Highly Configurable Handles Loading State automatically High Learning Curve Lot‘s of things happen behind the scenes
  21. NGRX Criticism Touch lots of files Boilerplate vs. Explicit Setup

    Distributed Setup (Actions, Reducers, Selectors, Effects) It is easy to forget parts of the setup (ActionType) High Learning Curve Lot‘s of things happen behind the scenes
  22. NGRX Redux the bad parts REDUCER ACTION TYPES DISPATCHER ACTIONS

    EFFECT ACTION REDUCER MAP ACTIONS$ ACTION CREATORS
  23. NGRX What if… 1. … we could automate the creation

    of an action 2. … an Action knows about it‘s case reducer 3. … an Acion could dispatch itself 4. … we have one object strucure providing us every thing we need?
  24. PROPERTY PATTERN Review Consume Redux as a Service TypeScript Compiler

    saves us for mistakes Serves Open Closed Principle Supports AoT Supports AoT … ` Setting up Dynamic Services is hard Could improve handling for Effects