feat(storage-manager): location hierarchy
This commit is contained in:
parent
0798d6870e
commit
80ff4ac2a7
@ -1,90 +1,180 @@
|
|||||||
const LIFETIME = {
|
export const LOCATION = {
|
||||||
INFINITE: 'infinite',
|
LOCAL: 'local',
|
||||||
|
WINDOW: 'window',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const LOCATION_SHADOWING = [ LOCATION.WINDOW, LOCATION.LOCAL ];
|
||||||
|
|
||||||
export class StorageManager {
|
export class StorageManager {
|
||||||
|
|
||||||
namespace;
|
namespace;
|
||||||
|
_options;
|
||||||
|
|
||||||
constructor(namespace) {
|
constructor(namespace, options) {
|
||||||
if (!namespace) {
|
if (!namespace) {
|
||||||
throw new Error('Cannot setup StorageManager without namespace');
|
throw new Error('Cannot setup StorageManager without namespace');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.namespace = namespace;
|
this.namespace = namespace;
|
||||||
|
|
||||||
|
if (options !== undefined) {
|
||||||
|
this._options = options;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
save(key, value, options) {
|
save(key, value, options=this._options) {
|
||||||
if (!key) {
|
if (!key) {
|
||||||
throw new Error('StorageManager.save called with invalid key');
|
throw new Error('StorageManager.save called with invalid key');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options && options.lifetime !== undefined && !Object.values(LIFETIME).includes(options.lifetime)) {
|
if (options && options.location !== undefined && !Object.values(LOCATION).includes(options.location)) {
|
||||||
throw new Error('StorageManager.save called with unsupported lifetime option');
|
throw new Error('StorageManager.save called with unsupported location option');
|
||||||
}
|
}
|
||||||
|
|
||||||
const lifetime = options && options.lifetime !== undefined ? options.lifetime : LIFETIME.INFINITE;
|
const location = options && options.location !== undefined ? options.location : LOCATION_SHADOWING[0];
|
||||||
|
|
||||||
switch (lifetime) {
|
switch (location) {
|
||||||
case LIFETIME.INFINITE: {
|
case LOCATION.LOCAL: {
|
||||||
this.saveToLocalStorage({ ...this.getFromLocalStorage(), [key]: value });
|
this._saveToLocalStorage({ ...this._getFromLocalStorage(), [key]: value });
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case LOCATION.WINDOW: {
|
||||||
|
this._saveToWindow({ ...this._getFromLocalStorage(), [key]: value });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
console.error('StorageManager.save cannot save item with unsupported lifetime');
|
console.error('StorageManager.save cannot save item with unsupported location');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
load(key, options) {
|
load(key, options=this._options) {
|
||||||
if (options && options.lifetime !== undefined && !Object.values(LIFETIME).includes(options.lifetime)) {
|
if (options && options.location !== undefined && !Object.values(LOCATION).includes(options.location)) {
|
||||||
throw new Error('StorageManager.load called with unsupported lifetime option');
|
throw new Error('StorageManager.load called with unsupported location option');
|
||||||
}
|
}
|
||||||
|
|
||||||
const lifetime = options && options.lifetime !== undefined ? options.lifetime : LIFETIME.INFINITE;
|
let locations = options && options.location !== undefined ? [options.location] : LOCATION_SHADOWING;
|
||||||
|
|
||||||
switch (lifetime) {
|
while (locations.length > 0) {
|
||||||
case LIFETIME.INFINITE:
|
const location = locations.shift();
|
||||||
return this.getFromLocalStorage()[key];
|
let val;
|
||||||
default:
|
|
||||||
console.error('StorageManager.load cannot load item with unsupported lifetime');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
remove(key, options) {
|
|
||||||
if (options && options.lifetime !== undefined && !Object.values(LIFETIME).includes(options.lifetime)) {
|
|
||||||
throw new Error('StorageManager.load called with unsupported lifetime option');
|
|
||||||
}
|
|
||||||
|
|
||||||
const lifetime = options && options.lifetime !== undefined ? options.lifetime : LIFETIME.INFINITE;
|
|
||||||
|
|
||||||
switch (lifetime) {
|
|
||||||
case LIFETIME.INFINITE: {
|
|
||||||
var val = this.getFromLocalStorage();
|
|
||||||
|
|
||||||
delete val[key];
|
|
||||||
|
|
||||||
return this.saveToLocalStorage(val);
|
switch (location) {
|
||||||
|
case LOCATION.LOCAL: {
|
||||||
|
val = this._getFromLocalStorage()[key];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case LOCATION.WINDOW: {
|
||||||
|
val = this._getFromWindow()[key];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
console.error('StorageManager.load cannot load item with unsupported location');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (val !== undefined || locations.length === 0) {
|
||||||
|
return val;
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
console.error('StorageManager.load cannot load item with unsupported lifetime');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getFromLocalStorage() {
|
remove(key, options=this._options) {
|
||||||
|
if (options && options.location !== undefined && !Object.values(LOCATION).includes(options.location)) {
|
||||||
|
throw new Error('StorageManager.load called with unsupported location option');
|
||||||
|
}
|
||||||
|
|
||||||
|
const locations = options && options.location !== undefined ? [options.location] : LOCATION_SHADOWING;
|
||||||
|
|
||||||
|
for (const location of locations) {
|
||||||
|
switch (location) {
|
||||||
|
case LOCATION.LOCAL: {
|
||||||
|
let val = this._getFromLocalStorage();
|
||||||
|
|
||||||
|
delete val[key];
|
||||||
|
|
||||||
|
return this._saveToLocalStorage(val);
|
||||||
|
}
|
||||||
|
case LOCATION.WINDOW: {
|
||||||
|
let val = this._getFromWindow();
|
||||||
|
|
||||||
|
delete val[key];
|
||||||
|
|
||||||
|
return this._saveToWindow(val);
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
console.error('StorageManager.load cannot load item with unsupported location');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
clear(options) {
|
||||||
|
if (options && options.location !== undefined && !Object.values(LOCATION).includes(options.location)) {
|
||||||
|
throw new Error('StorageManager.clear called with unsupported location option');
|
||||||
|
}
|
||||||
|
|
||||||
|
const locations = options && options.location !== undefined ? [options.location] : LOCATION_SHADOWING;
|
||||||
|
|
||||||
|
for (const location of locations) {
|
||||||
|
switch (location) {
|
||||||
|
case LOCATION.LOCAL:
|
||||||
|
return this._clearLocalStorage();
|
||||||
|
case LOCATION.WINDOW:
|
||||||
|
return this._clearWindow();
|
||||||
|
default:
|
||||||
|
console.error('StorageManager.clear cannot clear with unsupported location');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
_getFromLocalStorage() {
|
||||||
const state = JSON.parse(window.localStorage.getItem(this.namespace));
|
const state = JSON.parse(window.localStorage.getItem(this.namespace));
|
||||||
if (state === null) {
|
if (state === null) {
|
||||||
// remove item from localStorage if it stores an invalid value (cannot be parsed)
|
// remove item from localStorage if it stores an invalid value (cannot be parsed)
|
||||||
this.clearLocalStorage();
|
this._clearLocalStorage();
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
saveToLocalStorage(value) {
|
_saveToLocalStorage(value) {
|
||||||
window.localStorage.setItem(this.namespace, JSON.stringify(value));
|
window.localStorage.setItem(this.namespace, JSON.stringify(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
clearLocalStorage() {
|
_clearLocalStorage() {
|
||||||
window.localStorage.removeItem(this.namespace);
|
window.localStorage.removeItem(this.namespace);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
_getFromWindow() {
|
||||||
|
if (!window || !window.App)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
if (!window.App.Storage)
|
||||||
|
window.App.Storage = {};
|
||||||
|
|
||||||
|
if (!window.App.Storage[this.namespace])
|
||||||
|
return {};
|
||||||
|
|
||||||
|
return window.App.Storage[this.namespace];
|
||||||
|
}
|
||||||
|
|
||||||
|
_saveToWindow(value) {
|
||||||
|
if (!window || !window.App) {
|
||||||
|
throw new Error('StorageManager._saveToWindow called when window.App is not available');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!window.App.Storage)
|
||||||
|
window.App.Storage = {};
|
||||||
|
|
||||||
|
window.App.Storage[this.namespace] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
_clearWindow() {
|
||||||
|
if (!window || !window.App) {
|
||||||
|
throw new Error('StorageManager._saveToWindow called when window.App is not available');
|
||||||
|
}
|
||||||
|
|
||||||
|
delete window.App.Storage[this.namespace];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { Utility } from '../../core/utility';
|
import { Utility } from '../../core/utility';
|
||||||
import { StorageManager } from '../../lib/storage-manager/storage-manager';
|
import { StorageManager, LOCATION } from '../../lib/storage-manager/storage-manager';
|
||||||
import './hide-columns.scss';
|
import './hide-columns.scss';
|
||||||
|
|
||||||
const HIDE_COLUMNS_CONTAINER_IDENT = 'uw-hide-columns';
|
const HIDE_COLUMNS_CONTAINER_IDENT = 'uw-hide-columns';
|
||||||
@ -22,7 +22,7 @@ const CELL_ORIGINAL_COLSPAN = 'uw-hide-column-original-colspan';
|
|||||||
})
|
})
|
||||||
export class HideColumns {
|
export class HideColumns {
|
||||||
|
|
||||||
_storageManager = new StorageManager('HIDE_COLUMNS');
|
_storageManager = new StorageManager('HIDE_COLUMNS', { location: LOCATION.LOCAL });
|
||||||
|
|
||||||
_element;
|
_element;
|
||||||
_elementWrapper;
|
_elementWrapper;
|
||||||
|
|||||||
Reference in New Issue
Block a user