I have a function that retrieves an observable like so:
constructor(private _http: HttpClient) {}
getUsers(location){
return this._http.get(`https://someurl?location=${location}`)
.pipe(
map((response: any) => response),
distinctUntilChanged()
)
}
(Assuming all required dependencies are imported)
To display the list of users, I use the loadUsers method.
loadUsers(location){
this.getUsers(location).subscribe( users => {
this.userList = users;
});
}
ngOnInit(){
this.loadUsers('mumbai');
}
The code above loads the list of users for those located in Mumbai.
Now on the UI, there is a list of locations with checkboxes next to them like:
Mumbai,
Delhi,
Kerala
Clicking on a location will trigger the loadUsers method with the selected location as a parameter.
When clicking on Mumbai again (without selecting any other location first), I don't want it to reload the Mumbai users since they were already loaded initially.
I've tried using distinctUntilChanged() but it doesn't prevent the unnecessary call when selecting Mumbai from the checkbox list.
Note: This scenario is fictional. I shared it to explain my issue clearly.
I'm new to Angular and RxJS. Any assistance would be appreciated.