PillowPillow/ng2-webstorage

isStorageAvailable is not a function

Closed this issue · 9 comments

Versions (please complete the following information):

  • NgxWebstorage: [e.g. 3.0.x]
  • Angular: [e.g. 7.2.0]

Describe the bug
I upgraded to the new version.. Counter to the readme the isStorageAvailable() function is not part of the LocalStorageService module.

To Reproduce
Steps to reproduce the behavior:

  1. Go to ' if(!this.storage.isStorageAvailable())...'

Expected behavior
Return true or false.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [Windows 10]
  • Browser [Chrome]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

Hi @phantomodm, thanks for your feedback. The current readme's not up to date, i have to finish the wiki that i started (this is the last thing to do before the release of the 3.0.0).

The new services do not implements the isStorageAvailable anymore since this information's now holded by the storage strategy.

You can verify if a strategy's available by injecting SessionStorageStrategy, LocalStorageStrategy or InMemoryStrategy and using the isAvailable method.

The InMemory strategy is the fallback used when the storage strategy is not available.

Btw, can you tell me why do you need the isStorageAvailable ?

Regards,

Yes I understood that, I mean why do you need to know if the storage is available since the library handle this case by using another strategy. You shouldn’t have to check that.

I removed from the current readme the method as you suggested, if you can explain me your use case I could think about adding it to the new version.

The isAvailable method exposed above should do the trick for now.

Hello @phantomodm , did you solve your issue ? any news ?

Hello @phantomodm, thanks for your reply. I'll close this issue then.

FYI : As i said before, the library handle the cases where the localStorage/sessionStorage aren't available for you by writing in an memory cache.

Nicolas, If the library handles this case, how do we know where to look for the data? In my logic, I check if Local Storage is available so I can read and write to it. If the new logic is putting my data elsewhere because Local Storage is not available, is it smart enough to pull that data when I ask for it from Local Storage?

Hi @RPbarfield,

the library uses differents strategies to store and retrieve values. By default there are three of them LocalStorageStrategy, SessionStorageStrategy and InMemoryStrategy but you can create your own strategy if necessary, handling IndexedDB for example.
Each strategy implements a property that determines if the storage used is available.
This property's used during the strategy indexing at the app initialization, if it returns false then the library will provides the InMemoryStrategy instead of the requested one. You'll can use it transparently, the only difference is that your data will not stored in the requested storage but kept in a simple object and lost during the next page reload.

I understand that you could need to act differently if the storage's not available. To do so you can use the same helper that i use in strategies definitions.

@Injectable({ providedIn: 'root' })
class FooBar {
   constructor(@Inject(SESSION_STORAGE) protected storage: WebStorage) {
        const isAvailable = CompatHelper.isStorageAvailable(this.storage);

       if(isAvailable) do();
    }
}