mirror of
https://github.com/ascribe/onion.git
synced 2024-12-22 09:23:13 +01:00
Revert "Add first cut on persistent stores"
This reverts commit bed067f9bc
.
Conflicts:
js/actions/user_actions.js
js/stores/session_persistent_store.js
js/stores/user_store.js
js/utils/feature_detection_utils.js
This commit is contained in:
parent
11f3ab51a3
commit
ff4067e637
@ -1,98 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
import { sanitize } from '../utils/general_utils';
|
||||
|
||||
/**
|
||||
* A tiny wrapper around HTML5's `webStorage`,
|
||||
* to enable saving JSON objects directly into `webStorage`
|
||||
*/
|
||||
export default class AscribeStorage {
|
||||
/**
|
||||
* @param {String} `name` A unique storage name
|
||||
*/
|
||||
constructor(type, name) {
|
||||
if(type === 'localStorage' || type === 'sessionStorage') {
|
||||
this.storage = window[type];
|
||||
} else {
|
||||
throw new Error('"type" can only be either "localStorage" or "sessionStorage"');
|
||||
}
|
||||
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Private method, do not use from the outside.
|
||||
* Constructs a unique identifier for a item in the global `webStorage`,
|
||||
* by appending the `ÀscribeStorage`'s name
|
||||
* @param {string} key A unique identifier
|
||||
* @return {string} A globally unique identifier for a value in `webStorage`
|
||||
*/
|
||||
_constructStorageKey(key) {
|
||||
return `${this.name}-${key}`;
|
||||
}
|
||||
|
||||
_deconstructStorageKey(key) {
|
||||
return key.replace(`${this.name}-`, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a JSON-serializeble object or a string into `webStorage`
|
||||
* @param {string} key Used as a unique identifier
|
||||
* @param {oneOfType([String, object])} value Either JSON-serializeble or a string
|
||||
*/
|
||||
setItem(key, value) {
|
||||
// We're "try-catching" errors in this method ourselves, to be able to
|
||||
// yield more readable error messages
|
||||
|
||||
if(!key || !value) {
|
||||
throw new Error('"key" or "value" cannot be "falsy" values');
|
||||
} else if(typeof value === 'string') {
|
||||
// since `value` is a string, we can directly write
|
||||
// it into `this.storage`
|
||||
this.storage.setItem(this._constructStorageKey(key), value);
|
||||
} else {
|
||||
// if `value` is not a string, we need to JSON-serialize it and then
|
||||
// put it into `this.storage`
|
||||
|
||||
let serializedValue;
|
||||
try {
|
||||
serializedValue = JSON.stringify(value);
|
||||
} catch(err) {
|
||||
throw new Error('You didn\'t pass valid JSON as "value" into setItem.');
|
||||
}
|
||||
|
||||
try {
|
||||
this.storage.setItem(this._constructStorageKey(key), serializedValue);
|
||||
} catch(err) {
|
||||
throw new Error('Failure saving a "serializedValue" in setItem');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getItem(key) {
|
||||
let deserializedValue;
|
||||
const serializedValue = this.storage.getItem(this._constructStorageKey(key));
|
||||
|
||||
try {
|
||||
deserializedValue = JSON.parse(serializedValue);
|
||||
} catch(err) {
|
||||
deserializedValue = serializedValue;
|
||||
}
|
||||
|
||||
return deserializedValue;
|
||||
}
|
||||
|
||||
toObject() {
|
||||
let obj = {};
|
||||
const storageCopy = JSON.parse(JSON.stringify(this.storage));
|
||||
const sanitizedStore = sanitize(storageCopy, s => !s.match(`${this.name}-`), true);
|
||||
|
||||
Object
|
||||
.keys(sanitizedStore)
|
||||
.forEach((key) => {
|
||||
obj[this._deconstructStorageKey(key)] = JSON.parse(sanitizedStore[key]);
|
||||
});
|
||||
|
||||
return obj;
|
||||
}
|
||||
}
|
@ -5,9 +5,6 @@ import UserActions from '../actions/user_actions';
|
||||
|
||||
import UserSource from '../sources/user_source';
|
||||
|
||||
// import AscribeStorage from '../models/ascribe_storage';
|
||||
// import { sessionStorageAvailable } from '../utils/feature_detection_utils';
|
||||
|
||||
|
||||
class UserStore {
|
||||
constructor() {
|
||||
|
@ -6,12 +6,9 @@
|
||||
* tagged as false by the passed in filter function
|
||||
*
|
||||
* @param {object} obj regular javascript object
|
||||
* @param {function} filterFn a filter function for filtering either by key or value
|
||||
* @param {bool} filterByKey a boolean for choosing whether the object should be filtered by
|
||||
* key or value
|
||||
* @return {object} regular javascript object without null values or empty strings
|
||||
*/
|
||||
export function sanitize(obj, filterFn, filterByKey) {
|
||||
export function sanitize(obj, filterFn) {
|
||||
if(!filterFn) {
|
||||
// By matching null with a double equal, we can match undefined and null
|
||||
// http://stackoverflow.com/a/15992131
|
||||
@ -21,9 +18,7 @@ export function sanitize(obj, filterFn, filterByKey) {
|
||||
Object
|
||||
.keys(obj)
|
||||
.map((key) => {
|
||||
const filterCondition = filterByKey ? filterFn(key) : filterFn(obj[key]);
|
||||
|
||||
if(filterCondition) {
|
||||
if(filterFn(obj[key])) {
|
||||
delete obj[key];
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user