Skip to content

Commit dff0988

Browse files
committed
Validates the availability of the storage mechanism when a new instance is created
1 parent c017f44 commit dff0988

File tree

9 files changed

+236
-103
lines changed

9 files changed

+236
-103
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# 2.0.0
2+
3+
### Breaking changes
4+
5+
This version bumps to major because the old method `.isAvaliable` is renamed to `.isAvailable`
6+
7+
### Improvements
8+
9+
1. Validates the availability of the storage mechanism when a new instance is created.
10+
If the requested storage is not available, then the first available storage is used.
11+
112
# 1.0.4
213

314
### Improvements

README.md

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ being returned by the intercepted method. See documentation [here](#static-metho
2828
3. [API: storage/default](#storage-or-default)
2929
4. [API: WebStorage](#webstorage)
3030
5. [API: configStorage](#configstorage)
31-
6. [API: isAvaliable](#isavaliable)
31+
6. [API: isAvailable](#isavailable)
3232
7. [Shimming](#shimming)
3333
8. [Running the project](#running-the-project)
3434

@@ -49,8 +49,8 @@ $ yarn add proxy-storage
4949
`proxy-storage` can be included directly from a CDN in your page:
5050

5151
```html
52-
<!-- last version: 1.0.4 -->
53-
<script src="https://cdn.rawgit.com/jherax/proxy-storage/1.0.4/dist/proxy-storage.min.js"></script>
52+
<!-- last version: 2.0.0 -->
53+
<script src="https://cdn.rawgit.com/jherax/proxy-storage/2.0.0/dist/proxy-storage.min.js"></script>
5454
```
5555

5656
In the above case, the [library](#api) is included into the namespace `proxyStorage` as a global object.
@@ -136,7 +136,7 @@ The availability is determined in the following order:
136136
4. **`memoryStorage`**: internal storage mechanism that can be used as a **fallback** when none of the above mechanisms are available.
137137
The behavior of _`memoryStorage`_ is similar to _`sessionStorage`_
138138

139-
**Note**: you can override the default storage mechanism by setting the new storage type with [configStorage.set()](#configstorage)
139+
**TIP**: you can override the default storage mechanism by setting the new storage type with [configStorage.set()](#configstorage)
140140

141141
#### Example
142142

@@ -183,18 +183,18 @@ has a special behavior, allowing you to set an expiration date and also to speci
183183
import { WebStorage } from 'proxy-storage';
184184

185185
const cookieStore = new WebStorage('cookieStorage');
186-
const data = {
186+
let data = {
187187
start: new Date().toISOString(),
188188
sessionId: 'J34H5J34609-DSFG7BND98W3',
189189
platform: 'Linux x86_64',
190190
};
191-
const options = {
191+
let options = {
192192
path: '/jherax',
193-
expires: { hours: 6 }
193+
expires: new Date('2017/03/06')
194194
};
195195

196196
cookieStore.setItem('activity', data, options);
197-
cookieStore.setItem('valid', true, {expires: new Date('2017/01/02')});
197+
cookieStore.setItem('testing', true, { expires: {hours:1} });
198198
```
199199

200200
### Getting all items stored
@@ -257,6 +257,27 @@ function clearDataFromStorage() {
257257
}
258258
```
259259

260+
**NOTE**: If you request a new instance of a storage mechanism that are not available, then you will get an instance
261+
of the first storage mechanism available, allowing you to keep saving data to the store. This is useful when you are relying
262+
on a specific storage mechanism. Let's see an example:
263+
264+
```javascript
265+
import storage, * as proxyStorage from 'proxy-storage';
266+
267+
// let's suppose sessionStorage is not available
268+
proxyStorage.isAvailable.sessionStorage = false;
269+
270+
// saves data to the default storage mechanism (localStorage)
271+
storage.setItem('querty', 12345);
272+
273+
const session = new proxyStorage.WebStorage('sessionStorage');
274+
session.setItem('asdfg', 67890);
275+
276+
// as sessionStorage is not available, the instance obtained
277+
// is the first available storage mechanism: localStorage
278+
console.log(session);
279+
```
280+
260281
### Static Methods
261282

262283
The **`WebStorage`** class provides the static method `interceptors` which allows us to register callbacks for each of the prototype
@@ -268,7 +289,7 @@ in the storage mechanism.
268289
- `command`_`{string}`_ Name of the API method to intercept. It can be `setItem`, `getItem`, `removeItem`, `clear`
269290
- `action`_`{function}`_ Callback executed when the API method is called.
270291

271-
**Tip**: interceptors are registered in chain, allowing the transformation in chain of the value passed through.
292+
**TIP**: interceptors are registered in chain, allowing the transformation in chain of the value passed through.
272293

273294
#### Example
274295

@@ -302,7 +323,7 @@ WebStorage.interceptors('getItem', (key, value) => {
302323
WebStorage.interceptors('removeItem', (key) => console.log(`removeItem: ${key}`));
303324

304325
// localStorage is the default storage mechanism
305-
storage.setItem('storage-test', {id: 1040, data: 'it works!'});
326+
storage.setItem('storage-test', { id: 1040, data: 'it works!' });
306327
storage.getItem('storage-test');
307328
```
308329

@@ -326,14 +347,14 @@ console.log('Default:', storageName);
326347
storage.setItem('defaultStorage', storageName);
327348

328349
// sets the new default storage mechanism
329-
configStorage.set('sessionStorage');
350+
configStorage.set('cookieStorage');
330351
storageName = configStorage.get();
331352
console.log('Current:', storageName);
332353

333354
storage.setItem('currentStorage', storageName);
334355
```
335356

336-
## isAvaliable
357+
## isAvailable
337358

338359
_@type_ `Object`. Determines which storage mechanisms are available. It contains the following flags:
339360

@@ -348,29 +369,23 @@ _@type_ `Object`. Determines which storage mechanisms are available. It contains
348369
import storage, * as proxyStorage from 'proxy-storage';
349370
// * imports the entire module's members into proxyStorage.
350371

351-
console.info('Available storage mechanisms');
352-
console.log(proxyStorage.isAvaliable);
372+
const flags = proxyStorage.isAvailable;
353373

354-
function init() {
355-
// memoryStorage is always available
374+
if (!flags.localStorage && !flags.sessionStorage) {
375+
// forces the storage mechanism to memoryStorage
356376
proxyStorage.configStorage.set('memoryStorage');
377+
}
357378

358-
if (isSafariInPrivateMode(proxyStorage.isAvaliable)) {
359-
// do something additional...
360-
}
379+
let data = storage.getItem('hidden-data');
361380

381+
if (!data) {
362382
storage.setItem('hidden-data', {
363383
mechanism: 'memoryStorage',
364-
availability: 'Current page: you can refresh your window'
384+
availability: 'Current page: you can refresh the page, data still remain'
365385
});
366-
367-
let data = storage.getItem('hidden-data');
368-
console.log('in memoryStorage', data);
369386
}
370387

371-
function isSafariInPrivateMode(flags) {
372-
return !flags.localStorage && !flags.sessionStorage;
373-
}
388+
console.log('in memoryStorage', data);
374389
```
375390

376391
## Shimming

0 commit comments

Comments
 (0)