How Can I Toggle (start/ Stop) An Ngrx Action?
Below is an action that dispatches a load action to the store. The corresponding effect will handle the request and sends back the response items. But What I want is I want to togg
Solution 1:
to have a toggle in an effect you need 2 actions (start and stop) and to use takeUntil
.
//effects.ts
loadCourierItems$ = createEffect(() =>
this.actions$.pipe(
ofType(actions.start),
exhaustMap(action => interval(1000).pipe(
// logic for every second activity.
map(actions.everySecondAction()),
)),
takeUntil(this.actions$.pipe(ofType(actions.stop))),
repeat(),
)
)
//app.component.ts
constructor(private store: Store<CourierItemsState>) {
}
ngOnInit() {
this.store.dispatch(startAction());
}
ngOnDestroy() {
this.store.dispatch(stopAction());
}
Post a Comment for "How Can I Toggle (start/ Stop) An Ngrx Action?"