UNPKG

@mobileaction/ui-modules

Version:

Mobile Action common modules for Vue projects

47 lines (39 loc) 1.6 kB
import MaStorage, { insertReducerProps } from '../libs/MaStorage.js'; import { injectPlugin, validateVueInstall } from '../PluginUtils.js'; const DEFAULT_CACHE_TIMEOUT = 3.6e+6; // an hour in ms export function MaSessionStorage(app, reducers = [], opts) { if (!validateVueInstall(app, MaSessionStorage, 'MaSessionStorage')) { return; } const _opts = Object.assign({ cacheTimeout: DEFAULT_CACHE_TIMEOUT, cachePrefix: 'MA_', keptKeys: [], keptRawKeys: [], windowGlobal: false, }, opts); const APP_VERSION_KEY = 'appVersion'; const APP_VERSION_INIT_TIME_KEY = 'appVersionInitTime'; _opts.keptKeys.push(APP_VERSION_KEY, APP_VERSION_INIT_TIME_KEY); const storageIns = new MaStorage(() => typeof window !== 'undefined' && window.sessionStorage, _opts); // add default app version reducer if not already given pushToReducersIfNotExists(reducers, APP_VERSION_KEY, '0.0.0'); // add app version checker init time pushToReducersIfNotExists(reducers, APP_VERSION_INIT_TIME_KEY, 0); // add reducers from options insertReducerProps(storageIns, reducers); injectPlugin(app, storageIns, '$maSessionStorage'); if (typeof window !== 'undefined' && _opts.windowGlobal) { window.$maSessionStorage = storageIns; } } function pushToReducersIfNotExists(reducers, name, defaultValue) { if (reducers.findIndex(r => r === name || r.name === name) !== -1) { return; } reducers.push({ name, default: defaultValue, }); } export default MaSessionStorage;