I want to cancel the previous http request if i made the new http request to server but it is not working as expected to me.
i want to cancel the request if some value got changed in the function i mean
if the value change the old request should cancel and new one should create
for this i have done this
1) Created a subject - The thing that will be watched by the observable
public stringVar = new Subject<string>();
2) Create an observable to watch the subject and send out a stream of updates (You will subscribe to this to get the update stream)
public stringVar$ = this.stringVar.asObservable()
3)In a particular function i am getting the value as an argument so i perform these steps
testFunction(value){
this.stringVar.next(listValueSelected);
this.testOutput = this.stringVar$.pipe(
switchMap(() => {
return this.teseService.getEntries(val_one, val_two,val_three);
})
)
this.testOutput.subscribe(result => console.log(`${result}`));
}
The request is cancelble when value is going to change but getting vary strange behaviour first time only one request is mode but when i click second time it calling api two time and this will go on .What is wrong with my code ?
in the component.ts
export class TestComponent {
testOutput :Observable<any>;
stringVar = new Subject<number>();
stringVar$ = this.stringVar.asObservable();
testResult(selectedValue) {
this.stringVar.next(selectedValue);
this.stringVar$.subscribe(data => {
});
this.testOutput = this.stringVar$.pipe(
switchMap(() => {
return this.testService.getTestEntries(this.x,this.y,this.z);
})
)
this.testOutput.subscribe(result => console.log(`${result}`));
}
}
in the service file
getTestEntries(x, y,selectedValue): Observable<any>{
let apiUrl = selectedValue=='3'?this.baseUrl1:this.baseUrl ;
return this.http.post(apiUrl,obj).map((response: Response) => {
return response;
})
}
i want to cancel my old request and create new if the value "selectedValue" changed in the component.