Storage.js 3.1 KB

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