@@ -28,7 +28,7 @@ being returned by the intercepted method. See documentation [here](#static-metho
28
28
3 . [ API: storage/default] ( #storage-or-default )
29
29
4 . [ API: WebStorage] ( #webstorage )
30
30
5 . [ API: configStorage] ( #configstorage )
31
- 6 . [ API: isAvaliable ] ( #isavaliable )
31
+ 6 . [ API: isAvailable ] ( #isavailable )
32
32
7 . [ Shimming] ( #shimming )
33
33
8 . [ Running the project] ( #running-the-project )
34
34
@@ -49,8 +49,8 @@ $ yarn add proxy-storage
49
49
` proxy-storage ` can be included directly from a CDN in your page:
50
50
51
51
``` 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 >
54
54
```
55
55
56
56
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:
136
136
4 . ** ` memoryStorage ` ** : internal storage mechanism that can be used as a ** fallback** when none of the above mechanisms are available.
137
137
The behavior of _ ` memoryStorage ` _ is similar to _ ` sessionStorage ` _
138
138
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 )
140
140
141
141
#### Example
142
142
@@ -183,18 +183,18 @@ has a special behavior, allowing you to set an expiration date and also to speci
183
183
import { WebStorage } from ' proxy-storage' ;
184
184
185
185
const cookieStore = new WebStorage (' cookieStorage' );
186
- const data = {
186
+ let data = {
187
187
start: new Date ().toISOString (),
188
188
sessionId: ' J34H5J34609-DSFG7BND98W3' ,
189
189
platform: ' Linux x86_64' ,
190
190
};
191
- const options = {
191
+ let options = {
192
192
path: ' /jherax' ,
193
- expires: { hours : 6 }
193
+ expires: new Date ( ' 2017/03/06 ' )
194
194
};
195
195
196
196
cookieStore .setItem (' activity' , data, options);
197
- cookieStore .setItem (' valid ' , true , {expires: new Date ( ' 2017/01/02 ' ) });
197
+ cookieStore .setItem (' testing ' , true , { expires: {hours : 1 } });
198
198
```
199
199
200
200
### Getting all items stored
@@ -257,6 +257,27 @@ function clearDataFromStorage() {
257
257
}
258
258
```
259
259
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
+
260
281
### Static Methods
261
282
262
283
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.
268
289
- ` command ` _ ` {string} ` _ Name of the API method to intercept. It can be ` setItem ` , ` getItem ` , ` removeItem ` , ` clear `
269
290
- ` action ` _ ` {function} ` _ Callback executed when the API method is called.
270
291
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.
272
293
273
294
#### Example
274
295
@@ -302,7 +323,7 @@ WebStorage.interceptors('getItem', (key, value) => {
302
323
WebStorage .interceptors (' removeItem' , (key ) => console .log (` removeItem: ${ key} ` ));
303
324
304
325
// 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!' });
306
327
storage .getItem (' storage-test' );
307
328
```
308
329
@@ -326,14 +347,14 @@ console.log('Default:', storageName);
326
347
storage .setItem (' defaultStorage' , storageName);
327
348
328
349
// sets the new default storage mechanism
329
- configStorage .set (' sessionStorage ' );
350
+ configStorage .set (' cookieStorage ' );
330
351
storageName = configStorage .get ();
331
352
console .log (' Current:' , storageName);
332
353
333
354
storage .setItem (' currentStorage' , storageName);
334
355
```
335
356
336
- ## isAvaliable
357
+ ## isAvailable
337
358
338
359
_ @type_ ` Object ` . Determines which storage mechanisms are available. It contains the following flags:
339
360
@@ -348,29 +369,23 @@ _@type_ `Object`. Determines which storage mechanisms are available. It contains
348
369
import storage , * as proxyStorage from ' proxy-storage' ;
349
370
// * imports the entire module's members into proxyStorage.
350
371
351
- console .info (' Available storage mechanisms' );
352
- console .log (proxyStorage .isAvaliable );
372
+ const flags = proxyStorage .isAvailable ;
353
373
354
- function init ( ) {
355
- // memoryStorage is always available
374
+ if ( ! flags . localStorage && ! flags . sessionStorage ) {
375
+ // forces the storage mechanism to memoryStorage
356
376
proxyStorage .configStorage .set (' memoryStorage' );
377
+ }
357
378
358
- if (isSafariInPrivateMode (proxyStorage .isAvaliable )) {
359
- // do something additional...
360
- }
379
+ let data = storage .getItem (' hidden-data' );
361
380
381
+ if (! data) {
362
382
storage .setItem (' hidden-data' , {
363
383
mechanism: ' memoryStorage' ,
364
- availability: ' Current page: you can refresh your window '
384
+ availability: ' Current page: you can refresh the page, data still remain '
365
385
});
366
-
367
- let data = storage .getItem (' hidden-data' );
368
- console .log (' in memoryStorage' , data);
369
386
}
370
387
371
- function isSafariInPrivateMode (flags ) {
372
- return ! flags .localStorage && ! flags .sessionStorage ;
373
- }
388
+ console .log (' in memoryStorage' , data);
374
389
```
375
390
376
391
## Shimming
0 commit comments