I am currently working on developing an Angular service that can seamlessly switch between making actual API calls and utilizing local storage within a single method invocation.
component.ts
this.userService.getAllUsers().subscribe(data => {
console.log('users', data); //getUser
});
user.service.ts
export class UsersService {
constructor(...){ }
getAllUsers() {
return this.storageMap.get('users').pipe(
switchMap(data => {
if (data) return data;
else return this.userStore.collection('users').valueChanges();
})
);
}
saveUsers(data){
return this.storageMap.set('users', data);
}
}
Local storage library: @ngx-pwa/local-storage
Issues:
I am unsure how to return "data" as an Observable from the "getAllUsers" method if it is available in storageMap.
How can I execute the "saveUsers" method to store data in local storage within "getAllUsers" if the data is undefined.
Thanks in advance!