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