LocalDatastore.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
  3. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  4. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  5. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  6. var _ParseQuery = _interopRequireDefault(require("./ParseQuery"));
  7. var _LocalDatastoreUtils = require("./LocalDatastoreUtils");
  8. function _createForOfIteratorHelperLoose(o, allowArrayLike) {
  9. var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
  10. if (it) return (it = it.call(o)).next.bind(it);
  11. if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
  12. if (it) o = it;
  13. var i = 0;
  14. return function () {
  15. if (i >= o.length) return {
  16. done: true
  17. };
  18. return {
  19. done: false,
  20. value: o[i++]
  21. };
  22. };
  23. }
  24. throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  25. }
  26. function _unsupportedIterableToArray(o, minLen) {
  27. if (!o) return;
  28. if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  29. var n = Object.prototype.toString.call(o).slice(8, -1);
  30. if (n === "Object" && o.constructor) n = o.constructor.name;
  31. if (n === "Map" || n === "Set") return Array.from(o);
  32. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
  33. }
  34. function _arrayLikeToArray(arr, len) {
  35. if (len == null || len > arr.length) len = arr.length;
  36. for (var i = 0, arr2 = new Array(len); i < len; i++) {
  37. arr2[i] = arr[i];
  38. }
  39. return arr2;
  40. }
  41. var LocalDatastore = {
  42. isEnabled: false,
  43. isSyncing: false,
  44. fromPinWithName: function (name) {
  45. var controller = _CoreManager.default.getLocalDatastoreController();
  46. return controller.fromPinWithName(name);
  47. },
  48. pinWithName: function (name, value) {
  49. var controller = _CoreManager.default.getLocalDatastoreController();
  50. return controller.pinWithName(name, value);
  51. },
  52. unPinWithName: function (name) {
  53. var controller = _CoreManager.default.getLocalDatastoreController();
  54. return controller.unPinWithName(name);
  55. },
  56. _getAllContents: function () {
  57. var controller = _CoreManager.default.getLocalDatastoreController();
  58. return controller.getAllContents();
  59. },
  60. _getRawStorage: function () {
  61. var controller = _CoreManager.default.getLocalDatastoreController();
  62. return controller.getRawStorage();
  63. },
  64. _clear: function () {
  65. var controller = _CoreManager.default.getLocalDatastoreController();
  66. return controller.clear();
  67. },
  68. _handlePinAllWithName: function (name, objects) {
  69. var _this = this;
  70. return function () {
  71. var pinName, toPinPromises, objectKeys, _iterator, _step, parent, children, parentKey, json, objectKey, fromPinPromise, _await$Promise$all, _await$Promise$all2, pinned, toPin;
  72. return _regenerator.default.async(function (_context) {
  73. while (1) {
  74. switch (_context.prev = _context.next) {
  75. case 0:
  76. pinName = _this.getPinName(name);
  77. toPinPromises = [];
  78. objectKeys = [];
  79. for (_iterator = _createForOfIteratorHelperLoose(objects); !(_step = _iterator()).done;) {
  80. parent = _step.value;
  81. children = _this._getChildren(parent);
  82. parentKey = _this.getKeyForObject(parent);
  83. json = parent._toFullJSON(undefined, true);
  84. if (parent._localId) {
  85. json._localId = parent._localId;
  86. }
  87. children[parentKey] = json;
  88. for (objectKey in children) {
  89. objectKeys.push(objectKey);
  90. toPinPromises.push(_this.pinWithName(objectKey, [children[objectKey]]));
  91. }
  92. }
  93. fromPinPromise = _this.fromPinWithName(pinName);
  94. _context.next = 7;
  95. return _regenerator.default.awrap(Promise.all([fromPinPromise, toPinPromises]));
  96. case 7:
  97. _await$Promise$all = _context.sent;
  98. _await$Promise$all2 = (0, _slicedToArray2.default)(_await$Promise$all, 1);
  99. pinned = _await$Promise$all2[0];
  100. toPin = (0, _toConsumableArray2.default)(new Set([].concat((0, _toConsumableArray2.default)(pinned || []), objectKeys)));
  101. return _context.abrupt("return", _this.pinWithName(pinName, toPin));
  102. case 12:
  103. case "end":
  104. return _context.stop();
  105. }
  106. }
  107. }, null, null, null, Promise);
  108. }();
  109. },
  110. _handleUnPinAllWithName: function (name, objects) {
  111. var _this2 = this;
  112. return function () {
  113. var localDatastore, pinName, promises, objectKeys, _iterator2, _step2, _objectKeys, parent, children, parentKey, pinned, _iterator3, _step3, objectKey, hasReference, key, pinnedObjects;
  114. return _regenerator.default.async(function (_context2) {
  115. while (1) {
  116. switch (_context2.prev = _context2.next) {
  117. case 0:
  118. _context2.next = 2;
  119. return _regenerator.default.awrap(_this2._getAllContents());
  120. case 2:
  121. localDatastore = _context2.sent;
  122. pinName = _this2.getPinName(name);
  123. promises = [];
  124. objectKeys = [];
  125. for (_iterator2 = _createForOfIteratorHelperLoose(objects); !(_step2 = _iterator2()).done;) {
  126. parent = _step2.value;
  127. children = _this2._getChildren(parent);
  128. parentKey = _this2.getKeyForObject(parent);
  129. (_objectKeys = objectKeys).push.apply(_objectKeys, [parentKey].concat((0, _toConsumableArray2.default)(Object.keys(children))));
  130. }
  131. objectKeys = (0, _toConsumableArray2.default)(new Set(objectKeys));
  132. pinned = localDatastore[pinName] || [];
  133. pinned = pinned.filter(function (item) {
  134. return !objectKeys.includes(item);
  135. });
  136. if (pinned.length === 0) {
  137. promises.push(_this2.unPinWithName(pinName));
  138. delete localDatastore[pinName];
  139. } else {
  140. promises.push(_this2.pinWithName(pinName, pinned));
  141. localDatastore[pinName] = pinned;
  142. }
  143. _iterator3 = _createForOfIteratorHelperLoose(objectKeys);
  144. case 12:
  145. if ((_step3 = _iterator3()).done) {
  146. _context2.next = 28;
  147. break;
  148. }
  149. objectKey = _step3.value;
  150. hasReference = false;
  151. _context2.t0 = _regenerator.default.keys(localDatastore);
  152. case 16:
  153. if ((_context2.t1 = _context2.t0()).done) {
  154. _context2.next = 25;
  155. break;
  156. }
  157. key = _context2.t1.value;
  158. if (!(key === _LocalDatastoreUtils.DEFAULT_PIN || key.startsWith(_LocalDatastoreUtils.PIN_PREFIX))) {
  159. _context2.next = 23;
  160. break;
  161. }
  162. pinnedObjects = localDatastore[key] || [];
  163. if (!pinnedObjects.includes(objectKey)) {
  164. _context2.next = 23;
  165. break;
  166. }
  167. hasReference = true;
  168. return _context2.abrupt("break", 25);
  169. case 23:
  170. _context2.next = 16;
  171. break;
  172. case 25:
  173. if (!hasReference) {
  174. promises.push(_this2.unPinWithName(objectKey));
  175. }
  176. case 26:
  177. _context2.next = 12;
  178. break;
  179. case 28:
  180. return _context2.abrupt("return", Promise.all(promises));
  181. case 29:
  182. case "end":
  183. return _context2.stop();
  184. }
  185. }
  186. }, null, null, null, Promise);
  187. }();
  188. },
  189. _getChildren: function (object) {
  190. var encountered = {};
  191. var json = object._toFullJSON(undefined, true);
  192. for (var key in json) {
  193. if (json[key] && json[key].__type && json[key].__type === 'Object') {
  194. this._traverse(json[key], encountered);
  195. }
  196. }
  197. return encountered;
  198. },
  199. _traverse: function (object, encountered) {
  200. if (!object.objectId) {
  201. return;
  202. }
  203. var objectKey = this.getKeyForObject(object);
  204. if (encountered[objectKey]) {
  205. return;
  206. }
  207. encountered[objectKey] = object;
  208. for (var key in object) {
  209. var json = object[key];
  210. if (!object[key]) {
  211. json = object;
  212. }
  213. if (json.__type && json.__type === 'Object') {
  214. this._traverse(json, encountered);
  215. }
  216. }
  217. },
  218. _serializeObjectsFromPinName: function (name) {
  219. var _this3 = this;
  220. return function () {
  221. var _ref;
  222. var localDatastore, allObjects, key, pinName, pinned, promises, objects;
  223. return _regenerator.default.async(function (_context3) {
  224. while (1) {
  225. switch (_context3.prev = _context3.next) {
  226. case 0:
  227. _context3.next = 2;
  228. return _regenerator.default.awrap(_this3._getAllContents());
  229. case 2:
  230. localDatastore = _context3.sent;
  231. allObjects = [];
  232. for (key in localDatastore) {
  233. if (key.startsWith(_LocalDatastoreUtils.OBJECT_PREFIX)) {
  234. allObjects.push(localDatastore[key][0]);
  235. }
  236. }
  237. if (name) {
  238. _context3.next = 7;
  239. break;
  240. }
  241. return _context3.abrupt("return", allObjects);
  242. case 7:
  243. pinName = _this3.getPinName(name);
  244. pinned = localDatastore[pinName];
  245. if (Array.isArray(pinned)) {
  246. _context3.next = 11;
  247. break;
  248. }
  249. return _context3.abrupt("return", []);
  250. case 11:
  251. promises = pinned.map(function (objectKey) {
  252. return _this3.fromPinWithName(objectKey);
  253. });
  254. _context3.next = 14;
  255. return _regenerator.default.awrap(Promise.all(promises));
  256. case 14:
  257. objects = _context3.sent;
  258. objects = (_ref = []).concat.apply(_ref, (0, _toConsumableArray2.default)(objects));
  259. return _context3.abrupt("return", objects.filter(function (object) {
  260. return object != null;
  261. }));
  262. case 17:
  263. case "end":
  264. return _context3.stop();
  265. }
  266. }
  267. }, null, null, null, Promise);
  268. }();
  269. },
  270. _serializeObject: function (objectKey, localDatastore) {
  271. var _this4 = this;
  272. return function () {
  273. var LDS, root, queue, meta, uniqueId, nodeId, subTreeRoot, field, value, key, pointer;
  274. return _regenerator.default.async(function (_context4) {
  275. while (1) {
  276. switch (_context4.prev = _context4.next) {
  277. case 0:
  278. LDS = localDatastore;
  279. if (LDS) {
  280. _context4.next = 5;
  281. break;
  282. }
  283. _context4.next = 4;
  284. return _regenerator.default.awrap(_this4._getAllContents());
  285. case 4:
  286. LDS = _context4.sent;
  287. case 5:
  288. if (!(!LDS[objectKey] || LDS[objectKey].length === 0)) {
  289. _context4.next = 7;
  290. break;
  291. }
  292. return _context4.abrupt("return", null);
  293. case 7:
  294. root = LDS[objectKey][0];
  295. queue = [];
  296. meta = {};
  297. uniqueId = 0;
  298. meta[uniqueId] = root;
  299. queue.push(uniqueId);
  300. while (queue.length !== 0) {
  301. nodeId = queue.shift();
  302. subTreeRoot = meta[nodeId];
  303. for (field in subTreeRoot) {
  304. value = subTreeRoot[field];
  305. if (value.__type && value.__type === 'Object') {
  306. key = _this4.getKeyForObject(value);
  307. if (LDS[key] && LDS[key].length > 0) {
  308. pointer = LDS[key][0];
  309. uniqueId++;
  310. meta[uniqueId] = pointer;
  311. subTreeRoot[field] = pointer;
  312. queue.push(uniqueId);
  313. }
  314. }
  315. }
  316. }
  317. return _context4.abrupt("return", root);
  318. case 15:
  319. case "end":
  320. return _context4.stop();
  321. }
  322. }
  323. }, null, null, null, Promise);
  324. }();
  325. },
  326. _updateObjectIfPinned: function (object) {
  327. var _this5 = this;
  328. return function () {
  329. var objectKey, pinned;
  330. return _regenerator.default.async(function (_context5) {
  331. while (1) {
  332. switch (_context5.prev = _context5.next) {
  333. case 0:
  334. if (_this5.isEnabled) {
  335. _context5.next = 2;
  336. break;
  337. }
  338. return _context5.abrupt("return");
  339. case 2:
  340. objectKey = _this5.getKeyForObject(object);
  341. _context5.next = 5;
  342. return _regenerator.default.awrap(_this5.fromPinWithName(objectKey));
  343. case 5:
  344. pinned = _context5.sent;
  345. if (!(!pinned || pinned.length === 0)) {
  346. _context5.next = 8;
  347. break;
  348. }
  349. return _context5.abrupt("return");
  350. case 8:
  351. return _context5.abrupt("return", _this5.pinWithName(objectKey, [object._toFullJSON()]));
  352. case 9:
  353. case "end":
  354. return _context5.stop();
  355. }
  356. }
  357. }, null, null, null, Promise);
  358. }();
  359. },
  360. _destroyObjectIfPinned: function (object) {
  361. var _this6 = this;
  362. return function () {
  363. var localDatastore, objectKey, pin, promises, key, pinned;
  364. return _regenerator.default.async(function (_context6) {
  365. while (1) {
  366. switch (_context6.prev = _context6.next) {
  367. case 0:
  368. if (_this6.isEnabled) {
  369. _context6.next = 2;
  370. break;
  371. }
  372. return _context6.abrupt("return");
  373. case 2:
  374. _context6.next = 4;
  375. return _regenerator.default.awrap(_this6._getAllContents());
  376. case 4:
  377. localDatastore = _context6.sent;
  378. objectKey = _this6.getKeyForObject(object);
  379. pin = localDatastore[objectKey];
  380. if (pin) {
  381. _context6.next = 9;
  382. break;
  383. }
  384. return _context6.abrupt("return");
  385. case 9:
  386. promises = [_this6.unPinWithName(objectKey)];
  387. delete localDatastore[objectKey];
  388. for (key in localDatastore) {
  389. if (key === _LocalDatastoreUtils.DEFAULT_PIN || key.startsWith(_LocalDatastoreUtils.PIN_PREFIX)) {
  390. pinned = localDatastore[key] || [];
  391. if (pinned.includes(objectKey)) {
  392. pinned = pinned.filter(function (item) {
  393. return item !== objectKey;
  394. });
  395. if (pinned.length === 0) {
  396. promises.push(_this6.unPinWithName(key));
  397. delete localDatastore[key];
  398. } else {
  399. promises.push(_this6.pinWithName(key, pinned));
  400. localDatastore[key] = pinned;
  401. }
  402. }
  403. }
  404. }
  405. return _context6.abrupt("return", Promise.all(promises));
  406. case 13:
  407. case "end":
  408. return _context6.stop();
  409. }
  410. }
  411. }, null, null, null, Promise);
  412. }();
  413. },
  414. _updateLocalIdForObject: function (localId, object) {
  415. var _this7 = this;
  416. return function () {
  417. var localKey, objectKey, unsaved, promises, localDatastore, key, pinned;
  418. return _regenerator.default.async(function (_context7) {
  419. while (1) {
  420. switch (_context7.prev = _context7.next) {
  421. case 0:
  422. if (_this7.isEnabled) {
  423. _context7.next = 2;
  424. break;
  425. }
  426. return _context7.abrupt("return");
  427. case 2:
  428. localKey = "" + _LocalDatastoreUtils.OBJECT_PREFIX + object.className + "_" + localId;
  429. objectKey = _this7.getKeyForObject(object);
  430. _context7.next = 6;
  431. return _regenerator.default.awrap(_this7.fromPinWithName(localKey));
  432. case 6:
  433. unsaved = _context7.sent;
  434. if (!(!unsaved || unsaved.length === 0)) {
  435. _context7.next = 9;
  436. break;
  437. }
  438. return _context7.abrupt("return");
  439. case 9:
  440. promises = [_this7.unPinWithName(localKey), _this7.pinWithName(objectKey, unsaved)];
  441. _context7.next = 12;
  442. return _regenerator.default.awrap(_this7._getAllContents());
  443. case 12:
  444. localDatastore = _context7.sent;
  445. for (key in localDatastore) {
  446. if (key === _LocalDatastoreUtils.DEFAULT_PIN || key.startsWith(_LocalDatastoreUtils.PIN_PREFIX)) {
  447. pinned = localDatastore[key] || [];
  448. if (pinned.includes(localKey)) {
  449. pinned = pinned.filter(function (item) {
  450. return item !== localKey;
  451. });
  452. pinned.push(objectKey);
  453. promises.push(_this7.pinWithName(key, pinned));
  454. localDatastore[key] = pinned;
  455. }
  456. }
  457. }
  458. return _context7.abrupt("return", Promise.all(promises));
  459. case 15:
  460. case "end":
  461. return _context7.stop();
  462. }
  463. }
  464. }, null, null, null, Promise);
  465. }();
  466. },
  467. updateFromServer: function () {
  468. var _this8 = this;
  469. return function () {
  470. var localDatastore, keys, key, pointersHash, _i, _keys, _key, _key$split, _key$split2, className, objectId, queryPromises, responses, objects, pinPromises;
  471. return _regenerator.default.async(function (_context8) {
  472. while (1) {
  473. switch (_context8.prev = _context8.next) {
  474. case 0:
  475. if (!(!_this8.checkIfEnabled() || _this8.isSyncing)) {
  476. _context8.next = 2;
  477. break;
  478. }
  479. return _context8.abrupt("return");
  480. case 2:
  481. _context8.next = 4;
  482. return _regenerator.default.awrap(_this8._getAllContents());
  483. case 4:
  484. localDatastore = _context8.sent;
  485. keys = [];
  486. for (key in localDatastore) {
  487. if (key.startsWith(_LocalDatastoreUtils.OBJECT_PREFIX)) {
  488. keys.push(key);
  489. }
  490. }
  491. if (!(keys.length === 0)) {
  492. _context8.next = 9;
  493. break;
  494. }
  495. return _context8.abrupt("return");
  496. case 9:
  497. _this8.isSyncing = true;
  498. pointersHash = {};
  499. _i = 0, _keys = keys;
  500. case 12:
  501. if (!(_i < _keys.length)) {
  502. _context8.next = 23;
  503. break;
  504. }
  505. _key = _keys[_i];
  506. _key$split = _key.split('_'), _key$split2 = (0, _slicedToArray2.default)(_key$split, 4), className = _key$split2[2], objectId = _key$split2[3];
  507. if (_key.split('_').length === 5 && _key.split('_')[3] === 'User') {
  508. className = '_User';
  509. objectId = _key.split('_')[4];
  510. }
  511. if (!objectId.startsWith('local')) {
  512. _context8.next = 18;
  513. break;
  514. }
  515. return _context8.abrupt("continue", 20);
  516. case 18:
  517. if (!(className in pointersHash)) {
  518. pointersHash[className] = new Set();
  519. }
  520. pointersHash[className].add(objectId);
  521. case 20:
  522. _i++;
  523. _context8.next = 12;
  524. break;
  525. case 23:
  526. queryPromises = Object.keys(pointersHash).map(function (className) {
  527. var objectIds = Array.from(pointersHash[className]);
  528. var query = new _ParseQuery.default(className);
  529. query.limit(objectIds.length);
  530. if (objectIds.length === 1) {
  531. query.equalTo('objectId', objectIds[0]);
  532. } else {
  533. query.containedIn('objectId', objectIds);
  534. }
  535. return query.find();
  536. });
  537. _context8.prev = 24;
  538. _context8.next = 27;
  539. return _regenerator.default.awrap(Promise.all(queryPromises));
  540. case 27:
  541. responses = _context8.sent;
  542. objects = [].concat.apply([], responses);
  543. pinPromises = objects.map(function (object) {
  544. var objectKey = _this8.getKeyForObject(object);
  545. return _this8.pinWithName(objectKey, object._toFullJSON());
  546. });
  547. _context8.next = 32;
  548. return _regenerator.default.awrap(Promise.all(pinPromises));
  549. case 32:
  550. _this8.isSyncing = false;
  551. _context8.next = 39;
  552. break;
  553. case 35:
  554. _context8.prev = 35;
  555. _context8.t0 = _context8["catch"](24);
  556. console.error('Error syncing LocalDatastore: ', _context8.t0);
  557. _this8.isSyncing = false;
  558. case 39:
  559. case "end":
  560. return _context8.stop();
  561. }
  562. }
  563. }, null, null, [[24, 35]], Promise);
  564. }();
  565. },
  566. getKeyForObject: function (object) {
  567. var objectId = object.objectId || object._getId();
  568. return "" + _LocalDatastoreUtils.OBJECT_PREFIX + object.className + "_" + objectId;
  569. },
  570. getPinName: function (pinName) {
  571. if (!pinName || pinName === _LocalDatastoreUtils.DEFAULT_PIN) {
  572. return _LocalDatastoreUtils.DEFAULT_PIN;
  573. }
  574. return _LocalDatastoreUtils.PIN_PREFIX + pinName;
  575. },
  576. checkIfEnabled: function () {
  577. if (!this.isEnabled) {
  578. console.error('Parse.enableLocalDatastore() must be called first');
  579. }
  580. return this.isEnabled;
  581. }
  582. };
  583. module.exports = LocalDatastore;
  584. _CoreManager.default.setLocalDatastoreController(require('./LocalDatastoreController.react-native'));
  585. _CoreManager.default.setLocalDatastore(LocalDatastore);