box-ui-elements-mlh
Version:
136 lines (119 loc) • 3.06 kB
Flow
/**
* @flow
* @file Local storage wrapper that falls back to an in memory store
* @author Box
*/
import Cache from './Cache';
import type APICache from './Cache';
const KEY_PREFIX = 'localStore';
const SERVICE_VERSION = '0';
class LocalStore {
memoryStore: APICache;
localStorage: typeof localStorage;
isLocalStorageAvailable: boolean;
/**
* [constructor]
*
* @return {void}
*/
constructor() {
this.memoryStore = new Cache();
try {
this.localStorage = window.localStorage;
this.isLocalStorageAvailable = this.canUseLocalStorage();
} catch (e) {
this.isLocalStorageAvailable = false;
}
}
/**
* Builds a key for the session store
* @private
* @param {string} key
*
* @return {string}
*/
buildKey(key: string): string {
return `${KEY_PREFIX}/${SERVICE_VERSION}/${key}`;
}
/**
* Test to see browser can use local storage.
* See http://stackoverflow.com/questions/14555347
* Note that this will return false if we are actually hitting the maximum localStorage
* size (5MB / 2.5M chars)
*
* @private
* @return {boolean} True if browser can use localStore
*/
canUseLocalStorage(): boolean {
if (!this.localStorage) {
return false;
}
try {
this.localStorage.setItem(this.buildKey('TestKey'), 'testValue');
this.localStorage.removeItem(this.buildKey('TestKey'));
return true;
} catch (e) {
return false;
}
}
/**
* Set an item
*
* @param {string} key
* @param {*} value
*
* @return {void}
*/
setItem(key: string, value: any) {
if (this.isLocalStorageAvailable) {
try {
this.localStorage.setItem(this.buildKey(key), JSON.stringify(value));
} catch (e) {
// no-op
}
} else {
this.memoryStore.set(key, value);
}
}
/**
* Get an item
*
* @param {string} key
*
* @return {*}
*/
getItem(key: string): ?Object {
if (this.isLocalStorageAvailable) {
try {
const item = this.localStorage.getItem(this.buildKey(key));
if (!item) {
return null;
}
return JSON.parse(item);
} catch (e) {
return null;
}
} else {
return this.memoryStore.get(key);
}
}
/**
* Remove an item
*
* @param {string} key
*
* @return {void}
*/
removeItem(key: string): void {
if (this.isLocalStorageAvailable) {
try {
this.localStorage.removeItem(this.buildKey(key));
} catch (e) {
// no-op
}
return;
}
this.memoryStore.unset(key);
}
}
export default LocalStore;