Reputation: 63
I have defined Callback function which takes event and it is called on interaction with UI. Event contains data about done changes. When CallBack Function is called i have to update local storage data by data from event and send post request to server. In some times call back function is called several times at the same times and it is expected behavior. My problem is how not to sending sometimes several request to server but send when the last event is emitted only.
I was trying sth like this but it sends a lot of request in some cases.
callBackFinction: (event) => {
this.MyService.subject.next(event);
this.MyService.UpdateData(event);
this.MyService.subject.pipe(debounceTime(1000)).subscribe(() => {
http.post('/url', data);
});
}
Upvotes: 1
Views: 74
Reputation: 2330
You need subject in ngOnInit
or constructor
. Because you will generate a new subscription when each callback, and there is not unsubscribe.
ngOnInit(): void {
this.MyService.subject.pipe(debounceTime(1000)).subscribe(data => {
http.post('/url', data);
});
}
callBackFinction: (event) => {
this.MyService.subject.next(event);
this.MyService.UpdateData(event);
}
Upvotes: 1