getStorage.js 1.4 KB

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