Storage.js 3.9 KB

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