Storage.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  4. /**
  5. * Copyright (c) 2015-present, Parse, LLC.
  6. * All rights reserved.
  7. *
  8. * This source code is licensed under the BSD-style license found in the
  9. * LICENSE file in the root directory of this source tree. An additional grant
  10. * of patent rights can be found in the PATENTS file in the same directory.
  11. *
  12. * @flow
  13. */
  14. var Storage = {
  15. async: function ()
  16. /*: boolean*/
  17. {
  18. var controller = _CoreManager.default.getStorageController();
  19. return !!controller.async;
  20. },
  21. getItem: function (path
  22. /*: string*/
  23. )
  24. /*: ?string*/
  25. {
  26. var controller = _CoreManager.default.getStorageController();
  27. if (controller.async === 1) {
  28. throw new Error('Synchronous storage is not supported by the current storage controller');
  29. }
  30. return controller.getItem(path);
  31. },
  32. getItemAsync: function (path
  33. /*: string*/
  34. )
  35. /*: Promise<string>*/
  36. {
  37. var controller = _CoreManager.default.getStorageController();
  38. if (controller.async === 1) {
  39. return controller.getItemAsync(path);
  40. }
  41. return Promise.resolve(controller.getItem(path));
  42. },
  43. setItem: function (path
  44. /*: string*/
  45. , value
  46. /*: string*/
  47. )
  48. /*: void*/
  49. {
  50. var controller = _CoreManager.default.getStorageController();
  51. if (controller.async === 1) {
  52. throw new Error('Synchronous storage is not supported by the current storage controller');
  53. }
  54. return controller.setItem(path, value);
  55. },
  56. setItemAsync: function (path
  57. /*: string*/
  58. , value
  59. /*: string*/
  60. )
  61. /*: Promise<void>*/
  62. {
  63. var controller = _CoreManager.default.getStorageController();
  64. if (controller.async === 1) {
  65. return controller.setItemAsync(path, value);
  66. }
  67. return Promise.resolve(controller.setItem(path, value));
  68. },
  69. removeItem: function (path
  70. /*: string*/
  71. )
  72. /*: void*/
  73. {
  74. var controller = _CoreManager.default.getStorageController();
  75. if (controller.async === 1) {
  76. throw new Error('Synchronous storage is not supported by the current storage controller');
  77. }
  78. return controller.removeItem(path);
  79. },
  80. removeItemAsync: function (path
  81. /*: string*/
  82. )
  83. /*: Promise<void>*/
  84. {
  85. var controller = _CoreManager.default.getStorageController();
  86. if (controller.async === 1) {
  87. return controller.removeItemAsync(path);
  88. }
  89. return Promise.resolve(controller.removeItem(path));
  90. },
  91. generatePath: function (path
  92. /*: string*/
  93. )
  94. /*: string*/
  95. {
  96. if (!_CoreManager.default.get('APPLICATION_ID')) {
  97. throw new Error('You need to call Parse.initialize before using Parse.');
  98. }
  99. if (typeof path !== 'string') {
  100. throw new Error('Tried to get a Storage path that was not a String.');
  101. }
  102. if (path[0] === '/') {
  103. path = path.substr(1);
  104. }
  105. return 'Parse/' + _CoreManager.default.get('APPLICATION_ID') + '/' + path;
  106. },
  107. _clear: function () {
  108. var controller = _CoreManager.default.getStorageController();
  109. if (controller.hasOwnProperty('clear')) {
  110. controller.clear();
  111. }
  112. }
  113. };
  114. module.exports = Storage;
  115. _CoreManager.default.setStorageController(require('./StorageController.browser'));