Share
Embed
Transcript
117 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 ★ Use view model selectors ★ Create reusable reducers ★ Treat actions as unique events ★ Group actions by source Store Tips -
★ Put global state in a single place ★ Use
selectors for derived state ★ Use view model selectors ★ Create reusable reducers ★ Treat actions as unique events ★ Group actions by source 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 ★ Use view model selectors ★ Create reusable reducers ★ Treat actions as unique events ★ Group actions by source 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 ★ Use view model selectors ★ Create reusable reducers ★ Treat actions as unique events ★ Group actions by source Store Tips -
musicians.component.ts @Component({ selector: 'musicians', template: ` <musician-search [query]="query$ | async">./musician-search>
<musician-list [musicians]="musicians$ | async" [activeMusician]="activeMusician$ | async" >./musician-list> <musician-details [musician]="activeMusician$ | async" >./musician-details> `, }) export class MusiciansComponent { readonly musicians$ = this.store.select(selectFilteredMusicians); readonly query$ = this.store.select(selectMusiciansQuery); readonly activeMusician$ = this.store.select(selectActiveMusician); constructor(private readonly store: Store) {} } -
musicians.component.ts @Component({ selector: 'musicians', template: ` <musician-search [query]="query$ | async">./musician-search>
<musician-list [musicians]="musicians$ | async" [activeMusician]="activeMusician$ | async" >./musician-list> <musician-details [musician]="activeMusician$ | async" >./musician-details> `, }) export class MusiciansComponent { readonly musicians$ = this.store.select(selectFilteredMusicians); readonly query$ = this.store.select(selectMusiciansQuery); readonly activeMusician$ = this.store.select(selectActiveMusician); constructor(private readonly store: Store) {} } -
musicians.component.ts @Component({ selector: 'musicians', template: ` <musician-search [query]="query$ | async">./musician-search>
<musician-list [musicians]="musicians$ | async" [activeMusician]="activeMusician$ | async" >./musician-list> <musician-details [musician]="activeMusician$ | async" >./musician-details> `, }) export class MusiciansComponent { readonly musicians$ = this.store.select(selectFilteredMusicians); readonly query$ = this.store.select(selectMusiciansQuery); readonly activeMusician$ = this.store.select(selectActiveMusician); constructor(private readonly store: Store) {} } -
musicians.component.ts @Component({ selector: 'musicians', template: ` <musician-search [query]="query$ | async">./musician-search>
<musician-list [musicians]="musicians$ | async" [activeMusician]="activeMusician$ | async" >./musician-list> <musician-details [musician]="activeMusician$ | async" >./musician-details> `, }) export class MusiciansComponent { readonly musicians$ = this.store.select(selectFilteredMusicians); readonly query$ = this.store.select(selectMusiciansQuery); readonly activeMusician$ = this.store.select(selectActiveMusician); constructor(private readonly store: Store) {} } -
musicians.component.ts @Component({ selector: 'musicians', template: ` <musician-search [query]="query$ | async">./musician-search>
<musician-list [musicians]="musicians$ | async" [activeMusician]="activeMusician$ | async" >./musician-list> <musician-details [musician]="activeMusician$ | async" >./musician-details> `, }) export class MusiciansComponent { readonly musicians$ = this.store.select(selectFilteredMusicians); readonly query$ = this.store.select(selectMusiciansQuery); readonly activeMusician$ = this.store.select(selectActiveMusician); constructor(private readonly store: Store) {} } musicians.selectors.ts export const selectMusiciansPageViewModel = createSelector( ); -
musicians.component.ts @Component({ selector: 'musicians', template: ` <musician-search [query]="query$ | async">./musician-search>
<musician-list [musicians]="musicians$ | async" [activeMusician]="activeMusician$ | async" >./musician-list> <musician-details [musician]="activeMusician$ | async" >./musician-details> `, }) export class MusiciansComponent { readonly musicians$ = this.store.select(selectFilteredMusicians); readonly query$ = this.store.select(selectMusiciansQuery); readonly activeMusician$ = this.store.select(selectActiveMusician); constructor(private readonly store: Store) {} } musicians.selectors.ts export const selectMusiciansPageViewModel = createSelector( selectFilteredMusicians, ); -
musicians.component.ts @Component({ selector: 'musicians', template: ` <musician-search [query]="query$ | async">./musician-search>
<musician-list [musicians]="musicians$ | async" [activeMusician]="activeMusician$ | async" >./musician-list> <musician-details [musician]="activeMusician$ | async" >./musician-details> `, }) export class MusiciansComponent { readonly musicians$ = this.store.select(selectFilteredMusicians); readonly query$ = this.store.select(selectMusiciansQuery); readonly activeMusician$ = this.store.select(selectActiveMusician); constructor(private readonly store: Store) {} } musicians.selectors.ts export const selectMusiciansPageViewModel = createSelector( selectFilteredMusicians, selectMusiciansQuery, ); -
musicians.component.ts @Component({ selector: 'musicians', template: ` <musician-search [query]="query$ | async">./musician-search>
<musician-list [musicians]="musicians$ | async" [activeMusician]="activeMusician$ | async" >./musician-list> <musician-details [musician]="activeMusician$ | async" >./musician-details> `, }) export class MusiciansComponent { readonly musicians$ = this.store.select(selectFilteredMusicians); readonly query$ = this.store.select(selectMusiciansQuery); readonly activeMusician$ = this.store.select(selectActiveMusician); constructor(private readonly store: Store) {} } musicians.selectors.ts export const selectMusiciansPageViewModel = createSelector( selectFilteredMusicians, selectMusiciansQuery, selectActiveMusician, ); -
musicians.component.ts @Component({ selector: 'musicians', template: ` <musician-search [query]="query$ | async">./musician-search>
<musician-list [musicians]="musicians$ | async" [activeMusician]="activeMusician$ | async" >./musician-list> <musician-details [musician]="activeMusician$ | async" >./musician-details> `, }) export class MusiciansComponent { readonly musicians$ = this.store.select(selectFilteredMusicians); readonly query$ = this.store.select(selectMusiciansQuery); readonly activeMusician$ = this.store.select(selectActiveMusician); constructor(private readonly store: Store) {} } musicians.selectors.ts export const selectMusiciansPageViewModel = createSelector( selectFilteredMusicians, selectMusiciansQuery, selectActiveMusician, (musicians, query, activeMusician) .> ({ musicians, query, activeMusician, }) ); -
musicians.component.ts @Component({ selector: 'musicians', template: ` <ng-container *ngIf="vm$ | async
as vm"> <musician-search [query]="vm.query">./musician-search> <musician-list [musicians]="vm.musicians" [activeMusician]="vm.activeMusician" >./musician-list> <musician-details [musician]="vm.activeMusician" >./musician-details> ./ng-container> `, }) export class MusiciansComponent { readonly vm$ = this.store.select(selectMusiciansPageViewModel); constructor(private readonly store: Store) {} } musicians.selectors.ts export const selectMusiciansPageViewModel = createSelector( selectFilteredMusicians, selectMusiciansQuery, selectActiveMusician, (musicians, query, activeMusician) .> ({ musicians, query, activeMusician, }) ); -
musicians.component.ts @Component({ selector: 'musicians', template: ` <ng-container *ngIf="vm$ | async
as vm"> <musician-search [query]="vm.query">./musician-search> <musician-list [musicians]="vm.musicians" [activeMusician]="vm.activeMusician" >./musician-list> <musician-details [musician]="vm.activeMusician" >./musician-details> ./ng-container> `, }) export class MusiciansComponent { readonly vm$ = this.store.select(selectMusiciansPageViewModel); constructor(private readonly store: Store) {} } musicians.selectors.ts export const selectMusiciansPageViewModel = createSelector( selectFilteredMusicians, selectMusiciansQuery, selectActiveMusician, (musicians, query, activeMusician) .> ({ musicians, query, activeMusician, }) ); -
musicians.component.ts @Component({ selector: 'musicians', template: ` <ng-container *ngIf="vm$ | async
as vm"> <musician-search [query]="vm.query">./musician-search> <musician-list [musicians]="vm.musicians" [activeMusician]="vm.activeMusician" >./musician-list> <musician-details [musician]="vm.activeMusician" >./musician-details> ./ng-container> `, }) export class MusiciansComponent { readonly vm$ = this.store.select(selectMusiciansPageViewModel); constructor(private readonly store: Store) {} } musicians.selectors.ts export const selectMusiciansPageViewModel = createSelector( selectFilteredMusicians, selectMusiciansQuery, selectActiveMusician, (musicians, query, activeMusician) .> ({ musicians, query, activeMusician, }) ); -
★ Put global state in a single place ★ Use
selectors for derived state ★ Use view model selectors ★ Create reusable reducers ★ Treat actions as unique events ★ Group actions by source 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 ★ Use view model selectors ★ Create reusable reducers ★ Treat actions as unique events ★ Group actions by source 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 ★ Use view model selectors ★ Create reusable reducers ★ Treat actions as unique events ★ Group actions by source 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 ★ Use view model selectors ★ Create reusable reducers ★ Treat actions as unique events ★ Group actions by source Store Tips -
★ Name effects like functions ★ Keep effects simple ★
Don’t create “boiler” effects ★ Apply good action hygiene Effects Tips -
★ Name effects like functions ★ Keep effects simple ★
Don’t create “boiler” effects ★ Apply good action hygiene Effects Tips -
readonly composerAddedSuccess$ = createEffect( () .> { return this.actions$.pipe( ofType(composersApiActions.composerAddedSuccess),
tap(() .> this.alert.success('Composer saved successfully!')) ); }, { dispatch: false } ); -
readonly composerAddedSuccess$ = createEffect( () .> { return this.actions$.pipe( ofType(composersApiActions.composerAddedSuccess),
tap(() .> this.alert.success('Composer saved successfully!')) ); }, { dispatch: false } ); -
readonly showSaveComposerSuccessAlert$ = createEffect( () .> { return this.actions$.pipe( ofType(composersApiActions.composerAddedSuccess),
tap(() .> this.alert.success('Composer saved successfully!')) ); }, { dispatch: false } ); -
readonly showSaveComposerSuccessAlert$ = createEffect( () .> { return this.actions$.pipe( ofType(
composersApiActions.composerAddedSuccess, composersApiActions.composerUpdatedSuccess ), tap(() .> this.alert.success('Composer saved successfully!')) ); }, { dispatch: false } ); -
readonly showSaveComposerSuccessAlert$ = createEffect( () .> { return this.actions$.pipe( ofType(
composersApiActions.composerAddedSuccess, composersApiActions.composerUpdatedSuccess ), tap(() .> this.alert.success('Composer saved successfully!')) ); }, { dispatch: false } ); -
★ Name effects like functions ★ Keep effects simple ★
Don’t create “boiler” effects ★ Apply good action hygiene Effects Tips -
readonly loadMusician$ = createEffect(() .> { return this.actions$.pipe( ofType(musicianDetailsPage.opened), concatLatestFrom(()
.> this.store.select(selectMusicianIdFromRoute) ), concatMap(([, musicianId]) .> { return this.musiciansResource.getMusician(musicianId).pipe( mergeMap((musician) .> { return this.bandsResource.getBand(musician.bandId).pipe( map((band) .> ({ ...musician, bandName: band.name })) ); }), map((musician) .> musiciansApiActions.musicianLoadedSuccess({ musician }) ), catchError((error: { message: string }) .> of(musiciansApiActions.musicianLoadedFailure({ error })) ) ); }) ); }); -
readonly loadMusician$ = createEffect(() .> { return this.actions$.pipe( ofType(musicianDetailsPage.opened), concatLatestFrom(()
.> this.store.select(selectMusicianIdFromRoute) ), concatMap(([, musicianId]) .> { return this.musiciansResource.getMusician(musicianId).pipe( mergeMap((musician) .> { return this.bandsResource.getBand(musician.bandId).pipe( map((band) .> ({ ...musician, bandName: band.name })) ); }), map((musician) .> musiciansApiActions.musicianLoadedSuccess({ musician }) ), catchError((error: { message: string }) .> of(musiciansApiActions.musicianLoadedFailure({ error })) ) ); }) ); }); -
readonly loadMusician$ = createEffect(() .> { return this.actions$.pipe( ofType(musicianDetailsPage.opened), concatLatestFrom(()
.> this.store.select(selectMusicianIdFromRoute) ), concatMap(([, musicianId]) .> { return this.musiciansResource.getMusician(musicianId).pipe( mergeMap((musician) .> { return this.bandsResource.getBand(musician.bandId).pipe( map((band) .> ({ ...musician, bandName: band.name })) ); }), map((musician) .> musiciansApiActions.musicianLoadedSuccess({ musician }) ), catchError((error: { message: string }) .> of(musiciansApiActions.musicianLoadedFailure({ error })) ) ); }) ); }); -
readonly loadMusician$ = createEffect(() .> { return this.actions$.pipe( ofType(musicianDetailsPage.opened), concatLatestFrom(()
.> this.store.select(selectMusicianIdFromRoute) ), concatMap(([, musicianId]) .> { return this.musiciansResource.getMusician(musicianId).pipe( mergeMap((musician) .> { return this.bandsResource.getBand(musician.bandId).pipe( map((band) .> ({ ...musician, bandName: band.name })) ); }), map((musician) .> musiciansApiActions.musicianLoadedSuccess({ musician }) ), catchError((error: { message: string }) .> of(musiciansApiActions.musicianLoadedFailure({ error })) ) ); }) ); }); -
readonly loadMusician$ = createEffect(() .> { return this.actions$.pipe( ofType(musicianDetailsPage.opened), concatLatestFrom(()
.> this.store.select(selectMusicianIdFromRoute) ), concatMap(([, musicianId]) .> { return this.musiciansResource.getMusician(musicianId).pipe( mergeMap((musician) .> { return this.bandsResource.getBand(musician.bandId).pipe( map((band) .> ({ ...musician, bandName: band.name })) ); }), map((musician) .> musiciansApiActions.musicianLoadedSuccess({ musician }) ), catchError((error: { message: string }) .> of(musiciansApiActions.musicianLoadedFailure({ error })) ) ); }) ); }); -
readonly loadMusician$ = createEffect(() .> { return this.actions$.pipe( ofType(musicianDetailsPage.opened), concatLatestFrom(()
.> this.store.select(selectMusicianIdFromRoute) ), concatMap(([, musicianId]) .> { return this.musiciansResource.getMusician(musicianId).pipe( mergeMap((musician) .> { return this.bandsResource.getBand(musician.bandId).pipe( map((band) .> ({ ...musician, bandName: band.name })) ); }), map((musician) .> musiciansApiActions.musicianLoadedSuccess({ musician }) ), catchError((error: { message: string }) .> of(musiciansApiActions.musicianLoadedFailure({ error })) ) ); }) ); }); -
readonly loadMusician$ = createEffect(() .> { return this.actions$.pipe( ofType(musicianDetailsPage.opened), concatLatestFrom(()
.> this.store.select(selectMusicianIdFromRoute) ), concatMap(([, musicianId]) .> { return this.musiciansResource.getMusician(musicianId).pipe( mergeMap((musician) .> { return this.bandsResource.getBand(musician.bandId).pipe( map((band) .> ({ ...musician, bandName: band.name })) ); }), map((musician) .> musiciansApiActions.musicianLoadedSuccess({ musician }) ), catchError((error: { message: string }) .> of(musiciansApiActions.musicianLoadedFailure({ error })) ) ); }) ); }); -
readonly loadMusician$ = createEffect(() .> { return this.actions$.pipe( ofType(musicianDetailsPage.opened), concatLatestFrom(()
.> this.store.select(selectMusicianIdFromRoute) ), concatMap(([, musicianId]) .> { return this.musiciansResource.getMusician(musicianId).pipe( mergeMap((musician) .> { return this.bandsResource.getBand(musician.bandId).pipe( map((band) .> ({ ...musician, bandName: band.name })) ); }), map((musician) .> musiciansApiActions.musicianLoadedSuccess({ musician }) ), catchError((error: { message: string }) .> of(musiciansApiActions.musicianLoadedFailure({ error })) ) ); }) ); }); -
@Injectable({ providedIn: 'root' }) export class MusiciansService { constructor( private
readonly musiciansResource: MusiciansResource, private readonly bandsResource: BandsResource ) {} } -
@Injectable({ providedIn: 'root' }) export class MusiciansService { constructor( private
readonly musiciansResource: MusiciansResource, private readonly bandsResource: BandsResource ) {} getMusician(musicianId: string): Observable<Musician> { return this.musiciansResource.getMusician(musicianId).pipe( mergeMap((musician) .> { return this.bandsResource.getBand(musician.bandId).pipe( map((band) .> ({ ...musician, bandName: band.name })) ); }) ); } } -
readonly loadMusician$ = createEffect(() .> { return this.actions$.pipe( ofType(musicianDetailsPage.opened), concatLatestFrom(()
.> this.store.select(selectMusicianIdFromRoute) ), concatMap(([, musicianId]) .> { return this.musiciansResource.getMusician(musicianId).pipe( mergeMap((musician) .> { return this.bandsResource.getBand(musician.bandId).pipe( map((band) .> ({ ...musician, bandName: band.name })) ); }), map((musician) .> musiciansApiActions.musicianLoadedSuccess({ musician }) ), catchError((error: { message: string }) .> of(musiciansApiActions.musicianLoadedFailure({ error })) ) ); }) ); }); -
readonly loadMusician$ = createEffect(() .> { return this.actions$.pipe( ofType(musicianDetailsPage.opened), concatLatestFrom(()
.> this.store.select(selectMusicianIdFromRoute) ), concatMap(([, musicianId]) .> { return this.musiciansService.getMusician(musicianId).pipe( map((musician) .> musiciansApiActions.musicianLoadedSuccess({ musician }) ), catchError((error: { message: string }) .> of(musiciansApiActions.musicianLoadedFailure({ error })) ) ); }) ); }); -
readonly loadMusician$ = createEffect(() .> { return this.actions$.pipe( ofType(musicianDetailsPage.opened), concatLatestFrom(()
.> this.store.select(selectMusicianIdFromRoute) ), concatMap(([, musicianId]) .> { return this.musiciansService.getMusician(musicianId).pipe( map((musician) .> musiciansApiActions.musicianLoadedSuccess({ musician }) ), catchError((error: { message: string }) .> of(musiciansApiActions.musicianLoadedFailure({ error })) ) ); }) ); }); -
★ Name effects like functions ★ Keep effects simple ★
Don’t create “boiler” effects ★ Apply good action hygiene Effects Tips -
readonly invokeLoadMusicians$ = createEffect(() .> { return this.actions$.pipe( ofType( musiciansPageActions.currentPageChanged,
musiciansPageActions.pageSizeChanged ), map(() .> musiciansActions.loadMusicians()) ); }); -
readonly invokeLoadMusicians$ = createEffect(() .> { return this.actions$.pipe( ofType( musiciansPageActions.currentPageChanged,
musiciansPageActions.pageSizeChanged ), map(() .> musiciansActions.loadMusicians()) ); }); -
readonly invokeLoadMusicians$ = createEffect(() .> { return this.actions$.pipe( ofType( musiciansPageActions.currentPageChanged,
musiciansPageActions.pageSizeChanged ), map(() .> musiciansActions.loadMusicians()) ); }); -
readonly invokeLoadMusicians$ = createEffect(() .> { return this.actions$.pipe( ofType( musiciansPageActions.currentPageChanged,
musiciansPageActions.pageSizeChanged ), map(() .> musiciansActions.loadMusicians()) ); }); readonly loadMusicians$ = createEffect(() .> { return this.actions$.pipe( ofType(musiciansActions.loadMusicians), concatLatestFrom(() .> this.store.select(selectMusiciansPagination) ), switchMap(([, pagination]) .> { return this.musiciansService.getMusicians(pagination).pipe( ** **. */ ); }) ); }); -
readonly invokeLoadMusicians$ = createEffect(() .> { return this.actions$.pipe( ofType( musiciansPageActions.currentPageChanged,
musiciansPageActions.pageSizeChanged ), map(() .> musiciansActions.loadMusicians()) ); }); readonly loadMusicians$ = createEffect(() .> { return this.actions$.pipe( ofType(musiciansActions.loadMusicians), concatLatestFrom(() .> this.store.select(selectMusiciansPagination) ), switchMap(([, pagination]) .> { return this.musiciansService.getMusicians(pagination).pipe( ** **. */ ); }) ); }); -
readonly invokeLoadMusicians$ = createEffect(() .> { return this.actions$.pipe( ofType( musiciansPageActions.currentPageChanged,
musiciansPageActions.pageSizeChanged ), map(() .> musiciansActions.loadMusicians()) ); }); readonly loadMusicians$ = createEffect(() .> { return this.actions$.pipe( concatLatestFrom(() .> this.store.select(selectMusiciansPagination) ), switchMap(([, pagination]) .> { return this.musiciansService.getMusicians(pagination).pipe( ** **. */ ); }) ); }); -
readonly loadMusicians$ = createEffect(() .> { return this.actions$.pipe( ofType( musiciansPageActions.currentPageChanged,
musiciansPageActions.pageSizeChanged ), concatLatestFrom(() .> this.store.select(selectMusiciansPagination) ), switchMap(([, pagination]) .> { return this.musiciansService.getMusicians(pagination).pipe( ** **. */ ); }) ); }); -
★ Name effects like functions ★ Keep effects simple ★
Don’t create “boiler” effects ★ Apply good action hygiene Effects Tips -
readonly loadAlbum$ = createEffect(() .> { return this.actions$.pipe( ofType(albumDetailsPageActions.opened), concatLatestFrom(()
.> this.store.select(selectAlbumIdFromRoute)), concatMap(([, albumId]) .> { return this.albumsService.getAlbum(albumId).pipe( mergeMap(({ songs, composers }) .> [ songsActions.loadSongsSuccess({ songs }), composersActions.loadComposersSuccess({ composers }), ]), catchError(** **. */) ); }) ); }); -
readonly loadAlbum$ = createEffect(() .> { return this.actions$.pipe( ofType(albumDetailsPageActions.opened), concatLatestFrom(()
.> this.store.select(selectAlbumIdFromRoute)), concatMap(([, albumId]) .> { return this.albumsService.getAlbum(albumId).pipe( mergeMap(({ songs, composers }) .> [ songsActions.loadSongsSuccess({ songs }), composersActions.loadComposersSuccess({ composers }), ]), catchError(** **. */) ); }) ); }); -
readonly loadAlbum$ = createEffect(() .> { return this.actions$.pipe( ofType(albumDetailsPageActions.opened), concatLatestFrom(()
.> this.store.select(selectAlbumIdFromRoute)), concatMap(([, albumId]) .> { return this.albumsService.getAlbum(albumId).pipe( mergeMap(({ songs, composers }) .> [ songsActions.loadSongsSuccess({ songs }), composersActions.loadComposersSuccess({ composers }), ]), catchError(** **. */) ); }) ); }); -
readonly loadAlbum$ = createEffect(() .> { return this.actions$.pipe( ofType(albumDetailsPageActions.opened), concatLatestFrom(()
.> this.store.select(selectAlbumIdFromRoute)), concatMap(([, albumId]) .> { return this.albumsService.getAlbum(albumId).pipe( mergeMap(({ songs, composers }) .> [ songsActions.loadSongsSuccess({ songs }), composersActions.loadComposersSuccess({ composers }), ]), catchError(** **. */) ); }) ); }); -
readonly loadAlbum$ = createEffect(() .> { return this.actions$.pipe( ofType(albumDetailsPageActions.opened), concatLatestFrom(()
.> this.store.select(selectAlbumIdFromRoute)), concatMap(([, albumId]) .> { return this.albumsService.getAlbum(albumId).pipe( map(({ songs, composers }) .> albumsApiActions.albumLoadedSuccess({ songs, composers }), ), catchError(** **. */) ); }) ); }); -
export const songsReducer = createReducer( on(albumsApiActions.albumLoadedSuccess, (state, { songs })
.> ({ ...state, songs, })) ); export const composersReducer = createReducer( on(albumsApiActions.albumLoadedSuccess, (state, { composers }) .> ({ ...state, composers, })) ); -
★ Name effects like functions ★ Keep effects simple ★
Don’t create “boiler” effects ★ Apply good action hygiene Effects Tips