angular-2-local-storage icon indicating copy to clipboard operation
angular-2-local-storage copied to clipboard

Change sessionStorage/localStorage dynamically?

Open Ks89 opened this issue 8 years ago • 9 comments

Is it possibile to change from sessionStorage to localStorage dynamically, for example into a service?

Is it exist a method like "setStorageType"?

Ks89 avatar Aug 22 '16 23:08 Ks89

Or, is it possibile to import this library two times renaming the service as Local... and Session... vars to use both of them at the same time?

Ks89 avatar Aug 22 '16 23:08 Ks89

Hi @Ks89, no, currently you cannot do that.

However it looks like since @phenomnomnominal forked the original grevory/angular-local-storage repo that functionality has been added, so at some point it would be good to get that new functionality added in here so the two libraries stay at parity.

Feel free to submit a PR if this is a feature you need

elwynelwyn avatar Nov 18 '16 20:11 elwynelwyn

Hi, it'd be good, like with angular-local-storage, to be able to add a third argument to dynamically set the storage type,like this:

	this.localStorageService.set('name', value, 'sessionStorage');

Can someone code this ?

In the meanwhile, is the following possible ?

this.localStorageService.setStorageType ('sessionStorage');
this.localStorageService.set('name', value);
this.localStorageService.setStorageType ('localStorage');

louisdoe avatar Mar 27 '17 14:03 louisdoe

I think everyone will be happy if setStorageType is public accessible. Thoughts about this?

A1rPun avatar Jun 12 '17 10:06 A1rPun

In most of my projects I use sessionStorage for some things, and localStorage for others, so it would be very useful to be able to declare a localStorageService and sessionStorageService and then use them as required as required.

For example, as session storage survives page reloads, I typically store user state information in session storage so that the user can authenticate, and then if they reload the browser window for some reason I can keep them authenticated and not punt them back to the login, however if they close the window then that user data is deleted.

On the other side, if I have a user doing data collection / data entry, I store that data in local storage until I've saved it to the database and then I remove it, that way if the user has spent an hour collecting data without saving to the server, and the browser crashes or they close a tab accidentally, once they log back into the application I can restore that data collection state and they can continue to work without losing data.

alignsoft avatar Jun 21 '17 22:06 alignsoft

I think the best way to do this is to have a second service, like this:

@Injectable()
export class SessionStorageService extends LocalStorageService {
    constructor () {
        super({ storageType: 'sessionStorage' });
    }
}

Then you can provide whichever one you need and use it explicitly.

P.S. I'm well aware this repo needs some love, I'll add docs and tests when I get some time 😄

phenomnomnominal avatar Jun 21 '17 22:06 phenomnomnominal

Is that supported in the current version, or are you thinking of that for possible future implementation for this feature request?

It's a good idea, I think, and would certainly accommodate my use case.

alignsoft avatar Jun 21 '17 22:06 alignsoft

That's currently available. We're using the same mechanism to split our localStorage so we can manage parts of it differently, e.g. clearing some values when a user logs out. You can thank @Lightw3ight!

phenomnomnominal avatar Jun 21 '17 22:06 phenomnomnominal

Fantastic! Thanks for the explanation, and thanks @Lightw3ight!

alignsoft avatar Jun 21 '17 22:06 alignsoft