I'm trying to understand ngrx/effects. I have built a simple function that increments number by 1 with each click. But it's going in an infinite loop when clicked, not sure whats going on. I'm sure im making some stupid mistake.
monitor.effects.ts
@Injectable()
export class MonitorEffects {
@Effect()
compute$: Observable<Action> = this.actions$
.ofType(monitor.ActionTypes.INCREMENT)
.map((action: monitor.IncrementAction) => action.payload)
.switchMap(payload => {
return this.http.get('https://jsonplaceholder.typicode.com/users')
.map(data => new monitor.IncrementAction(payload+1))
.catch(err => of(new monitor.InitialAction(0)))
});
constructor(private actions$: Actions, private http:Http ) {};
}
monitor.component.ts
ngOnInit() {
this.storeSubsciber = this
.store
.select('monitor')
.subscribe((data: IMonitor.State) => {
this.value = data.value;
this.errorMsg = data.error;
this.currentState = data.currentState;
});
}
increment(value: number) {
this.store.dispatch(new monitorActions.IncrementAction(value));
}
monitor.reducer.ts
export const monitorReducer: ActionReducer<IMonitor.State> = (state = initialState, action: Actions) => {
switch (action.type) {
case ActionTypes.INCREMENT:
return Object.assign({}, { value: action.payload, currentState: ActionTypes.INCREMENT, error: null });
...
...
...
default:
return Object.assign({}, { value: 0, currentState: ActionTypes.INITIAL, error: null });
}
}