getStorage.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 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); }
  2. function noop() {}
  3. var noopStorage = {
  4. getItem: noop,
  5. setItem: noop,
  6. removeItem: noop
  7. };
  8. function hasStorage(storageType) {
  9. if ((typeof self === "undefined" ? "undefined" : _typeof(self)) !== 'object' || !(storageType in self)) {
  10. return false;
  11. }
  12. try {
  13. var storage = self[storageType];
  14. var testKey = "redux-persist ".concat(storageType, " test");
  15. storage.setItem(testKey, 'test');
  16. storage.getItem(testKey);
  17. storage.removeItem(testKey);
  18. } catch (e) {
  19. if (process.env.NODE_ENV !== 'production') console.warn("redux-persist ".concat(storageType, " test failed, persistence will be disabled."));
  20. return false;
  21. }
  22. return true;
  23. }
  24. export default function getStorage(type) {
  25. var storageType = "".concat(type, "Storage");
  26. if (hasStorage(storageType)) return self[storageType];else {
  27. if (process.env.NODE_ENV !== 'production') {
  28. console.error("redux-persist failed to create sync storage. falling back to noop storage.");
  29. }
  30. return noopStorage;
  31. }
  32. }