Storage.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  7. var Storage = {
  8. async: function () {
  9. var controller = _CoreManager.default.getStorageController();
  10. return !!controller.async;
  11. },
  12. getItem: function (path) {
  13. var controller = _CoreManager.default.getStorageController();
  14. if (controller.async === 1) {
  15. throw new Error('Synchronous storage is not supported by the current storage controller');
  16. }
  17. return controller.getItem(path);
  18. },
  19. getItemAsync: function (path) {
  20. var controller = _CoreManager.default.getStorageController();
  21. if (controller.async === 1) {
  22. return controller.getItemAsync(path);
  23. }
  24. return Promise.resolve(controller.getItem(path));
  25. },
  26. setItem: function (path, value) {
  27. var controller = _CoreManager.default.getStorageController();
  28. if (controller.async === 1) {
  29. throw new Error('Synchronous storage is not supported by the current storage controller');
  30. }
  31. return controller.setItem(path, value);
  32. },
  33. setItemAsync: function (path, value) {
  34. var controller = _CoreManager.default.getStorageController();
  35. if (controller.async === 1) {
  36. return controller.setItemAsync(path, value);
  37. }
  38. return Promise.resolve(controller.setItem(path, value));
  39. },
  40. removeItem: function (path) {
  41. var controller = _CoreManager.default.getStorageController();
  42. if (controller.async === 1) {
  43. throw new Error('Synchronous storage is not supported by the current storage controller');
  44. }
  45. return controller.removeItem(path);
  46. },
  47. removeItemAsync: function (path) {
  48. var controller = _CoreManager.default.getStorageController();
  49. if (controller.async === 1) {
  50. return controller.removeItemAsync(path);
  51. }
  52. return Promise.resolve(controller.removeItem(path));
  53. },
  54. getAllKeys: function () {
  55. var controller = _CoreManager.default.getStorageController();
  56. if (controller.async === 1) {
  57. throw new Error('Synchronous storage is not supported by the current storage controller');
  58. }
  59. return controller.getAllKeys();
  60. },
  61. getAllKeysAsync: function () {
  62. var controller = _CoreManager.default.getStorageController();
  63. if (controller.async === 1) {
  64. return controller.getAllKeysAsync();
  65. }
  66. return Promise.resolve(controller.getAllKeys());
  67. },
  68. generatePath: function (path) {
  69. if (!_CoreManager.default.get('APPLICATION_ID')) {
  70. throw new Error('You need to call Parse.initialize before using Parse.');
  71. }
  72. if (typeof path !== 'string') {
  73. throw new Error('Tried to get a Storage path that was not a String.');
  74. }
  75. if (path[0] === '/') {
  76. path = path.substr(1);
  77. }
  78. return 'Parse/' + _CoreManager.default.get('APPLICATION_ID') + '/' + path;
  79. },
  80. _clear: function () {
  81. var controller = _CoreManager.default.getStorageController();
  82. if (controller.hasOwnProperty('clear')) {
  83. controller.clear();
  84. }
  85. }
  86. };
  87. module.exports = Storage;
  88. var _default = exports.default = Storage;