Skip to content

Commit

Permalink
Deleted the 'api.initialize' after being invoked
Browse files Browse the repository at this point in the history
  • Loading branch information
jherax committed Apr 24, 2017
1 parent e75f060 commit 6df8575
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 77 deletions.
61 changes: 29 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,44 +117,41 @@ require(['proxy-storage'], function(proxyStorage) {
});
```

See an example with RequireJS here: http://jsfiddle.net/FdKTn/67/
See an example with RequireJS here: http://jsfiddle.net/FdKTn/71/

# API

The exposed interface manages an adapter that stores the data
as **JSON**, allowing to save `Object` and `Array<Any>` values, which
is not the default behavior when using the native `window.localStorage`,
`window.sessionStorage` and `document.cookie` storages. It also provides
a new storage mechanism called **`memoryStorage`** which persists the data
in memory (current browser tab), even if a forced refresh is done
(similar to `sessionStorage`).
The exposed API manages an adapter that stores the data
as **JSON**, allowing to save and retrieve **primitive**,
`Object` and `Array<Any>` values, thanks to `JSON.stringify`.

It also provides a new storage mechanism called **`memoryStorage`**
which persists the data in memory (current tab in the browser), even
if a forced refresh is done (similar to `sessionStorage`).

The [`WebStorage`](#webstorage) class has a static member called
[`interceptors`](#interceptors) which lets you to register callback
functions on the prototype methods `setItem`, `getItem`, `removeItem`,
functions upon the prototype methods `setItem`, `getItem`, `removeItem`,
and `clear`, giving you the ability to intercept and modify the values
to read/write/delete.
to read, write, or delete.

This library is exported as [UMD] _(Universal Module Definition)_ and the API
contains the following members:
This library is exported as [UMD] _(Universal Module Definition)_ and
the API contains the following members:

## storage (or default)

**_@type_ `Object`**

This is the **default** module and is an instance of
[`WebStorage`](#webstorage). It saves and retrieves the data internally as
**JSON**, allowing not only storing **primitive** values but also **Object**
values.

It inherits the following members from the `WebStorage` prototype:
This is the **default** member of the library and is an instance of
[`WebStorage`](#webstorage). It inherits the following members from
the prototype:

- **`setItem`**`(key, value [,options])`: stores a `value` given a `key` name.
<br>The `options` parameter is used only when you set `"cookieStorage"`.
<br>The `options` parameter is used only with instances of `cookieStorage`.
Read more details [here](#handling-cookies).
- **`getItem`**`(key)`: retrieves a value by its `key` name.
- **`removeItem`**`(key [,options])`: deletes an item from the storage.
<br>The `options` parameter is used only when you set `"cookieStorage"`.
<br>The `options` parameter is used only with instances of `cookieStorage`.
Read more details [here](#handling-cookies).
- **`clear`**`()`: removes all items from the storage instance.
- **`length`**: gets the number of items stored in the storage instance.
Expand Down Expand Up @@ -230,21 +227,21 @@ Where **`storageType`** is a `string` that describes the type of storage
to manage. It can be one of the following values: `"localStorage"`,
`"sessionStorage"`, `"cookieStorage"`, or `"memoryStorage"`.

Each instance handles an adapter with the following API:
Each instance inherits the following members:

- **`setItem`**`(key, value [,options])`: stores a `value` given a `key` name.
<br>The `options` parameter is used only when you set `"cookieStorage"`.
<br>The `options` parameter is used only with instances of `cookieStorage`.
Read more details [here](#handling-cookies).
- **`getItem`**`(key)`: retrieves a value by its `key` name.
- **`removeItem`**`(key [,options])`: deletes an item from the storage.
<br>The `options` parameter is used only when you set `"cookieStorage"`.
<br>The `options` parameter is used only with instances of `cookieStorage`.
Read more details [here](#handling-cookies).
- **`clear`**`()`: removes all items from the storage instance.
- **`length`**: gets the number of items stored in the storage instance.

You can create multiple instances of `WebStorage` to handle different storage
mechanisms. To store data in `cookies` and also in `sessionStorage`, you can
do as follow:
You can create multiple instances of `WebStorage` to handle different
storage mechanisms. For example, to store data in `cookies` and also in
`sessionStorage`, you can do as follow:

```javascript
import storage, { WebStorage } from 'proxy-storage';
Expand Down Expand Up @@ -284,7 +281,7 @@ import { WebStorage, isAvailable } from 'proxy-storage';

### Handling cookies

When you create an instance of `WebStorage` for `"cookieStorage"`, the
When you create an instance of `WebStorage` with `cookieStorage`, the
method `setItem()` receives an optional argument as the last parameter,
that configures the way how the cookie is stored.

Expand Down Expand Up @@ -317,7 +314,7 @@ const cookieStore = new WebStorage('cookieStorage');

let data = {
start: new Date().toISOString(),
sessionId: 'J34H5609-SG7BND98W3',
sessionId: 'J34H5609-SG7ND98W3',
platform: 'Linux x86_64',
};

Expand Down Expand Up @@ -373,7 +370,7 @@ cookieStore.removeItem('optimizelyEndUserId', {

You can loop over the items in the storage instance, e.g.
`localStorage`, `sessionStorage`, `cookieStorage`, or `memoryStorage`,
but it is not a good practice, see the notes below.
but it is not a good practice, see the Important note below.

```javascript
const sessionStore = new WebStorage('sessionStorage');
Expand Down Expand Up @@ -428,10 +425,10 @@ function clearAllStorages() {
### Interceptors

The [`WebStorage`](#webstorage) class exposes the static member `interceptors`
which lets you to register callback functions on the prototype methods
which lets you to register callback functions upon the prototype methods
`setItem`, `getItem`, `removeItem`, and `clear`.
It is very useful when you need to take actions accessing the API methods,
giving you the ability to intercept and modify the values to read/write/delete.
It is very useful when you need to perform an action to intercept the value
to read, write, or delete.

- **`WebStorage.interceptors`**`(command, action)`: adds an interceptor to
the API method.
Expand Down
45 changes: 22 additions & 23 deletions dist/proxy-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,9 @@ var WebStorage = function () {
return WebStorage;
}();

// @public API
/**
* @public API
*/


exports.default = WebStorage;
Expand All @@ -526,7 +528,7 @@ var _utils = __webpack_require__(0);
/**
* @private
*
* Proxy for the default cookie storage associated with the current document.
* Proxy for document.cookie
*
* @see
* https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
Expand All @@ -545,14 +547,13 @@ var $cookie = {
/**
* @private
*
* Builds the expiration part for the cookie.
* Builds the expiration for the cookie.
*
* @see utils.alterDate(options)
*
* @param {Date|object} date: the expiration date
* @return {string}
*/
/* eslint-disable no-invalid-this */
function buildExpirationString(date) {
var expires = date instanceof Date ? (0, _utils.alterDate)({ date: date }) : (0, _utils.alterDate)(date);
return expires.toUTCString();
Expand Down Expand Up @@ -589,7 +590,7 @@ function findCookie(cookie) {
/**
* @public
*
* Create, read, and delete elements from document cookies
* Create, read, and delete elements from document.cookie,
* and implements the Web Storage interface.
*
* @return {object}
Expand Down Expand Up @@ -643,18 +644,17 @@ function cookieStorage() {
}
});
},


// this method will be removed after being invoked
// because is not part of the Web Storage interface
initialize: function initialize() {
// copies all existing elements in the storage
$cookie.get().split(';').forEach(function (cookie) {
var index = cookie.indexOf('=');
var key = cookie.substring(0, index).trim();
var value = cookie.substring(index + 1).trim();
// copies all existing elements in the storage
if (key) api[key] = decodeURIComponent(value);
});
// this method is removed after being invoked
// because is not part of the Web Storage interface
delete api.initialize;
}
};
return api;
Expand Down Expand Up @@ -707,9 +707,9 @@ function setStoreToWindow(hashtable) {
/**
* @public
*
* Create, read, and delete elements from memory store and
* implements the Web Storage interface. It also adds a hack
* to persist the store in session for the current browser-tab.
* Create, read, and delete elements from memory, and implements
* the Web Storage interface. It also adds a hack to persist
* the storage in the session for the current tab (browser).
*
* @return {object}
*/
Expand All @@ -734,12 +734,12 @@ function memoryStorage() {
});
setStoreToWindow(hashtable);
},

// this method will be removed after being invoked
// because is not part of the Web Storage interface
initialize: function initialize() {
// copies all existing elements in the storage
Object.assign(api, hashtable);
// this method is removed after being invoked
// because is not part of the Web Storage interface
delete api.initialize;
}
};
return api;
Expand Down Expand Up @@ -772,31 +772,28 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
/**
* @private
*
* Adds the current elements in the storage object.
* Copy the current items in the storage mechanism.
*
* @param {object} api: the storage mechanism to initialize
* @return {object}
*/
function initApi(api) {
if (!api.initialize) return api;
// sets read-only and non-enumerable properties
// sets API members to read-only and non-enumerable
for (var prop in api) {
// eslint-disable-line
if (prop !== 'initialize') {
(0, _utils.setProperty)(api, prop);
}
}
api.initialize();
// this method is removed after being invoked
// because is not part of the Web Storage interface
delete api.initialize;
return api;
}

/**
* @public
*
* Proxy for storage mechanisms.
* Proxy for the storage mechanisms.
* All members implement the Web Storage interface.
*
* @see
Expand Down Expand Up @@ -937,7 +934,9 @@ function init() {

init();

// @public API
/**
* @public API
*/
exports.default = storage;
exports.WebStorage = _webStorage2.default;
exports.configStorage = configStorage;
Expand Down
2 changes: 1 addition & 1 deletion dist/proxy-storage.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/proxy-storage.min.map

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions src/cookie-storage.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-disable no-invalid-this */
import {alterDate, isObject} from './utils';

/**
* @private
*
* Proxy for the default cookie storage associated with the current document.
* Proxy for document.cookie
*
* @see
* https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
Expand All @@ -22,7 +21,7 @@ const $cookie = {
/**
* @private
*
* Builds the expiration part for the cookie.
* Builds the expiration for the cookie.
*
* @see utils.alterDate(options)
*
Expand Down Expand Up @@ -68,13 +67,14 @@ function findCookie(cookie) {
/**
* @public
*
* Create, read, and delete elements from document cookies
* Create, read, and delete elements from document.cookie,
* and implements the Web Storage interface.
*
* @return {object}
*/
export default function cookieStorage() {
const api = {

setItem(key, value, options) {
options = Object.assign({path: '/'}, options);
// keep track of the metadata associated to the cookie
Expand Down Expand Up @@ -125,16 +125,17 @@ export default function cookieStorage() {
});
},

// this method will be removed after being invoked
// because is not part of the Web Storage interface
initialize() {
// copies all existing elements in the storage
$cookie.get().split(';').forEach((cookie) => {
const index = cookie.indexOf('=');
const key = cookie.substring(0, index).trim();
const value = cookie.substring(index + 1).trim();
// copies all existing elements in the storage
if (key) api[key] = decodeURIComponent(value);
});
// this method is removed after being invoked
// because is not part of the Web Storage interface
delete api.initialize;
},
};
return api;
Expand Down
16 changes: 11 additions & 5 deletions src/memory-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,42 @@ function setStoreToWindow(hashtable) {
/**
* @public
*
* Create, read, and delete elements from memory store and
* implements the Web Storage interface. It also adds a hack
* to persist the store in session for the current browser-tab.
* Create, read, and delete elements from memory, and implements
* the Web Storage interface. It also adds a hack to persist
* the storage in the session for the current tab (browser).
*
* @return {object}
*/
export default function memoryStorage() {
const hashtable = getStoreFromWindow();
const api = {

setItem(key, value) {
hashtable[key] = value;
setStoreToWindow(hashtable);
},

getItem(key) {
const value = hashtable[key];
return value === undefined ? null : value;
},

removeItem(key) {
delete hashtable[key];
setStoreToWindow(hashtable);
},

clear() {
Object.keys(hashtable).forEach(key => delete hashtable[key]);
setStoreToWindow(hashtable);
},
// this method will be removed after being invoked
// because is not part of the Web Storage interface

initialize() {
// copies all existing elements in the storage
Object.assign(api, hashtable);
// this method is removed after being invoked
// because is not part of the Web Storage interface
delete api.initialize;
},
};
return api;
Expand Down
Loading

0 comments on commit 6df8575

Please sign in to comment.