getStoredStateMigrateV4.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = getStoredState;
  4. var _getStoredState = _interopRequireDefault(require("../getStoredState"));
  5. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  6. 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; }
  7. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  8. 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; }
  9. function _typeof(obj) { 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); }
  10. function getStoredState(v4Config) {
  11. return function (v5Config) {
  12. return (0, _getStoredState.default)(v5Config).then(function (state) {
  13. if (state) return state;else return getStoredStateV4(v4Config);
  14. });
  15. };
  16. }
  17. var KEY_PREFIX = 'reduxPersist:';
  18. function hasLocalStorage() {
  19. if ((typeof self === "undefined" ? "undefined" : _typeof(self)) !== 'object' || !('localStorage' in self)) {
  20. return false;
  21. }
  22. try {
  23. var storage = self.localStorage;
  24. var testKey = "redux-persist localStorage test";
  25. storage.setItem(testKey, 'test');
  26. storage.getItem(testKey);
  27. storage.removeItem(testKey);
  28. } catch (e) {
  29. if (process.env.NODE_ENV !== 'production') console.warn("redux-persist localStorage test failed, persistence will be disabled.");
  30. return false;
  31. }
  32. return true;
  33. }
  34. var noop = function noop() {
  35. /* noop */
  36. return null;
  37. };
  38. var noStorage = {
  39. getItem: noop,
  40. setItem: noop,
  41. removeItem: noop,
  42. getAllKeys: noop
  43. };
  44. var createAsyncLocalStorage = function createAsyncLocalStorage() {
  45. if (!hasLocalStorage()) return noStorage;
  46. var localStorage = self.localStorage;
  47. return {
  48. getAllKeys: function getAllKeys(cb) {
  49. try {
  50. var keys = [];
  51. for (var i = 0; i < localStorage.length; i++) {
  52. keys.push(localStorage.key(i));
  53. }
  54. cb(null, keys);
  55. } catch (e) {
  56. cb(e);
  57. }
  58. },
  59. getItem: function getItem(key, cb) {
  60. try {
  61. var s = localStorage.getItem(key);
  62. cb(null, s);
  63. } catch (e) {
  64. cb(e);
  65. }
  66. },
  67. setItem: function setItem(key, string, cb) {
  68. try {
  69. localStorage.setItem(key, string);
  70. cb(null);
  71. } catch (e) {
  72. cb(e);
  73. }
  74. },
  75. removeItem: function removeItem(key, cb) {
  76. try {
  77. localStorage.removeItem(key);
  78. cb && cb(null);
  79. } catch (e) {
  80. cb(e);
  81. }
  82. }
  83. };
  84. };
  85. function getStoredStateV4(v4Config) {
  86. return new Promise(function (resolve, reject) {
  87. var storage = v4Config.storage || createAsyncLocalStorage();
  88. var deserializer = v4Config.serialize === false ? function (data) {
  89. return data;
  90. } : function (serial) {
  91. return JSON.parse(serial);
  92. };
  93. var blacklist = v4Config.blacklist || [];
  94. var whitelist = v4Config.whitelist || false;
  95. var transforms = v4Config.transforms || [];
  96. var keyPrefix = v4Config.keyPrefix !== undefined ? v4Config.keyPrefix : KEY_PREFIX; // fallback getAllKeys to `keys` if present (LocalForage compatability)
  97. if (storage.keys && !storage.getAllKeys) storage = _objectSpread({}, storage, {
  98. getAllKeys: storage.keys
  99. });
  100. var restoredState = {};
  101. var completionCount = 0;
  102. storage.getAllKeys(function (err) {
  103. var allKeys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  104. if (err) {
  105. if (process.env.NODE_ENV !== 'production') console.warn('redux-persist/getStoredState: Error in storage.getAllKeys');
  106. return reject(err);
  107. }
  108. var persistKeys = allKeys.filter(function (key) {
  109. return key.indexOf(keyPrefix) === 0;
  110. }).map(function (key) {
  111. return key.slice(keyPrefix.length);
  112. });
  113. var keysToRestore = persistKeys.filter(passWhitelistBlacklist);
  114. var restoreCount = keysToRestore.length;
  115. if (restoreCount === 0) resolve(undefined);
  116. keysToRestore.forEach(function (key) {
  117. storage.getItem(createStorageKey(key), function (err, serialized) {
  118. if (err && process.env.NODE_ENV !== 'production') console.warn('redux-persist/getStoredState: Error restoring data for key:', key, err);else restoredState[key] = rehydrate(key, serialized);
  119. completionCount += 1;
  120. if (completionCount === restoreCount) resolve(restoredState);
  121. });
  122. });
  123. });
  124. function rehydrate(key, serialized) {
  125. var state = null;
  126. try {
  127. var data = serialized ? deserializer(serialized) : undefined;
  128. state = transforms.reduceRight(function (subState, transformer) {
  129. return transformer.out(subState, key, {});
  130. }, data);
  131. } catch (err) {
  132. if (process.env.NODE_ENV !== 'production') console.warn('redux-persist/getStoredState: Error restoring data for key:', key, err);
  133. }
  134. return state;
  135. }
  136. function passWhitelistBlacklist(key) {
  137. if (whitelist && whitelist.indexOf(key) === -1) return false;
  138. if (blacklist.indexOf(key) !== -1) return false;
  139. return true;
  140. }
  141. function createStorageKey(key) {
  142. return "".concat(keyPrefix).concat(key);
  143. }
  144. });
  145. }
  146. function defaultDeserializer(serial) {
  147. return JSON.parse(serial);
  148. }