mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-03 02:00:43 +00:00
Read from sessionStorage before localStorage when loading view state
This commit is contained in:
@@ -2,30 +2,50 @@ import debug from 'debug';
|
||||
|
||||
const log = debug('scope:storage-utils');
|
||||
|
||||
// localStorage detection
|
||||
const storage = (typeof Storage) !== 'undefined' ? window.localStorage : null;
|
||||
export const localSessionStorage = {
|
||||
getItem(k) {
|
||||
return window.sessionStorage.getItem(k) || window.localStorage.getItem(k);
|
||||
},
|
||||
setItem(k, v) {
|
||||
window.sessionStorage.setItem(k, v);
|
||||
window.localStorage.setItem(k, v);
|
||||
},
|
||||
};
|
||||
|
||||
export function storageGet(key, defaultValue) {
|
||||
if (storage && storage.getItem(key) !== undefined) {
|
||||
return storage.getItem(key);
|
||||
export function storageGet(key, defaultValue, storage = localSessionStorage) {
|
||||
if (!storage) {
|
||||
return defaultValue;
|
||||
}
|
||||
return defaultValue;
|
||||
|
||||
const value = storage.getItem(key);
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
export function storageSet(key, value) {
|
||||
export function storageSet(key, value, storage = localSessionStorage) {
|
||||
if (storage) {
|
||||
try {
|
||||
storage.setItem(key, value);
|
||||
return true;
|
||||
} catch (e) {
|
||||
log('Error storing value in storage. Maybe full? Could not store key.', key);
|
||||
log(
|
||||
'Error storing value in storage. Maybe full? Could not store key.',
|
||||
key
|
||||
);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function storageGetObject(key, defaultValue) {
|
||||
const value = storageGet(key);
|
||||
export function storageGetObject(
|
||||
key,
|
||||
defaultValue,
|
||||
storage = localSessionStorage
|
||||
) {
|
||||
const value = storageGet(key, undefined, storage);
|
||||
if (value) {
|
||||
try {
|
||||
return JSON.parse(value);
|
||||
@@ -36,9 +56,9 @@ export function storageGetObject(key, defaultValue) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
export function storageSetObject(key, obj) {
|
||||
export function storageSetObject(key, obj, storage = localSessionStorage) {
|
||||
try {
|
||||
return storageSet(key, JSON.stringify(obj));
|
||||
return storageSet(key, JSON.stringify(obj), storage);
|
||||
} catch (e) {
|
||||
log('Error encoding object for key', key);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user