LocalDatastoreController.react-native.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  4. var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
  5. var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
  6. var _LocalDatastoreUtils = require("./LocalDatastoreUtils");
  7. /**
  8. * Copyright (c) 2015-present, Parse, LLC.
  9. * All rights reserved.
  10. *
  11. * This source code is licensed under the BSD-style license found in the
  12. * LICENSE file in the root directory of this source tree. An additional grant
  13. * of patent rights can be found in the PATENTS file in the same directory.
  14. *
  15. * @flow
  16. */
  17. var RNStorage = require('./StorageController.react-native');
  18. var LocalDatastoreController = {
  19. fromPinWithName: function () {
  20. var _fromPinWithName = (0, _asyncToGenerator2.default)(
  21. /*#__PURE__*/
  22. _regenerator.default.mark(function _callee(name
  23. /*: string*/
  24. ) {
  25. var values, objects;
  26. return _regenerator.default.wrap(function (_context) {
  27. while (1) {
  28. switch (_context.prev = _context.next) {
  29. case 0:
  30. _context.next = 2;
  31. return RNStorage.getItemAsync(name);
  32. case 2:
  33. values = _context.sent;
  34. if (values) {
  35. _context.next = 5;
  36. break;
  37. }
  38. return _context.abrupt("return", []);
  39. case 5:
  40. objects = JSON.parse(values);
  41. return _context.abrupt("return", objects);
  42. case 7:
  43. case "end":
  44. return _context.stop();
  45. }
  46. }
  47. }, _callee);
  48. }));
  49. return function () {
  50. return _fromPinWithName.apply(this, arguments);
  51. };
  52. }(),
  53. pinWithName: function () {
  54. var _pinWithName = (0, _asyncToGenerator2.default)(
  55. /*#__PURE__*/
  56. _regenerator.default.mark(function _callee2(name
  57. /*: string*/
  58. , value
  59. /*: any*/
  60. ) {
  61. var values;
  62. return _regenerator.default.wrap(function (_context2) {
  63. while (1) {
  64. switch (_context2.prev = _context2.next) {
  65. case 0:
  66. _context2.prev = 0;
  67. values = JSON.stringify(value);
  68. _context2.next = 4;
  69. return RNStorage.setItemAsync(name, values);
  70. case 4:
  71. _context2.next = 9;
  72. break;
  73. case 6:
  74. _context2.prev = 6;
  75. _context2.t0 = _context2["catch"](0); // Quota exceeded, possibly due to Safari Private Browsing mode
  76. console.error(_context2.t0.message);
  77. case 9:
  78. case "end":
  79. return _context2.stop();
  80. }
  81. }
  82. }, _callee2, null, [[0, 6]]);
  83. }));
  84. return function () {
  85. return _pinWithName.apply(this, arguments);
  86. };
  87. }(),
  88. unPinWithName: function (name
  89. /*: string*/
  90. )
  91. /*: Promise<void>*/
  92. {
  93. return RNStorage.removeItemAsync(name);
  94. },
  95. getAllContents: function () {
  96. var _getAllContents = (0, _asyncToGenerator2.default)(
  97. /*#__PURE__*/
  98. _regenerator.default.mark(function _callee3() {
  99. var keys, batch, i, key, LDS, results;
  100. return _regenerator.default.wrap(function (_context3) {
  101. while (1) {
  102. switch (_context3.prev = _context3.next) {
  103. case 0:
  104. _context3.next = 2;
  105. return RNStorage.getAllKeys();
  106. case 2:
  107. keys = _context3.sent;
  108. batch = [];
  109. for (i = 0; i < keys.length; i += 1) {
  110. key = keys[i];
  111. if ((0, _LocalDatastoreUtils.isLocalDatastoreKey)(key)) {
  112. batch.push(key);
  113. }
  114. }
  115. LDS = {};
  116. results = [];
  117. _context3.prev = 7;
  118. _context3.next = 10;
  119. return RNStorage.multiGet(batch);
  120. case 10:
  121. results = _context3.sent;
  122. _context3.next = 17;
  123. break;
  124. case 13:
  125. _context3.prev = 13;
  126. _context3.t0 = _context3["catch"](7);
  127. console.error('Error getAllContents: ', _context3.t0);
  128. return _context3.abrupt("return", {});
  129. case 17:
  130. results.forEach(function (pair) {
  131. var _pair = (0, _slicedToArray2.default)(pair, 2),
  132. key = _pair[0],
  133. value = _pair[1];
  134. try {
  135. LDS[key] = JSON.parse(value);
  136. } catch (error) {
  137. LDS[key] = null;
  138. }
  139. });
  140. return _context3.abrupt("return", LDS);
  141. case 19:
  142. case "end":
  143. return _context3.stop();
  144. }
  145. }
  146. }, _callee3, null, [[7, 13]]);
  147. }));
  148. return function () {
  149. return _getAllContents.apply(this, arguments);
  150. };
  151. }(),
  152. getRawStorage: function () {
  153. var _getRawStorage = (0, _asyncToGenerator2.default)(
  154. /*#__PURE__*/
  155. _regenerator.default.mark(function _callee4() {
  156. var keys, storage, results;
  157. return _regenerator.default.wrap(function (_context4) {
  158. while (1) {
  159. switch (_context4.prev = _context4.next) {
  160. case 0:
  161. _context4.next = 2;
  162. return RNStorage.getAllKeys();
  163. case 2:
  164. keys = _context4.sent;
  165. storage = {};
  166. _context4.next = 6;
  167. return RNStorage.multiGet(keys);
  168. case 6:
  169. results = _context4.sent;
  170. results.map(function (pair) {
  171. var _pair2 = (0, _slicedToArray2.default)(pair, 2),
  172. key = _pair2[0],
  173. value = _pair2[1];
  174. storage[key] = value;
  175. });
  176. return _context4.abrupt("return", storage);
  177. case 9:
  178. case "end":
  179. return _context4.stop();
  180. }
  181. }
  182. }, _callee4);
  183. }));
  184. return function () {
  185. return _getRawStorage.apply(this, arguments);
  186. };
  187. }(),
  188. clear: function () {
  189. var _clear = (0, _asyncToGenerator2.default)(
  190. /*#__PURE__*/
  191. _regenerator.default.mark(function _callee5() {
  192. var keys, batch, i, key;
  193. return _regenerator.default.wrap(function (_context5) {
  194. while (1) {
  195. switch (_context5.prev = _context5.next) {
  196. case 0:
  197. _context5.next = 2;
  198. return RNStorage.getAllKeys();
  199. case 2:
  200. keys = _context5.sent;
  201. batch = [];
  202. for (i = 0; i < keys.length; i += 1) {
  203. key = keys[i];
  204. if ((0, _LocalDatastoreUtils.isLocalDatastoreKey)(key)) {
  205. batch.push(key);
  206. }
  207. }
  208. return _context5.abrupt("return", RNStorage.multiRemove(batch).catch(function (error) {
  209. return console.error('Error clearing local datastore: ', error);
  210. }));
  211. case 6:
  212. case "end":
  213. return _context5.stop();
  214. }
  215. }
  216. }, _callee5);
  217. }));
  218. return function () {
  219. return _clear.apply(this, arguments);
  220. };
  221. }()
  222. };
  223. module.exports = LocalDatastoreController;