Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ngrx Component Store: Can I call another effect inside a effect?

Tags:

angular

ngrx

I'm currently implementing a feature that "open a snackbar"(effect),if close snackbar with action then "open a dialog"(effect) and finally make an API call(effect) after the dialog close.

I want to implement this feature in Component Store,

openDialog = this.effect((trigger$) => 
    trigger$.pipe( 
    exhuastMap(() => this.dialog.open(comp).afterClosed().pipe(
    tap((result) => this.callApi(result))))))

snackBar = this.effect((trigger$)=>
   trigger$.pipe(
   exhuastMap(()=> this.snackBar.open("test","OK").onAction().pipe(
   tap(() => this.openDialog())))))

I noticed that the source implementation of effect() will return a subscription, so is calling another effect inside an effect OK or not? If not, I will need to create additional states to trigger above effects or convert some of them into normal function in the service.

like image 476
Chi Avatar asked Nov 05 '25 18:11

Chi


1 Answers

Looking at the source code on GitHub, there should be no problem with calling one effect from another. The effect function doesn't do that much. It will just push the values you call the effect with to a Subject (your trigger$), so it shouldn't matter where you call it from.

like image 67
lbilger Avatar answered Nov 07 '25 09:11

lbilger