Skip to content

Commit

Permalink
Bump web storage object to 1.0.5 (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
capJavert authored Aug 25, 2019
1 parent 89e1640 commit df2bea4
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/assets/js/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ browser.runtime.onMessageExternal.addListener((request, sender, sendResponse) =>
name: 'SILENCE_MY_BROTHER',
value: {
installed: true,
isActive: settings.isActive || false,
isActive: settings.isActive,
version: manifest.version
}
})
Expand All @@ -134,7 +134,7 @@ browser.runtime.onMessageExternal.addListener((request, sender, sendResponse) =>
toggleClippy()
sendResponse({
name: 'SILENCE_MY_BROTHER',
value: settings.isActive || false
value: settings.isActive
})
break
default:
Expand Down
99 changes: 80 additions & 19 deletions src/assets/js/web-storage-object.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.webStorageObject = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
'use strict';
"use strict";

var WebStorageObject = require('./WebStorageObject');
var WebStorageEnum = require('./WebStorageEnum');

var WebStorageEnum = require('./WebStorageEnum');
/**
* LocalStorageObject
*
Expand All @@ -14,18 +14,20 @@ var WebStorageEnum = require('./WebStorageEnum');
* @param {boolean} overwrite set this flag if you wish to overwrite existing key if it exits inside webStorage
* @return {Proxy} Proxy object containing LocalStorageObject handler
*/


var LocalStorageObject = function LocalStorageObject(target, key, overwrite) {
return new WebStorageObject(WebStorageEnum.localStorage, target, key, overwrite);
};

module.exports = LocalStorageObject;

},{"./WebStorageEnum":3,"./WebStorageObject":4}],2:[function(require,module,exports){
'use strict';
"use strict";

var WebStorageObject = require('./WebStorageObject');
var WebStorageEnum = require('./WebStorageEnum');

var WebStorageEnum = require('./WebStorageEnum');
/**
* SessionStorageObject
*
Expand All @@ -36,14 +38,16 @@ var WebStorageEnum = require('./WebStorageEnum');
* @param {boolean} overwrite set this flag if you wish to overwrite existing key if it exits inside webStorage
* @return {Proxy} Proxy object containing SessionStorageObject handler
*/


var SessionStorageObject = function SessionStorageObject(target, key, overwrite) {
return new WebStorageObject(WebStorageEnum.sessionStorage, target, key, overwrite);
};

module.exports = SessionStorageObject;

},{"./WebStorageEnum":3,"./WebStorageObject":4}],3:[function(require,module,exports){
'use strict';
"use strict";

/**
* WebStorageEnum
Expand All @@ -54,16 +58,14 @@ var WebStorageEnum = Object.freeze({
localStorage: 'localStorage',
sessionStorage: 'sessionStorage'
});

module.exports = WebStorageEnum;

},{}],4:[function(require,module,exports){
'use strict';
"use strict";

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

var WebStorageEnum = require('./WebStorageEnum');

/**
* Abstract WebStorageObject type
* Used in child LocalStorageObject and SessionStorageObject types
Expand All @@ -74,20 +76,25 @@ var WebStorageEnum = require('./WebStorageEnum');
* @param {boolean} overwrite Defaults to true, unset this flag to keep existing data if the key already exsits inside webStorage
* @return {Proxy} Proxy object containing WebStorageObject handler
*/


var WebStorageObject = function WebStorageObject(type, target, key, overwrite) {
if (overwrite == null) {
overwrite = true;
}

var handler = this._handler(key);

if (!handler._setStorage(type)) {
throw "WebStorage type is not valid or supported.";
}

if (overwrite === true || handler._fetch() === null) {
handler._persist(target);
} else {
target = handler._fetch();
}

var proxy = new Proxy(target, handler);
handler._proxy = proxy;

Expand All @@ -99,6 +106,7 @@ var WebStorageObject = function WebStorageObject(type, target, key, overwrite) {

return proxy;
};

WebStorageObject.prototype._handler = function (key) {
return {
/**
Expand All @@ -107,19 +115,22 @@ WebStorageObject.prototype._handler = function (key) {
* @type {string}
*/
_id: key || this._uuid(),

/**
* Reference to handlers Proxy object
* Always set if WebStorageObject is created through constructor function
*
* @type {Proxy}
*/
_proxy: null,

/**
* Reference to selected WebStorage type
*
* @type {localStorage|sessionStorage}
*/
_storage: null,

/**
* Getter for binded webStorage object properties
*
Expand All @@ -129,26 +140,46 @@ WebStorageObject.prototype._handler = function (key) {
*/
get: function get(target, key) {
target = this._fetch();

if (_typeof(target[key]) === 'object') {
return new WebStorageProperty(target[key], key, this._proxy);
} else {
return target[key] || null;
return target.hasOwnProperty(key) ? target[key] : null;
}
},

/**
* Setter for binded webStorage object properties
*
* @param {object} target
* @param {string|number} key
* @return {any}
* @return {boolean}
*/
set: function set(target, key, value) {
target[key] = value;

var temp = this._fetch();
temp[key] = value;

temp[key] = value;
return this._persist(temp);
},

/**
* Delete operator handler
*
* @param {object} target
* @param {string|number} key
* @return {boolean}
*/
deleteProperty: function deleteProperty(target, key) {
if (key in target) {
delete target[key];
return this._persist(target);
} else {
return false;
}
},

/**
* Used to generate random object identifier inside webStorage
*
Expand All @@ -161,6 +192,7 @@ WebStorageObject.prototype._handler = function (key) {
return v.toString(16);
});
},

/**
* Save data to webStorage as JSON string
*
Expand All @@ -169,20 +201,24 @@ WebStorageObject.prototype._handler = function (key) {
_persist: function _persist(value) {
if (value) {
this._storage.setItem(this._id, JSON.stringify(value));

return true;
} else {
return false;
}
},

/**
* Get data from webStorage as object
*
* @return {object}
*/
_fetch: function _fetch() {
var temp = this._storage.getItem(this._id);

return temp ? JSON.parse(temp) : null;
},

/**
* Abstract webStorage setter
*
Expand All @@ -194,25 +230,27 @@ WebStorageObject.prototype._handler = function (key) {
case WebStorageEnum.localStorage:
this._storage = localStorage;
return true;

case WebStorageEnum.sessionStorage:
this._storage = sessionStorage;
return true;
}

return false;
},

/**
* Reflect all values from WebStorage to proxy internal target object
*/
_reflect: function _reflect() {
var temp = this._fetch();

for (var key in temp) {
this._proxy[key] = temp[key];
}
}
};
};

/**
* Constructor for creating an object of WebStorageProperty type
*
Expand All @@ -221,13 +259,16 @@ WebStorageObject.prototype._handler = function (key) {
* @param {Proxy} parent Proxy object of a parent object
* @return {Proxy} Proxy object containing WebStorageProperty handler
*/


var WebStorageProperty = function WebStorageProperty(target, key, parent) {
var handler = this._handler(key, parent);

var proxy = new Proxy(target, handler);
handler._proxy = proxy;

return proxy;
};

WebStorageProperty.prototype._handler = function (key, parent) {
return {
/**
Expand All @@ -236,19 +277,22 @@ WebStorageProperty.prototype._handler = function (key, parent) {
* @type {string}
*/
_id: key,

/**
* Reference to handlers Proxy object
* Always set if WebStorageProperty is created through constructor function
*
* @type {Proxy}
*/
_proxy: null,

/**
* Original Proxy that traps this property
*
* @type {object}
*/
_parent: parent,

/**
* Getter for binded webStorage property properties
*
Expand All @@ -260,29 +304,46 @@ WebStorageProperty.prototype._handler = function (key, parent) {
if (_typeof(target[key]) === 'object') {
return new WebStorageProperty(target[key], key, this._proxy);
} else {
return target[key] || null;
return target.hasOwnProperty(key) ? target[key] : null;
}
},

/**
* Setter for binded webStorage property properties
*
* @param {object} target
* @param {string|number} key
* @return {any}
* @return {boolean}
*/
set: function set(target, key, value) {
target[key] = value;
this._parent[this._id] = target;

return true;
},

/**
* Delete operator handler
*
* @param {object} target
* @param {string|number} key
* @return {boolean}
*/
deleteProperty: function deleteProperty(target, key) {
if (key in target) {
delete target[key];
this._parent[this._id] = target;
return true;
} else {
return false;
}
}
};
};

module.exports = WebStorageObject;

},{"./WebStorageEnum":3}],5:[function(require,module,exports){
'use strict';
"use strict";

/**
* API providing 2 way binding of JavaScript objects to browser WebStorage
Expand All @@ -293,8 +354,8 @@ module.exports = WebStorageObject;
* Each is used to bind any JavaScript object to specific WebStorage type
*
*/

var LocalStorageObject = require('./LocalStorageObject');

var SessionStorageObject = require('./SessionStorageObject');

module.exports = {
Expand Down

0 comments on commit df2bea4

Please sign in to comment.