@calvear/react-redux
Version:
Preconfigured Redux store initializer with Redux Saga, Redux Logger and Reselect for React SPA applications.
178 lines (152 loc) • 5.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.write = write;
exports.read = read;
exports.memoCall = memoCall;
exports.storageType = void 0;
var _effects = require("redux-saga/effects");
var _marked = /*#__PURE__*/regeneratorRuntime.mark(memoCall);
/**
* Browser storage types.
*/
var storageType = {
LOCAL_STORAGE: 'localStorage',
SESSION_STORAGE: 'sessionStorage',
MEMORY_STORAGE: 'memoryStorage'
}; // initializes memory storage.
exports.storageType = storageType;
window[storageType.MEMORY_STORAGE] = {};
/**
* Writes a value to storage.
*
* @param {string} key item key.
* @param {any} value value for store.
* @param {string} [storageType] storage type. Default is localStorage.
*/
function write(key, value) {
var storageType = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : storageType.LOCAL_STORAGE;
try {
if (storageType.MEMORY_STORAGE) {
window[storageType][key] = value;
return;
}
if (!value) window[storageType].setItem(key, value);else window[storageType].setItem(key, JSON.stringify(value));
} catch (_unused) {
window[storageType].clear();
}
}
/**
* Reads a value from storage.
*
* @param {string} key item key.
* @param {string} [storageType] storage type. Default is localStorage.
*
* @returns {any} stored value.
*/
function read(key) {
var storageType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : storageType.LOCAL_STORAGE;
if (storageType.MEMORY_STORAGE) return window[storageType][key];
var data = window[storageType].getItem(key);
if (!data || data === 'null' || data === 'undefined' || data === '{}' || data === '[]') return null;
return JSON.parse(data);
}
/**
* Calculates the date in
* next days.
*
* @param {number} days next days.
*
* @returns {Date} date in next days from now.
*/
function dateNextDays(days) {
if (!days) return null;
var date = new Date();
date.setDate(date.getDate() + days);
return date;
}
/**
* Persists the result from an generator
* callback, storing it in browser storage.
*
* @param {string} key persisted value accessor.
* @param {IterableIterator<any>} generator generator callback.
* @param {string} [config] memo config.
* @param {string} [config.storageType] storage type.
* @param {number | null} [config.expirationInDays] cache duration days.
* @param {Array} args generator function args.
*
* @yields {any}
* @throws {Error} on non valid key.
*
* @returns {IterableIterator<any>} cached/persisted value or result.
*/
function memoCall(key, generator) {
var _ref,
expirationInDays,
_ref$storageType,
storageType,
_len,
args,
_key,
cache,
data,
_args2 = arguments;
return regeneratorRuntime.wrap(function memoCall$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_ref = _args2.length > 2 && _args2[2] !== undefined ? _args2[2] : {}, expirationInDays = _ref.expirationInDays, _ref$storageType = _ref.storageType, storageType = _ref$storageType === void 0 ? storageType.LOCAL_STORAGE : _ref$storageType;
for (_len = _args2.length, args = new Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) {
args[_key - 3] = _args2[_key];
}
cache = read(key, storageType); // optimistic data refresh.
if (cache !== null && cache !== void 0 && cache.expiration && new Date().getTime() > new Date(cache.expiration).getTime()) {
// delayed data refresh from source.
/*#__PURE__*/
regeneratorRuntime.mark(function _callee() {
var data;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return _effects.call.apply(void 0, [generator].concat(args));
case 2:
data = _context.sent;
write(key, {
expiration: dateNextDays(expirationInDays),
data: data
}, storageType);
case 4:
case "end":
return _context.stop();
}
}
}, _callee);
})();
}
if (!(cache && cache !== 'undefined')) {
_context2.next = 6;
break;
}
return _context2.abrupt("return", cache.data);
case 6:
_context2.next = 8;
return _effects.call.apply(void 0, [generator].concat(args));
case 8:
data = _context2.sent;
write(key, {
expiration: dateNextDays(expirationInDays),
data: data
}, storageType);
return _context2.abrupt("return", data);
case 11:
case "end":
return _context2.stop();
}
}
}, _marked);
}
//# sourceMappingURL=memo.lib.js.map