At the moment I have the following setup :
In my component :
/**/
onCitySearch() {
this.citiesService.getCities(this.citySearchControl.value);
}
In my service :
/**/
getCities(term: string) {
if (term.length < 3 || term === this.lastSearchTerm || this.autocompleteAjaxLock) {
return;
}
this.autocompleteAjaxLock = true;
this.httpClient
.get<City[]>(environment.api_url + '/api/city/autocomplete?term=' + term)
.subscribe(
(cities) => {
this.lastSearchTerm = term;
this.cities = cities;
this.citiesUpdated.next([...this.cities]);
this.autocompleteAjaxLock = false;
},
error => this.autocompleteAjaxLock = false
);
}
My component is subscribed to the service cities and automatically gets notified when the search is updated. Now, with the lock system I setup, if the user types quickly my search doesnt get updated. I read around about switchMap, that should be appropriate for my use case. Where and how should I implement it ? In my component or in the service function ?