Greg
Greg

Reputation: 1402

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

Upvotes: 0

Views: 218

Answers (1)

Mrk Sef
Mrk Sef

Reputation: 8022

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
});

Upvotes: 1

Related Questions