Create StorageMap#replace #471
-
Configuration
Description of the issueI have some options that are act like toggles... as for example, the user preferred menu style: collapsed or not and some others... and for this, I have to access the current value stored to mount the new value. Now I'm doing it like this:
...but it could be a little verbose if we do it several times and that's why I'd like to propose something like this:
... so we could use it Note that I'm unsure if it's on the scope of this project, but as I needed to solve this situation, I thought it was a good idea to post here. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This is a common misconception, but client-side storage in general is just here to save some values for when the user leaves the application and when he/she comes back. But it should not replace your classic properties management, for many reasons:
So it is not the role of client-side storage in general to do the replace. Your service should look like that: @Injectable({
providedIn: 'root',
})
export class PreferencesService {
/* This property is still required */
menuToggled = false;
constructor(
private storage: StorageMap,
) {
this.storage.get('menuToggled', { type: 'boolean' }).subscribe((menuToggled) => {
this.menuToggled = menuToggled ?? false;
});
}
toggle(): void {
this.menuToggled = !this.menuToggled;
this.storage.set('menuToggled', this.menuToggled).subscribe();
}
} |
Beta Was this translation helpful? Give feedback.
This is a common misconception, but client-side storage in general is just here to save some values for when the user leaves the application and when he/she comes back. But it should not replace your classic properties management, for many reasons:
indexedDb
for better performances thanlocalStorage
, it is still heavier to access client-side storage than just a property of your classesSo it is not the role of client…