Storage.js 2.9 KB

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