@mobileaction/ui-modules
Version:
Mobile Action common modules for Vue projects
45 lines (37 loc) • 1.53 kB
JavaScript
/**
* Local Storage holds values that should be persisted over long terms so only
* values that are very stable and that doesn't have problem should be stored.
* By stable: data structure should not change much
*/
import MaStorage, { insertReducerProps } from '../libs/MaStorage.js';
import { injectPlugin, validateVueInstall } from '../PluginUtils.js';
const DEFAULT_CACHE_TIMEOUT = -1; // an hour in ms non positive value means disabled
export function MaLocalStorage(app, reducers = [], opts) {
if (!validateVueInstall(app, MaLocalStorage, 'MaLocalStorage')) {
return;
}
const _opts = Object.assign({
cacheTimeout: DEFAULT_CACHE_TIMEOUT,
cachePrefix: 'MA_',
keptKeys: [],
keptRawKeys: [],
windowGlobal: false,
}, opts);
const APP_VERSION_KEY = 'appVersion';
_opts.keptKeys.push(APP_VERSION_KEY);
const storageIns = new MaStorage(() => typeof window !== 'undefined' && window.localStorage, _opts);
// add default app version reducer if not already given
if (reducers.findIndex(r => r === APP_VERSION_KEY || r.name === APP_VERSION_KEY) === -1) {
reducers.push({
name: APP_VERSION_KEY,
default: '0.0.0',
});
}
// add reducers from options
insertReducerProps(storageIns, reducers);
injectPlugin(app, storageIns, '$maLocalStorage');
if (typeof window !== 'undefined' && _opts.windowGlobal) {
window.$maLocalStorage = storageIns;
}
}
export default MaLocalStorage;