Storage.js 3.3 KB

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