LocalDatastore.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  7. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  8. var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
  9. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  10. var _LocalDatastoreController = _interopRequireDefault(require("./LocalDatastoreController"));
  11. var _ParseQuery = _interopRequireDefault(require("./ParseQuery"));
  12. var _LocalDatastoreUtils = require("./LocalDatastoreUtils");
  13. var LocalDatastore = {
  14. isEnabled: false,
  15. isSyncing: false,
  16. fromPinWithName: function (name) {
  17. var controller = _CoreManager.default.getLocalDatastoreController();
  18. return controller.fromPinWithName(name);
  19. },
  20. pinWithName: function () {
  21. var _pinWithName = (0, _asyncToGenerator2.default)(function* (name, value) {
  22. var controller = _CoreManager.default.getLocalDatastoreController();
  23. return controller.pinWithName(name, value);
  24. });
  25. function pinWithName() {
  26. return _pinWithName.apply(this, arguments);
  27. }
  28. return pinWithName;
  29. }(),
  30. unPinWithName: function () {
  31. var _unPinWithName = (0, _asyncToGenerator2.default)(function* (name) {
  32. var controller = _CoreManager.default.getLocalDatastoreController();
  33. return controller.unPinWithName(name);
  34. });
  35. function unPinWithName() {
  36. return _unPinWithName.apply(this, arguments);
  37. }
  38. return unPinWithName;
  39. }(),
  40. _getAllContents: function () {
  41. var controller = _CoreManager.default.getLocalDatastoreController();
  42. return controller.getAllContents();
  43. },
  44. _getRawStorage: function () {
  45. var _getRawStorage2 = (0, _asyncToGenerator2.default)(function* () {
  46. var controller = _CoreManager.default.getLocalDatastoreController();
  47. return controller.getRawStorage();
  48. });
  49. function _getRawStorage() {
  50. return _getRawStorage2.apply(this, arguments);
  51. }
  52. return _getRawStorage;
  53. }(),
  54. _clear: function () {
  55. var _clear2 = (0, _asyncToGenerator2.default)(function* () {
  56. var controller = _CoreManager.default.getLocalDatastoreController();
  57. return controller.clear();
  58. });
  59. function _clear() {
  60. return _clear2.apply(this, arguments);
  61. }
  62. return _clear;
  63. }(),
  64. _handlePinAllWithName: function () {
  65. var _handlePinAllWithName2 = (0, _asyncToGenerator2.default)(function* (name, objects) {
  66. var pinName = this.getPinName(name);
  67. var toPinPromises = [];
  68. var objectKeys = [];
  69. for (var parent of objects) {
  70. var children = this._getChildren(parent);
  71. var parentKey = this.getKeyForObject(parent);
  72. var json = parent._toFullJSON(undefined, true);
  73. if (parent._localId) {
  74. json._localId = parent._localId;
  75. }
  76. children[parentKey] = json;
  77. for (var objectKey in children) {
  78. objectKeys.push(objectKey);
  79. toPinPromises.push(this.pinWithName(objectKey, [children[objectKey]]));
  80. }
  81. }
  82. var fromPinPromise = this.fromPinWithName(pinName);
  83. var _yield$Promise$all = yield Promise.all([fromPinPromise, toPinPromises]),
  84. _yield$Promise$all2 = (0, _slicedToArray2.default)(_yield$Promise$all, 1),
  85. pinned = _yield$Promise$all2[0];
  86. var toPin = (0, _toConsumableArray2.default)(new Set([].concat((0, _toConsumableArray2.default)(pinned || []), objectKeys)));
  87. return this.pinWithName(pinName, toPin);
  88. });
  89. function _handlePinAllWithName() {
  90. return _handlePinAllWithName2.apply(this, arguments);
  91. }
  92. return _handlePinAllWithName;
  93. }(),
  94. _handleUnPinAllWithName: function () {
  95. var _handleUnPinAllWithName2 = (0, _asyncToGenerator2.default)(function* (name, objects) {
  96. var localDatastore = yield this._getAllContents();
  97. var pinName = this.getPinName(name);
  98. var promises = [];
  99. var objectKeys = [];
  100. for (var parent of objects) {
  101. var _objectKeys;
  102. var children = this._getChildren(parent);
  103. var parentKey = this.getKeyForObject(parent);
  104. (_objectKeys = objectKeys).push.apply(_objectKeys, [parentKey].concat((0, _toConsumableArray2.default)(Object.keys(children))));
  105. }
  106. objectKeys = (0, _toConsumableArray2.default)(new Set(objectKeys));
  107. var pinned = localDatastore[pinName] || [];
  108. pinned = pinned.filter(function (item) {
  109. return !objectKeys.includes(item);
  110. });
  111. if (pinned.length == 0) {
  112. promises.push(this.unPinWithName(pinName));
  113. delete localDatastore[pinName];
  114. } else {
  115. promises.push(this.pinWithName(pinName, pinned));
  116. localDatastore[pinName] = pinned;
  117. }
  118. for (var objectKey of objectKeys) {
  119. var hasReference = false;
  120. for (var key in localDatastore) {
  121. if (key === _LocalDatastoreUtils.DEFAULT_PIN || key.startsWith(_LocalDatastoreUtils.PIN_PREFIX)) {
  122. var pinnedObjects = localDatastore[key] || [];
  123. if (pinnedObjects.includes(objectKey)) {
  124. hasReference = true;
  125. break;
  126. }
  127. }
  128. }
  129. if (!hasReference) {
  130. promises.push(this.unPinWithName(objectKey));
  131. }
  132. }
  133. return Promise.all(promises);
  134. });
  135. function _handleUnPinAllWithName() {
  136. return _handleUnPinAllWithName2.apply(this, arguments);
  137. }
  138. return _handleUnPinAllWithName;
  139. }(),
  140. _getChildren: function (object) {
  141. var encountered = {};
  142. var json = object._toFullJSON(undefined, true);
  143. for (var key in json) {
  144. if (json[key] && json[key].__type && json[key].__type === 'Object') {
  145. this._traverse(json[key], encountered);
  146. }
  147. }
  148. return encountered;
  149. },
  150. _traverse: function (object, encountered) {
  151. if (!object.objectId) {
  152. return;
  153. } else {
  154. var objectKey = this.getKeyForObject(object);
  155. if (encountered[objectKey]) {
  156. return;
  157. }
  158. encountered[objectKey] = object;
  159. }
  160. for (var key in object) {
  161. var json = object[key];
  162. if (!object[key]) {
  163. json = object;
  164. }
  165. if (json.__type && json.__type === 'Object') {
  166. this._traverse(json, encountered);
  167. }
  168. }
  169. },
  170. _serializeObjectsFromPinName: function () {
  171. var _serializeObjectsFromPinName2 = (0, _asyncToGenerator2.default)(function* (name) {
  172. var _this = this,
  173. _ref;
  174. var localDatastore = yield this._getAllContents();
  175. var allObjects = [];
  176. for (var key in localDatastore) {
  177. if (key.startsWith(_LocalDatastoreUtils.OBJECT_PREFIX)) {
  178. allObjects.push(localDatastore[key][0]);
  179. }
  180. }
  181. if (!name) {
  182. return allObjects;
  183. }
  184. var pinName = this.getPinName(name);
  185. var pinned = localDatastore[pinName];
  186. if (!Array.isArray(pinned)) {
  187. return [];
  188. }
  189. var promises = pinned.map(function (objectKey) {
  190. return _this.fromPinWithName(objectKey);
  191. });
  192. var objects = yield Promise.all(promises);
  193. objects = (_ref = []).concat.apply(_ref, (0, _toConsumableArray2.default)(objects));
  194. return objects.filter(function (object) {
  195. return object != null;
  196. });
  197. });
  198. function _serializeObjectsFromPinName() {
  199. return _serializeObjectsFromPinName2.apply(this, arguments);
  200. }
  201. return _serializeObjectsFromPinName;
  202. }(),
  203. _serializeObject: function () {
  204. var _serializeObject2 = (0, _asyncToGenerator2.default)(function* (objectKey, localDatastore) {
  205. var LDS = localDatastore;
  206. if (!LDS) {
  207. LDS = yield this._getAllContents();
  208. }
  209. if (!LDS[objectKey] || LDS[objectKey].length === 0) {
  210. return null;
  211. }
  212. var root = LDS[objectKey][0];
  213. var queue = [];
  214. var meta = {};
  215. var uniqueId = 0;
  216. meta[uniqueId] = root;
  217. queue.push(uniqueId);
  218. while (queue.length !== 0) {
  219. var nodeId = queue.shift();
  220. var subTreeRoot = meta[nodeId];
  221. for (var field in subTreeRoot) {
  222. var value = subTreeRoot[field];
  223. if (value.__type && value.__type === 'Object') {
  224. var key = this.getKeyForObject(value);
  225. if (LDS[key] && LDS[key].length > 0) {
  226. var pointer = LDS[key][0];
  227. uniqueId++;
  228. meta[uniqueId] = pointer;
  229. subTreeRoot[field] = pointer;
  230. queue.push(uniqueId);
  231. }
  232. }
  233. }
  234. }
  235. return root;
  236. });
  237. function _serializeObject() {
  238. return _serializeObject2.apply(this, arguments);
  239. }
  240. return _serializeObject;
  241. }(),
  242. _updateObjectIfPinned: function () {
  243. var _updateObjectIfPinned2 = (0, _asyncToGenerator2.default)(function* (object) {
  244. if (!this.isEnabled) {
  245. return;
  246. }
  247. var objectKey = this.getKeyForObject(object);
  248. var pinned = yield this.fromPinWithName(objectKey);
  249. if (!pinned || pinned.length === 0) {
  250. return;
  251. }
  252. return this.pinWithName(objectKey, [object._toFullJSON()]);
  253. });
  254. function _updateObjectIfPinned() {
  255. return _updateObjectIfPinned2.apply(this, arguments);
  256. }
  257. return _updateObjectIfPinned;
  258. }(),
  259. _destroyObjectIfPinned: function () {
  260. var _destroyObjectIfPinned2 = (0, _asyncToGenerator2.default)(function* (object) {
  261. if (!this.isEnabled) {
  262. return;
  263. }
  264. var localDatastore = yield this._getAllContents();
  265. var objectKey = this.getKeyForObject(object);
  266. var pin = localDatastore[objectKey];
  267. if (!pin) {
  268. return;
  269. }
  270. var promises = [this.unPinWithName(objectKey)];
  271. delete localDatastore[objectKey];
  272. for (var key in localDatastore) {
  273. if (key === _LocalDatastoreUtils.DEFAULT_PIN || key.startsWith(_LocalDatastoreUtils.PIN_PREFIX)) {
  274. var pinned = localDatastore[key] || [];
  275. if (pinned.includes(objectKey)) {
  276. pinned = pinned.filter(function (item) {
  277. return item !== objectKey;
  278. });
  279. if (pinned.length == 0) {
  280. promises.push(this.unPinWithName(key));
  281. delete localDatastore[key];
  282. } else {
  283. promises.push(this.pinWithName(key, pinned));
  284. localDatastore[key] = pinned;
  285. }
  286. }
  287. }
  288. }
  289. return Promise.all(promises);
  290. });
  291. function _destroyObjectIfPinned() {
  292. return _destroyObjectIfPinned2.apply(this, arguments);
  293. }
  294. return _destroyObjectIfPinned;
  295. }(),
  296. _updateLocalIdForObject: function () {
  297. var _updateLocalIdForObject2 = (0, _asyncToGenerator2.default)(function* (localId, object) {
  298. if (!this.isEnabled) {
  299. return;
  300. }
  301. var localKey = `${_LocalDatastoreUtils.OBJECT_PREFIX}${object.className}_${localId}`;
  302. var objectKey = this.getKeyForObject(object);
  303. var unsaved = yield this.fromPinWithName(localKey);
  304. if (!unsaved || unsaved.length === 0) {
  305. return;
  306. }
  307. var promises = [this.unPinWithName(localKey), this.pinWithName(objectKey, unsaved)];
  308. var localDatastore = yield this._getAllContents();
  309. for (var key in localDatastore) {
  310. if (key === _LocalDatastoreUtils.DEFAULT_PIN || key.startsWith(_LocalDatastoreUtils.PIN_PREFIX)) {
  311. var pinned = localDatastore[key] || [];
  312. if (pinned.includes(localKey)) {
  313. pinned = pinned.filter(function (item) {
  314. return item !== localKey;
  315. });
  316. pinned.push(objectKey);
  317. promises.push(this.pinWithName(key, pinned));
  318. localDatastore[key] = pinned;
  319. }
  320. }
  321. }
  322. return Promise.all(promises);
  323. });
  324. function _updateLocalIdForObject() {
  325. return _updateLocalIdForObject2.apply(this, arguments);
  326. }
  327. return _updateLocalIdForObject;
  328. }(),
  329. updateFromServer: function () {
  330. var _updateFromServer = (0, _asyncToGenerator2.default)(function* () {
  331. var _this2 = this;
  332. if (!this.checkIfEnabled() || this.isSyncing) {
  333. return;
  334. }
  335. var localDatastore = yield this._getAllContents();
  336. var keys = [];
  337. for (var key in localDatastore) {
  338. if (key.startsWith(_LocalDatastoreUtils.OBJECT_PREFIX)) {
  339. keys.push(key);
  340. }
  341. }
  342. if (keys.length === 0) {
  343. return;
  344. }
  345. this.isSyncing = true;
  346. var pointersHash = {};
  347. for (var _key of keys) {
  348. var _key$split = _key.split('_'),
  349. _key$split2 = (0, _slicedToArray2.default)(_key$split, 4),
  350. className = _key$split2[2],
  351. objectId = _key$split2[3];
  352. if (_key.split('_').length === 5 && _key.split('_')[3] === 'User') {
  353. className = '_User';
  354. objectId = _key.split('_')[4];
  355. }
  356. if (objectId.startsWith('local')) {
  357. continue;
  358. }
  359. if (!(className in pointersHash)) {
  360. pointersHash[className] = new Set();
  361. }
  362. pointersHash[className].add(objectId);
  363. }
  364. var queryPromises = Object.keys(pointersHash).map(function (className) {
  365. var objectIds = Array.from(pointersHash[className]);
  366. var query = new _ParseQuery.default(className);
  367. query.limit(objectIds.length);
  368. if (objectIds.length === 1) {
  369. query.equalTo('objectId', objectIds[0]);
  370. } else {
  371. query.containedIn('objectId', objectIds);
  372. }
  373. return query.find();
  374. });
  375. try {
  376. var responses = yield Promise.all(queryPromises);
  377. var objects = [].concat.apply([], responses);
  378. var pinPromises = objects.map(function (object) {
  379. var objectKey = _this2.getKeyForObject(object);
  380. return _this2.pinWithName(objectKey, object._toFullJSON());
  381. });
  382. yield Promise.all(pinPromises);
  383. this.isSyncing = false;
  384. } catch (error) {
  385. console.error('Error syncing LocalDatastore: ', error);
  386. this.isSyncing = false;
  387. }
  388. });
  389. function updateFromServer() {
  390. return _updateFromServer.apply(this, arguments);
  391. }
  392. return updateFromServer;
  393. }(),
  394. getKeyForObject: function (object) {
  395. var objectId = object.objectId || object._getId();
  396. return `${_LocalDatastoreUtils.OBJECT_PREFIX}${object.className}_${objectId}`;
  397. },
  398. getPinName: function (pinName) {
  399. if (!pinName || pinName === _LocalDatastoreUtils.DEFAULT_PIN) {
  400. return _LocalDatastoreUtils.DEFAULT_PIN;
  401. }
  402. return _LocalDatastoreUtils.PIN_PREFIX + pinName;
  403. },
  404. checkIfEnabled: function () {
  405. if (!this.isEnabled) {
  406. console.error('Parse.enableLocalDatastore() must be called first');
  407. }
  408. return this.isEnabled;
  409. }
  410. };
  411. module.exports = LocalDatastore;
  412. var _default = exports.default = LocalDatastore;
  413. _CoreManager.default.setLocalDatastoreController(_LocalDatastoreController.default);
  414. _CoreManager.default.setLocalDatastore(LocalDatastore);