phenomnomnominal / angular-2-local-storage

LocalStorageService for Angular 2 with the same API as angular-local-storage
MIT License
93 stars 49 forks source link

Change sessionStorage/localStorage dynamically? #9

Open Ks89 opened 8 years ago

Ks89 commented 8 years ago

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

Is it exist a method like "setStorageType"?

Ks89 commented 8 years ago

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?

elwynelwyn commented 8 years ago

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

louisdoe commented 7 years ago

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');
A1rPun commented 7 years ago

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

alignsoft commented 7 years ago

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.

phenomnomnominal commented 7 years ago

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 😄

alignsoft commented 7 years ago

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.

phenomnomnominal commented 7 years ago

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!

alignsoft commented 7 years ago

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