Storage.js 3.2 KB

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