@Effect()
public firstAction$: Observable<Action> = this.actions$.pipe(
ofType( 'FIRST_ACTION' ),
mapTo( new SecondAction() )
);
This effect says:
@Effect()
public dashboardLoadRequired$: Observable<Action> =
this.actions$.pipe(
ofType( 'SIGN_IN_SUCCESS', 'OPEN_DASHBOARD'),
mapTo( new LoadDashboardData() )
);
This effect says:
@Effect()
public signInSuccess$: Observable<Action> = this.actions$.pipe(
ofType( 'SIGN_IN_SUCCESS' ),
concatMapTo([
new LoadDashboardData(),
new LoadChatHistory()
])
);
This effect says:
Another common effect you might write is when multiple action types trigger the same result. The ofType operator allows for multiple types.
@Effect()
public dashboardLoadRequired$: Observable<Action> =
this.actions$.pipe(
ofType( 'SIGN_IN_SUCCESS', 'OPEN_DASHBOARD'),
mapTo( new LoadDashboardData() )
);
This effect says:
@Effect()
public signInSuccess$: Observable<Action> = this.actions$.pipe(
ofType( 'SIGN_IN_SUCCESS' ),
concatMapTo([
new LoadDashboardData(),
new LoadChatHistory()
])
);
This effect says:
For this use-case, add {dispatch: false} to the effect decorator.
@Effect({dispatch: false})
public signInSuccess$: Observable<Action> = this.actions$.pipe(
ofType( 'SIGN_IN_SUCCESS' ),
tap( () => this.router.navigate(['dashboard']) )
);
This effect says:
The above examples have used the operators mapTo and concatMapTo. These operators map to static values. Sometimes you want to map to dynamic values, such as using a value passed in via an action’s payload property. For dynamic values, use the matching operators map or concatMap which expect a function rather than a static value.
@Effect()
public signInSuccess$: Observable<Action> = this.actions$.pipe(
ofType( 'SIGN_IN_SUCCESS' ),
map( action => action.payload ),
concatMap( payload => [
new LoadDashboardData( payload.companyId ),
new LoadChatData( payload.userId )
])
);
This effect says: