Pass the action value in effects for switchMap via both another switchMap and filter?

222 Views Asked by At

There is a code

  @Effect()
  myEffect$ = this.actions$.pipe(
    ofType(MyActions.doSomething),
    tap(() => this.store.dispatch(MyActions.doSomething2())),
    switchMap(action => {
      

that works. I need to inject a filter right before tap. How to do it so that switchMap doesn't lose the action value?

I'm trying to do it like

  @Effect()
  myEffect$ = this.actions$.pipe(
    ofType(MyActions.doSomething),
    switchMap(() => this.store.pipe(select(mySelectors.getAmount), take(1))),
    filter(amount => amount <= 0),
    tap(() => this.store.dispatch(MyActions.doSomething2())),
    switchMap(action => {
    ....

however it says property action.xxxx doesn't exist on type number which is clear why. How not to lose the action value returned by MyActions.doSomething?

1

There are 1 best solutions below

0
On BEST ANSWER

I would move the filter within the operator chain of the amount store directly if it only applies to those values for better encapsulation. Then you don't have to export the amounts outside of that switchMap, just remapping them to actions:

  @Effect()
  myEffect$ = this.actions$.pipe(
    ofType(MyActions.doSomething),
    switchMap(action => this.store.pipe(
      select(mySelectors.getAmount),
      take(1),
      filter(amount => amount <= 0), // filter amounts here
      map(_ => action) // remap to actions
    ))),
    tap(() => this.store.dispatch(MyActions.doSomething2())),
    switchMap(action => {
      // ...
    })