I am trying my best to enable my ngrx effects conditionally call itself every 2 seconds in following way using timer:
@Effect()
loadSummary$ = this.actions$
.pipe(
ofType(SummaryTypes.SAVE_SUMMARY),
switchMap((action:any) => (action.running ? timer(0, 2000) : NEVER)),
switchMap((action: any) => {
console.log(action); /* prints 0 (how is this possible?!) */
return this.analyticsService.getData(action.fetchArr, action.startAndEndDate).pipe(
catchError((err) => {
/* log error */
}),
)
}),
)
So I am calling second switchMap after every 2 seconds. action.running
also correctly returns true or false in first switchMap call, however, the console.log(action)
returns 0 and similarly, action.fetchArr
and action.startAndDate
returns undefined.
This is because i am not able to return first action
to second action as is. Could someone guide me to what am I doing wrong?
What I want:
I want my effect to call itself every 2 seconds and if action.running
returns false then it must immediately stop. console.log(action);
should correctly return the passed object in second switchMap
and not 0.
What I tried:
Tried reading about effects from last few hours and even tried using takeUntil
but not able to find any particular way.