getStoredStateMigrateV4.js 6.0 KB

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