Skip to content

Commit

Permalink
WebStorage instances are singletons by storage mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
jherax committed Nov 17, 2016
1 parent dea90e5 commit 81a6241
Show file tree
Hide file tree
Showing 11 changed files with 579 additions and 109 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# 1.0.1
# 1.0.2

### Improvements

1. `WebStorage` instances are singletons by storage mechanism, in order to keep consistency of the data stored.

### Fixes

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ $ yarn add proxy-storage
`proxy-storage` can be included directly from a CDN in your page:

```html
<!-- last version: 1.0.1 -->
<script src="https://cdn.rawgit.com/jherax/proxy-storage/1.0.1/dist/proxy-storage.min.js"></script>
<!-- last version: 1.0.2 -->
<script src="https://cdn.rawgit.com/jherax/proxy-storage/1.0.2/dist/proxy-storage.min.js"></script>
```

In the above case, the [library](#api) is included into the namespace `proxyStorage` as a global object.
Expand Down
67 changes: 40 additions & 27 deletions dist/proxy-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,27 @@ return /******/ (function(modules) { // webpackBootstrap
// If you want to support all ES6 features, uncomment the next line
// import 'babel-polyfill';

/**
* @public
*
* Current storage mechanism.
* @type {object}
*/
var storage = null;

/**
* @public
*
* Determines which storage mechanisms are available.
*
* @type {object}
*/
var isAvaliable = {
localStorage: false,
sessionStorage: false,
cookieStorage: false,
memoryStorage: true };

/**
* @private
*
Expand All @@ -106,6 +127,15 @@ return /******/ (function(modules) { // webpackBootstrap
}
};

/**
* @private
*
* Keeps WebStorage instances by type as singletons
*
* @type {object}
*/
var _instances = {};

/**
* @private
*
Expand Down Expand Up @@ -222,7 +252,7 @@ return /******/ (function(modules) { // webpackBootstrap
* @Reference
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
*
* @type {Class}
* @type {class}
*/

var WebStorage = function () {
Expand All @@ -241,6 +271,10 @@ return /******/ (function(modules) { // webpackBootstrap
if (!_proxy.hasOwnProperty(storageType)) {
throw new Error('Storage type "' + storageType + '" is not valid');
}
// keeps instances by storageType as singletons
if (_instances[storageType]) {
return _instances[storageType];
}
setProperty(this, '__storage__', storageType);
// copies all existing elements in the storage
Object.keys(_proxy[storageType]).forEach(function (key) {
Expand All @@ -250,7 +284,8 @@ return /******/ (function(modules) { // webpackBootstrap
} catch (e) {
_this[key] = value;
}
});
}, this);
_instances[storageType] = this;
}
/**
* Stores a value given a key name.
Expand Down Expand Up @@ -322,8 +357,8 @@ return /******/ (function(modules) { // webpackBootstrap

executeInterceptors('clear');
Object.keys(this).forEach(function (key) {
return delete _this2[key];
});
delete _this2[key];
}, this);
_proxy[this.__storage__].clear();
}
/**
Expand Down Expand Up @@ -362,33 +397,11 @@ return /******/ (function(modules) { // webpackBootstrap
/**
* @public
*
* Determines which storage mechanisms are available.
*
* Get/Set the storage mechanism to use by default.
* @type {object}
*/


var isAvaliable = {
localStorage: false,
sessionStorage: false,
cookieStorage: false,
memoryStorage: true
};

/**
* @public
*
* Current storage mechanism.
* @type {object}
*/
var storage = null;

/**
* @public
*
* Get/Set the storage mechanism to use by default.
* @type {object}
*/
var configStorage = {
get: function get() {
return storage.__storage__;
Expand Down
2 changes: 1 addition & 1 deletion dist/proxy-storage.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
{
"name": "proxy-storage",
"version": "1.0.2",
"description": "Storage mechanism that implements the Web Storage interface",
"version": "1.0.1",
"author": "David Rivera <jherax@gmail.com>",
"src": "src/proxy-storage.js",
"main": "dist/proxy-storage.js",
"keywords": [
"javascript",
"storage",
"web storage",
"cookie",
"sessionStorage",
"localStorage",
"cookie"
"web storage",
"javascript"
],
"repository": {
"url": "git@github.com:jherax/proxy-storage.git",
Expand All @@ -22,7 +21,7 @@
},
"homepage": "https://github.com/jherax/proxy-storage#readme",
"scripts": {
"build": "webpack",
"build": "webpack --bail && webpack --config webpack.uglify.config.js",
"eslint": "eslint src || true"
},
"engines": {
Expand All @@ -40,7 +39,8 @@
"eslint": "^3.9.0",
"eslint-config-google": "^0.7.0",
"eslint-loader": "^1.6.1",
"webpack": "^1.13.3"
"webpack": "^1.13.3",
"webpack-validator": "^2.2.9"
},
"babel": {
"presets": [
Expand Down
67 changes: 41 additions & 26 deletions src/proxy-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@
// If you want to support all ES6 features, uncomment the next line
// import 'babel-polyfill';

/**
* @public
*
* Current storage mechanism.
* @type {object}
*/
let storage = null;

/**
* @public
*
* Determines which storage mechanisms are available.
*
* @type {object}
*/
const isAvaliable = {
localStorage: false,
sessionStorage: false,
cookieStorage: false,
memoryStorage: true, // fallback storage
};

/**
* @private
*
Expand All @@ -36,6 +58,15 @@ const $cookie = {
},
};

/**
* @private
*
* Keeps WebStorage instances by type as singletons
*
* @type {object}
*/
const _instances = {};

/**
* @private
*
Expand Down Expand Up @@ -146,7 +177,7 @@ function checkEmpty(key) {
* @Reference
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
*
* @type {Class}
* @type {class}
*/
class WebStorage {
/**
Expand All @@ -160,7 +191,10 @@ class WebStorage {
if (!_proxy.hasOwnProperty(storageType)) {
throw new Error(`Storage type "${storageType}" is not valid`);
}
// TODO: make singleton by storageType to access the same stored elements
// keeps instances by storageType as singletons
if (_instances[storageType]) {
return _instances[storageType];
}
setProperty(this, '__storage__', storageType);
// copies all existing elements in the storage
Object.keys(_proxy[storageType]).forEach((key) => {
Expand All @@ -170,7 +204,8 @@ class WebStorage {
} catch (e) {
this[key] = value;
}
});
}, this);
_instances[storageType] = this;
}
/**
* Stores a value given a key name.
Expand Down Expand Up @@ -226,7 +261,9 @@ class WebStorage {
*/
clear() {
executeInterceptors('clear');
Object.keys(this).forEach((key) => delete this[key]);
Object.keys(this).forEach((key) => {
delete this[key];
}, this);
_proxy[this.__storage__].clear();
}
/**
Expand Down Expand Up @@ -254,28 +291,6 @@ class WebStorage {
}
}

/**
* @public
*
* Determines which storage mechanisms are available.
*
* @type {object}
*/
const isAvaliable = {
localStorage: false,
sessionStorage: false,
cookieStorage: false,
memoryStorage: true,
};

/**
* @public
*
* Current storage mechanism.
* @type {object}
*/
let storage = null;

/**
* @public
*
Expand Down
45 changes: 10 additions & 35 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,40 @@

const webpack = require('webpack');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const path = require('path');
const validate = require('webpack-validator');
const PATHS = require('./webpack.constants');

const dir_js = path.resolve(__dirname, 'src');
const file_js = path.resolve(dir_js, 'proxy-storage.js');
const dir_dist = path.resolve(__dirname, 'dist');

// TODO: prevent generate '.min' and '.map' files

module.exports = {
const config = {
// multi-entries: http://ow.ly/PNXR3063UHP
entry: {
'proxy-storage': file_js,
'proxy-storage.min': file_js,
'proxy-storage': PATHS.jsSource,
},
output: {
path: dir_dist,
path: PATHS.dist,
filename: '[name].js',
libraryTarget: 'umd',
library: 'proxyStorage',
},
module: {
loaders: [{
loaders: ['babel-loader', 'eslint-loader'],
test: dir_js,
test: PATHS.source,
}],
},
// https://github.com/MoOx/eslint-loader
eslint: {
configFile: '.eslintrc.json',
failOnError: true,
emitError: false,
quiet: false,
emitError: true,
},
plugins: [
new CleanWebpackPlugin(['dist']),
// Search for equal or similar files and deduplicate them in the output
// https://webpack.github.io/docs/list-of-plugins.html#dedupeplugin
// Note: Don’t use it in watch mode. Only for production builds.
new webpack.optimize.DedupePlugin(),
// http://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin
new webpack.optimize.UglifyJsPlugin({
test: /\.min.js($|\?)/i,
minimize: true,
// https://github.com/mishoo/UglifyJS2#compressor-options
compress: {
dead_code: true,
drop_debugger: true,
drop_console: true,
},
mangle: {
except: ['WebStorage'],
},
}),
// plugins are read from bottom to top
new webpack.SourceMapDevToolPlugin({
filename: '[file].map',
exclude: [
'proxy-storage.js',
],
}),
],

};

module.exports = validate(config);
13 changes: 13 additions & 0 deletions webpack.constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const path = require('path');

const dist = path.resolve(__dirname, 'dist');
const source = path.resolve(__dirname, 'src');
const jsSource = path.resolve(source, 'proxy-storage.js');
const jsDist = path.resolve(dist, 'proxy-storage.js');

module.exports = {
dist,
source,
jsSource,
jsDist,
};
Loading

0 comments on commit 81a6241

Please sign in to comment.