Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading