Share
Embed
Transcript
69 slides
-
Marko Stanimirović @MarkoStDev ★ Sr. Frontend Engineer at JobCloud ★
NgRx Team Member ★ Angular Belgrade Organizer ★ Hobby Musician ★ M.Sc. in Software Engineering -
★ Put global state in a single place ★ Use
selectors for derived state ★ Create reusable reducers ★ Treat actions as unique events ★ Group actions by source ★ Don’t dispatch actions conditionally Store Tips -
★ Put global state in a single place ★ Use
selectors for derived state ★ Create reusable reducers ★ Treat actions as unique events ★ Group actions by source ★ Don’t dispatch actions conditionally Store Tips -
SongsService songs$: Observable<Song[]>; activeSong$: Observable<Song | null>; SongsComponent songsWithComposers$ =
NgRx Store composers: { entities: Dictionary<Composer>; }; -
SongsService songs$: Observable<Song[]>; activeSong$: Observable<Song | null>; SongsComponent songsWithComposers$ =
combineLatest([ ]) NgRx Store composers: { entities: Dictionary<Composer>; }; -
SongsService songs$: Observable<Song[]>; activeSong$: Observable<Song | null>; SongsComponent songsWithComposers$ =
combineLatest([ this.songsService.songs$, ]) NgRx Store composers: { entities: Dictionary<Composer>; }; -
SongsService songs$: Observable<Song[]>; activeSong$: Observable<Song | null>; SongsComponent songsWithComposers$ =
combineLatest([ this.songsService.songs$, this.store.select(selectComposers), ]) NgRx Store composers: { entities: Dictionary<Composer>; }; -
SongsService songs$: Observable<Song[]>; activeSong$: Observable<Song | null>; SongsComponent songsWithComposers$ =
combineLatest([ this.songsService.songs$, this.store.select(selectComposers), ]).pipe( map(([songs, composers]) => songs.map((song) => ({ ==.song, composer: composers[song.composerId], })) ) ); NgRx Store composers: { entities: Dictionary<Composer>; }; -
SongsService songs$: Observable<Song[]>; activeSong$: Observable<Song | null>; SongsFacade songsWithComposers$ =
NgRx Store composers: { entities: Dictionary<Composer>; }; SongsComponent songsWithComposers$ = -
SongsService songs$: Observable<Song[]>; activeSong$: Observable<Song | null>; SongsFacade songsWithComposers$ =
combineLatest([ this.songsService.songs$, this.store.select(selectComposers), ]).pipe( map(([songs, composers]) => songs.map((song) => ({ ==.song, composer: composers[song.composerId], })) ) ); NgRx Store composers: { entities: Dictionary<Composer>; }; SongsComponent songsWithComposers$ = -
SongsService songs$: Observable<Song[]>; activeSong$: Observable<Song | null>; SongsFacade songsWithComposers$ =
combineLatest([ this.songsService.songs$, this.store.select(selectComposers), ]).pipe( map(([songs, composers]) => songs.map((song) => ({ ==.song, composer: composers[song.composerId], })) ) ); NgRx Store composers: { entities: Dictionary<Composer>; }; SongsComponent songsWithComposers$ = this.facade.songsWithComposers$; -
SongsService songs$: Observable<Song[]>; activeSong$: Observable<Song | null>; SongsFacade songsWithComposers$ =
combineLatest([ this.songsService.songs$, this.store.select(selectComposers), ]).pipe( map(([songs, composers]) => songs.map((song) => ({ ==.song, composer: composers[song.composerId], })) ) ); NgRx Store composers: { entities: Dictionary<Composer>; }; SongsComponent songsWithComposers$ = this.facade.songsWithComposers$; -
NgRx Store songs: { entities: Dictionary<Song>; activeId: string | null;
}; composers: { entities: Dictionary<Composer>; }; -
NgRx Store songs: { entities: Dictionary<Song>; activeId: string | null;
}; composers: { entities: Dictionary<Composer>; }; songs.selectors.ts const selectSongsWithComposers = -
NgRx Store songs: { entities: Dictionary<Song>; activeId: string | null;
}; composers: { entities: Dictionary<Composer>; }; songs.selectors.ts const selectSongsWithComposers = createSelector( selectAllSongs, ); -
NgRx Store songs: { entities: Dictionary<Song>; activeId: string | null;
}; composers: { entities: Dictionary<Composer>; }; songs.selectors.ts const selectSongsWithComposers = createSelector( selectAllSongs, selectComposers, ); -
NgRx Store songs: { entities: Dictionary<Song>; activeId: string | null;
}; composers: { entities: Dictionary<Composer>; }; songs.selectors.ts const selectSongsWithComposers = createSelector( selectAllSongs, selectComposers, (songs, composers) => songs.map((song) => ({ ==.song, composer: composers[song.composerId], })) ); -
NgRx Store songs: { entities: Dictionary<Song>; activeId: string | null;
}; composers: { entities: Dictionary<Composer>; }; songs.selectors.ts const selectSongsWithComposers = createSelector( selectAllSongs, selectComposers, (songs, composers) => songs.map((song) => ({ ==.song, composer: composers[song.composerId], })) ); SongsComponent songsWithComposers$ = this.store.select(selectSongsWithComposers); -
★ Put global state in a single place ★ Use
selectors for derived state ★ Create reusable reducers ★ Treat actions as unique events ★ Group actions by source ★ Don’t dispatch actions conditionally Store Tips -
export const musiciansReducer = createReducer( on(musiciansPageActions.search, (state, { query })
=> { const filteredMusicians = state.musicians.filter(({ name }) => name.includes(query) ); return { ==.state, query, filteredMusicians, }; }) ); -
export const musiciansReducer = createReducer( on(musiciansPageActions.search, (state, { query })
=> { const filteredMusicians = state.musicians.filter(({ name }) => name.includes(query) ); return { ==.state, query, filteredMusicians, }; }) ); -
export const selectFilteredMusicians = createSelector( selectAllMusicians, selectMusicianQuery, (musicians, query) =>
musicians.filter(({ name }) => name.includes(query)) ); -
export const selectFilteredMusicians = createSelector( selectAllMusicians, selectMusicianQuery, (musicians, query) =>
musicians.filter(({ name }) => name.includes(query)) ); -
export const selectFilteredMusicians = createSelector( selectAllMusicians, selectMusicianQuery, (musicians, query) =>
musicians.filter(({ name }) => name.includes(query)) ); -
export const selectFilteredMusicians = createSelector( selectAllMusicians, selectMusicianQuery, (musicians, query) =>
musicians.filter(({ name }) => name.includes(query)) ); -
export const selectFilteredMusicians = createSelector( selectAllMusicians, selectMusicianQuery, (musicians, query) =>
musicians.filter(({ name }) => name.includes(query)) ); export const musiciansReducer = createReducer( on(musiciansPageActions.search, (state, { query }) => ({ ==.state, query, })) ); -
★ Put global state in a single place ★ Use
selectors for derived state ★ Create reusable reducers ★ Treat actions as unique events ★ Group actions by source ★ Don’t dispatch actions conditionally Store Tips -
export const composersReducer = createReducer( initialState, on( composerExistsGuardActions.canActivate, composersPageActions.opened, songsPageActions.opened,
(state) => ({ ==.state, isLoading: true }) ) ); -
export const composersReducer = createReducer( initialState, on( composerExistsGuardActions.canActivate, composersPageActions.opened, songsPageActions.opened,
(state) => ({ ==.state, isLoading: true }) ) ); -
export const composersReducer = createReducer( initialState, on( composerExistsGuardActions.canActivate, composersPageActions.opened, songsPageActions.opened,
(state, action) => action.type === composerExistsGuardActions.canActivate.type =& state.entities[action.composerId] ? state : { ==.state, isLoading: true } ) ); -
export const composersReducer = createReducer( initialState, on(composersPageActions.opened, songsPageActions.opened, (state) =>
({ ==.state, isLoading: true, })), on(composerExistsGuardActions.canActivate, (state, { composerId }) => state.entities[composerId] ? state : { ==.state, isLoading: true } ) ); -
export const composersReducer = createReducer( initialState, on(composersPageActions.opened, songsPageActions.opened, (state) =>
({ ==.state, isLoading: true, })), on(composerExistsGuardActions.canActivate, (state, { composerId }) => state.entities[composerId] ? state : { ==.state, isLoading: true } ) ); -
★ Put global state in a single place ★ Use
selectors for derived state ★ Create reusable reducers ★ Treat actions as unique events ★ Group actions by source ★ Don’t dispatch actions conditionally Store Tips -
@Component(** **. */) export class SongsComponent implements OnInit { readonly
songs$ = this.store.select(selectSongs); constructor(private readonly store: Store) {} ngOnInit(): void { this.store.dispatch({ type: '[Songs] Load Songs' }); } } -
@Component(** **. */) export class SongsComponent implements OnInit { readonly
songs$ = this.store.select(selectSongs); constructor(private readonly store: Store) {} ngOnInit(): void { this.store.dispatch({ type: '[Songs] Load Songs' }); } } -
@Component(** **. */) export class SongsComponent implements OnInit { readonly
songs$ = this.store.select(selectSongsWithComposers); constructor(private readonly store: Store) {} ngOnInit(): void { this.store.dispatch({ type: '[Songs] Load Songs' }); this.store.dispatch({ type: '[Composers] Load Composers' }); } } -
@Component(** **. */) export class SongsComponent implements OnInit { readonly
songs$ = this.store.select(selectSongsWithComposers); constructor(private readonly store: Store) {} ngOnInit(): void { this.store.dispatch({ type: '[Songs] Load Songs' }); this.store.dispatch({ type: '[Composers] Load Composers' }); } } -
@Component(** **. */) export class SongsComponent implements OnInit { readonly
songs$ = this.store.select(selectSongsWithComposers); constructor(private readonly store: Store) {} ngOnInit(): void { this.store.dispatch({ type: '[Songs Page] Opened' }); } } -
@Component(** **. */) export class SongsComponent implements OnInit { readonly
songs$ = this.store.select(selectSongsWithComposers); constructor(private readonly store: Store) {} ngOnInit(): void { this.store.dispatch({ type: '[Songs Page] Opened' }); } } source event -
[Login Page] Login Form Submitted [Auth API] User Logged in
Successfully [Songs Page] Opened [Songs API] Songs Loaded Successfully [Composers API] Composers Loaded Successfully -
[Login Page] Login Form Submitted [Auth API] User Logged in
Successfully [Songs Page] Opened [Songs API] Songs Loaded Successfully [Composers API] Composers Loaded Successfully [Auth] Login [Auth] Login Success [Songs] Load Songs [Composers] Load Composers [Songs] Load Songs Success [Composers] Load Composers Success -
★ Put global state in a single place ★ Use
selectors for derived state ★ Create reusable reducers ★ Treat actions as unique events ★ Group actions by source ★ Don’t dispatch actions conditionally Store Tips -
songs-page.actions.ts export const opened = createAction('[Songs Page] Opened'); export const
searchSongs = createAction( '[Songs Page] Search Songs Button Clicked', props<{ query: string }>() ); export const addComposer = createAction( '[Songs Page] Add Composer Form Submitted', props<{ composer: Composer }>() ); -
songs-page.actions.ts export const opened = createAction('[Songs Page] Opened'); export const
searchSongs = createAction( '[Songs Page] Search Songs Button Clicked', props<{ query: string }>() ); export const addComposer = createAction( '[Songs Page] Add Composer Form Submitted', props<{ composer: Composer }>() ); songs-api.actions.ts export const songsLoadedSuccess = createAction( '[Songs API] Songs Loaded Successfully', props<{ songs: Song[] }>() ); export const songsLoadedFailure = createAction( '[Songs API] Failed to Load Songs', props<{ errorMsg: string }>() ); -
songs-page.actions.ts export const opened = createAction('[Songs Page] Opened'); export const
searchSongs = createAction( '[Songs Page] Search Songs Button Clicked', props<{ query: string }>() ); export const addComposer = createAction( '[Songs Page] Add Composer Form Submitted', props<{ composer: Composer }>() ); songs-api.actions.ts export const songsLoadedSuccess = createAction( '[Songs API] Songs Loaded Successfully', props<{ songs: Song[] }>() ); export const songsLoadedFailure = createAction( '[Songs API] Failed to Load Songs', props<{ errorMsg: string }>() ); composer-exists-guard.actions.ts export const canActivate = createAction( '[Composer Exists Guard] Can Activate Entered', props<{ composerId: string }>() ); -
★ Put global state in a single place ★ Use
selectors for derived state ★ Create reusable reducers ★ Treat actions as unique events ★ Group actions by source ★ Don’t dispatch actions conditionally Store Tips -
@Component(** **. */) export class SongsComponent implements OnInit { constructor(private
readonly store: Store) {} ngOnInit(): void { this.store.select(selectSongs).pipe( tap((songs) => { if (!songs) { this.store.dispatch(songsActions.loadSongs()); } }), take(1) ).subscribe(); } } -
@Component(** **. */) export class SongsComponent implements OnInit { constructor(private
readonly store: Store) {} ngOnInit(): void { this.store.select(selectSongs).pipe( tap((songs) => { if (!songs) { this.store.dispatch(songsActions.loadSongs()); } }), take(1) ).subscribe(); } } -
readonly loadSongsIfNotLoaded$ = createEffect(() => { return this.actions$.pipe( ofType(songsPageActions.opened), concatLatestFrom(()
=> this.store.select(selectSongs)), filter(([, songs]) => !songs), exhaustMap(() => { return this.songsService.getSongs().pipe( map((songs) => songsApiActions.songsLoadedSuccess({ songs })), catchError((error: { message: string }) => of(songsApiActions.songsLoadedFailure({ error })) ) ); }) ); }); -
readonly loadSongsIfNotLoaded$ = createEffect(() => { return this.actions$.pipe( ofType(songsPageActions.opened), concatLatestFrom(()
=> this.store.select(selectSongs)), filter(([, songs]) => !songs), exhaustMap(() => { return this.songsService.getSongs().pipe( map((songs) => songsApiActions.songsLoadedSuccess({ songs })), catchError((error: { message: string }) => of(songsApiActions.songsLoadedFailure({ error })) ) ); }) ); }); -
readonly loadSongsIfNotLoaded$ = createEffect(() => { return this.actions$.pipe( ofType(songsPageActions.opened), concatLatestFrom(()
=> this.store.select(selectSongs)), filter(([, songs]) => !songs), exhaustMap(() => { return this.songsService.getSongs().pipe( map((songs) => songsApiActions.songsLoadedSuccess({ songs })), catchError((error: { message: string }) => of(songsApiActions.songsLoadedFailure({ error })) ) ); }) ); }); -
readonly loadSongsIfNotLoaded$ = createEffect(() => { return this.actions$.pipe( ofType(songsPageActions.opened), concatLatestFrom(()
=> this.store.select(selectSongs)), filter(([, songs]) => !songs), exhaustMap(() => { return this.songsService.getSongs().pipe( map((songs) => songsApiActions.songsLoadedSuccess({ songs })), catchError((error: { message: string }) => of(songsApiActions.songsLoadedFailure({ error })) ) ); }) ); }); -
@Component(** **. */) export class SongsComponent implements OnInit { constructor(private
readonly store: Store) {} ngOnInit(): void { this.store.dispatch(songsPageActions.opened()); } } -
★ Put global state in a single place ★ Use
selectors for derived state ★ Create reusable reducers ★ Treat actions as unique events ★ Group actions by source ★ Don’t dispatch actions conditionally Store Tips