I'm still learning rxjs and run into a problem.
I have a service with a BehavioralSubject intended to hold a single value and emmiting it to other components when changed. The other components will change the value so they comunciate bettween components- I am using it with a component that does an http request to save a document when it recieves a specific value from the subscription (another component is in charge of changing that value). Once I start the application it works fine, but the second time it emits the value 2 times, sending 2 http requests, 3 the 3rd time, 4 the 4th, and so on...
Here is the code for the service
save.service.ts
export class SaveService {
readonly state: BehaviorSubject<SAVE_STATE>;
constructor() {
this.state = new BehaviorSubject(SAVE_STATE.IDLE);
}
public save() {
this.state.next(SAVE_STATE.SAVE);
}
public reset() {
this.state.next(SAVE_STATE.RESET);
}
public changed() {
this.state.next(SAVE_STATE.CHANGED);
}
public idle() {
this.state.next(SAVE_STATE.IDLE);
}
public loading() {
this.state.next(SAVE_STATE.LOADING);
}
}
Here is the component that changes the value
save-options.component.ts
private show: boolean;
private loading: boolean;
constructor(private saveService: SaveService) { }
ngOnInit() {
this.show = false;
this.saveService.state.subscribe((state) => {
this.show = state === SAVE_STATE.IDLE ? false : true;
this.loading = state === SAVE_STATE.LOADING ? true : false;
});
}
saveAction() {
this.saveService.save();
}
discardAction() {
this.saveService.reset();
}
Here is the function in the component that recives the value and makes the request, this method is called in the ngOnInit()
create-or-edit.component.ts
private listenToSaveEvents() {
this.saveService.state.subscribe((state) => {
console.log(state);
switch(state){
case SAVE_STATE.SAVE:
this.saveStore();
break;
case SAVE_STATE.RESET:
this.undo();
break;
default:
break;
}
});
}
The later function is the one executing multiple times incrementally. the result of the log is: First execution
0
3
4
3
Second execution
0
3
4
3
0
3
(2)4
(2)3
I might be using BehaviorSubject wrong but can't manage to figure out why, thank you.