Storage.js 3.5 KB

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