UNPKG

redux-localstorage-saver

Version:

Redux middleware for automatical save actions to localStorage with restore by action

91 lines (74 loc) 3.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.restore = restore; exports["default"] = void 0; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } var LOAD_LOCALSTORAGE = Symbol('LOAD_LOCALSTORAGE'); function restore() { return { type: LOAD_LOCALSTORAGE }; } var DEFAULT_CONFIG = { items: [], prefix: 'rls_', serialize: JSON.stringify, deserialize: JSON.parse }; function saver(_config) { var config = DEFAULT_CONFIG; if (Array.isArray(_config)) { config.items = _config; } else if (_typeof(_config) === 'object') { config = _objectSpread({}, DEFAULT_CONFIG, {}, _config); } else { throw 'You need to use saver(config), where config is array or object'; } saver.getItem = function (item) { try { var payload = localStorage.getItem("".concat(config.prefix).concat(item)); return config.deserialize(payload); } catch (error) { return null; } }; saver.loadAll = function (dispatch) { config.items.forEach(function (item) { var payload = saver.getItem(item); if (payload !== null) { dispatch({ type: item, payload: payload }); } }); }; saver.save = function (action) { try { localStorage.setItem("".concat(config.prefix).concat(action.type), config.serialize(action.payload)); } catch (error) { return; } }; return function () { return function (next) { return function (action) { if (action.type === LOAD_LOCALSTORAGE) { saver.loadAll(next); } else { if (config.items.includes(action.type)) { saver.save(action); } next(action); } }; }; }; } var _default = saver; exports["default"] = _default;