Combine multiple ngrx subscribe into one

I currently subscribe on a selector to dispatch an action and I subscribe on that as well but I was wondering how I can combine them so I don’t need to use a subscribe in a subscribe

this.store$
  .pipe(
    select(getStateParams),
    isNotNullOrUndefined(),
    map(({ id }) => ({
      id,
    })),
    isNotNullOrUndefinedObjectProperties(),
    stringObjectPropertiesToNumbers(),
    take(1),
    takeUntil(this.ngUnsubscribe$)
)
.subscribe(({ id }) => {
  this.store$.dispatch(
    updateContent({
      id
    })
  ).subscribe(() => { //...do something)}
});

I have been playing around with switchMap and mergeMap but can’t seem to get it to work

>Solution :

SwitchMap should work just fine… what have you tried?

For example:

this.store$.pipe(
  
  select(getStateParams),
  isNotNullOrUndefined(),
  map(({ id }) => ({ // <- This map is a null operation
    id,              // Why is it here? What are you 
  })),               // Hoping that this does?
  isNotNullOrUndefinedObjectProperties(),
  stringObjectPropertiesToNumbers(),
  switchMap(idOby => this.store$.dispatch(
    updateContent(idOby)
  )),
  take(1),
  takeUntil(this.ngUnsubscribe$)

).subscribe(() => { 
  //...do something
});

Leave a Reply