getStoredState.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = getStoredState;
  4. var _constants = require("./constants");
  5. function getStoredState(config) {
  6. var transforms = config.transforms || [];
  7. var storageKey = "".concat(config.keyPrefix !== undefined ? config.keyPrefix : _constants.KEY_PREFIX).concat(config.key);
  8. var storage = config.storage;
  9. var debug = config.debug;
  10. var deserialize;
  11. if (config.deserialize === false) {
  12. deserialize = function deserialize(x) {
  13. return x;
  14. };
  15. } else if (typeof config.deserialize === 'function') {
  16. deserialize = config.deserialize;
  17. } else {
  18. deserialize = defaultDeserialize;
  19. }
  20. return storage.getItem(storageKey).then(function (serialized) {
  21. if (!serialized) return undefined;else {
  22. try {
  23. var state = {};
  24. var rawState = deserialize(serialized);
  25. Object.keys(rawState).forEach(function (key) {
  26. state[key] = transforms.reduceRight(function (subState, transformer) {
  27. return transformer.out(subState, key, rawState);
  28. }, deserialize(rawState[key]));
  29. });
  30. return state;
  31. } catch (err) {
  32. if (process.env.NODE_ENV !== 'production' && debug) console.log("redux-persist/getStoredState: Error restoring data ".concat(serialized), err);
  33. throw err;
  34. }
  35. }
  36. });
  37. }
  38. function defaultDeserialize(serial) {
  39. return JSON.parse(serial);
  40. }