LocalDatastore.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. "use strict";
  2. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  3. var _ParseQuery = _interopRequireDefault(require("./ParseQuery"));
  4. var _LocalDatastoreUtils = require("./LocalDatastoreUtils");
  5. function _interopRequireDefault(obj) {
  6. return obj && obj.__esModule ? obj : {
  7. default: obj
  8. };
  9. }
  10. /**
  11. * Copyright (c) 2015-present, Parse, LLC.
  12. * All rights reserved.
  13. *
  14. * This source code is licensed under the BSD-style license found in the
  15. * LICENSE file in the root directory of this source tree. An additional grant
  16. * of patent rights can be found in the PATENTS file in the same directory.
  17. *
  18. * @flow
  19. */
  20. /**
  21. * Provides a local datastore which can be used to store and retrieve <code>Parse.Object</code>. <br />
  22. * To enable this functionality, call <code>Parse.enableLocalDatastore()</code>.
  23. *
  24. * Pin object to add to local datastore
  25. *
  26. * <pre>await object.pin();</pre>
  27. * <pre>await object.pinWithName('pinName');</pre>
  28. *
  29. * Query pinned objects
  30. *
  31. * <pre>query.fromLocalDatastore();</pre>
  32. * <pre>query.fromPin();</pre>
  33. * <pre>query.fromPinWithName();</pre>
  34. *
  35. * <pre>const localObjects = await query.find();</pre>
  36. *
  37. * @class Parse.LocalDatastore
  38. * @static
  39. */
  40. const LocalDatastore = {
  41. isEnabled: false,
  42. isSyncing: false,
  43. fromPinWithName(name
  44. /*: string*/
  45. )
  46. /*: Promise<Array<Object>>*/
  47. {
  48. const controller = _CoreManager.default.getLocalDatastoreController();
  49. return controller.fromPinWithName(name);
  50. },
  51. pinWithName(name
  52. /*: string*/
  53. , value
  54. /*: any*/
  55. )
  56. /*: Promise<void>*/
  57. {
  58. const controller = _CoreManager.default.getLocalDatastoreController();
  59. return controller.pinWithName(name, value);
  60. },
  61. unPinWithName(name
  62. /*: string*/
  63. )
  64. /*: Promise<void>*/
  65. {
  66. const controller = _CoreManager.default.getLocalDatastoreController();
  67. return controller.unPinWithName(name);
  68. },
  69. _getAllContents()
  70. /*: Promise<Object>*/
  71. {
  72. const controller = _CoreManager.default.getLocalDatastoreController();
  73. return controller.getAllContents();
  74. },
  75. // Use for testing
  76. _getRawStorage()
  77. /*: Promise<Object>*/
  78. {
  79. const controller = _CoreManager.default.getLocalDatastoreController();
  80. return controller.getRawStorage();
  81. },
  82. _clear()
  83. /*: Promise<void>*/
  84. {
  85. const controller = _CoreManager.default.getLocalDatastoreController();
  86. return controller.clear();
  87. },
  88. // Pin the object and children recursively
  89. // Saves the object and children key to Pin Name
  90. async _handlePinAllWithName(name
  91. /*: string*/
  92. , objects
  93. /*: Array<ParseObject>*/
  94. )
  95. /*: Promise<void>*/
  96. {
  97. const pinName = this.getPinName(name);
  98. const toPinPromises = [];
  99. const objectKeys = [];
  100. for (const parent of objects) {
  101. const children = this._getChildren(parent);
  102. const parentKey = this.getKeyForObject(parent);
  103. const json = parent._toFullJSON();
  104. if (parent._localId) {
  105. json._localId = parent._localId;
  106. }
  107. children[parentKey] = json;
  108. for (const objectKey in children) {
  109. objectKeys.push(objectKey);
  110. toPinPromises.push(this.pinWithName(objectKey, [children[objectKey]]));
  111. }
  112. }
  113. const fromPinPromise = this.fromPinWithName(pinName);
  114. const [pinned] = await Promise.all([fromPinPromise, toPinPromises]);
  115. const toPin = [...new Set([...(pinned || []), ...objectKeys])];
  116. return this.pinWithName(pinName, toPin);
  117. },
  118. // Removes object and children keys from pin name
  119. // Keeps the object and children pinned
  120. async _handleUnPinAllWithName(name
  121. /*: string*/
  122. , objects
  123. /*: Array<ParseObject>*/
  124. ) {
  125. const localDatastore = await this._getAllContents();
  126. const pinName = this.getPinName(name);
  127. const promises = [];
  128. let objectKeys = [];
  129. for (const parent of objects) {
  130. const children = this._getChildren(parent);
  131. const parentKey = this.getKeyForObject(parent);
  132. objectKeys.push(parentKey, ...Object.keys(children));
  133. }
  134. objectKeys = [...new Set(objectKeys)];
  135. let pinned = localDatastore[pinName] || [];
  136. pinned = pinned.filter(item => !objectKeys.includes(item));
  137. if (pinned.length == 0) {
  138. promises.push(this.unPinWithName(pinName));
  139. delete localDatastore[pinName];
  140. } else {
  141. promises.push(this.pinWithName(pinName, pinned));
  142. localDatastore[pinName] = pinned;
  143. }
  144. for (const objectKey of objectKeys) {
  145. let hasReference = false;
  146. for (const key in localDatastore) {
  147. if (key === _LocalDatastoreUtils.DEFAULT_PIN || key.startsWith(_LocalDatastoreUtils.PIN_PREFIX)) {
  148. const pinnedObjects = localDatastore[key] || [];
  149. if (pinnedObjects.includes(objectKey)) {
  150. hasReference = true;
  151. break;
  152. }
  153. }
  154. }
  155. if (!hasReference) {
  156. promises.push(this.unPinWithName(objectKey));
  157. }
  158. }
  159. return Promise.all(promises);
  160. },
  161. // Retrieve all pointer fields from object recursively
  162. _getChildren(object
  163. /*: ParseObject*/
  164. ) {
  165. const encountered = {};
  166. const json = object._toFullJSON();
  167. for (const key in json) {
  168. if (json[key] && json[key].__type && json[key].__type === 'Object') {
  169. this._traverse(json[key], encountered);
  170. }
  171. }
  172. return encountered;
  173. },
  174. _traverse(object
  175. /*: any*/
  176. , encountered
  177. /*: any*/
  178. ) {
  179. if (!object.objectId) {
  180. return;
  181. } else {
  182. const objectKey = this.getKeyForObject(object);
  183. if (encountered[objectKey]) {
  184. return;
  185. }
  186. encountered[objectKey] = object;
  187. }
  188. for (const key in object) {
  189. let json = object[key];
  190. if (!object[key]) {
  191. json = object;
  192. }
  193. if (json.__type && json.__type === 'Object') {
  194. this._traverse(json, encountered);
  195. }
  196. }
  197. },
  198. // Transform keys in pin name to objects
  199. async _serializeObjectsFromPinName(name
  200. /*: string*/
  201. ) {
  202. const localDatastore = await this._getAllContents();
  203. const allObjects = [];
  204. for (const key in localDatastore) {
  205. if (key.startsWith(_LocalDatastoreUtils.OBJECT_PREFIX)) {
  206. allObjects.push(localDatastore[key][0]);
  207. }
  208. }
  209. if (!name) {
  210. return allObjects;
  211. }
  212. const pinName = this.getPinName(name);
  213. const pinned = localDatastore[pinName];
  214. if (!Array.isArray(pinned)) {
  215. return [];
  216. }
  217. const promises = pinned.map(objectKey => this.fromPinWithName(objectKey));
  218. let objects = await Promise.all(promises);
  219. objects = [].concat(...objects);
  220. return objects.filter(object => object != null);
  221. },
  222. // Replaces object pointers with pinned pointers
  223. // The object pointers may contain old data
  224. // Uses Breadth First Search Algorithm
  225. async _serializeObject(objectKey
  226. /*: string*/
  227. , localDatastore
  228. /*: any*/
  229. ) {
  230. let LDS = localDatastore;
  231. if (!LDS) {
  232. LDS = await this._getAllContents();
  233. }
  234. if (!LDS[objectKey] || LDS[objectKey].length === 0) {
  235. return null;
  236. }
  237. const root = LDS[objectKey][0];
  238. const queue = [];
  239. const meta = {};
  240. let uniqueId = 0;
  241. meta[uniqueId] = root;
  242. queue.push(uniqueId);
  243. while (queue.length !== 0) {
  244. const nodeId = queue.shift();
  245. const subTreeRoot = meta[nodeId];
  246. for (const field in subTreeRoot) {
  247. const value = subTreeRoot[field];
  248. if (value.__type && value.__type === 'Object') {
  249. const key = this.getKeyForObject(value);
  250. if (LDS[key] && LDS[key].length > 0) {
  251. const pointer = LDS[key][0];
  252. uniqueId++;
  253. meta[uniqueId] = pointer;
  254. subTreeRoot[field] = pointer;
  255. queue.push(uniqueId);
  256. }
  257. }
  258. }
  259. }
  260. return root;
  261. },
  262. // Called when an object is save / fetched
  263. // Update object pin value
  264. async _updateObjectIfPinned(object
  265. /*: ParseObject*/
  266. )
  267. /*: Promise<void>*/
  268. {
  269. if (!this.isEnabled) {
  270. return;
  271. }
  272. const objectKey = this.getKeyForObject(object);
  273. const pinned = await this.fromPinWithName(objectKey);
  274. if (!pinned || pinned.length === 0) {
  275. return;
  276. }
  277. return this.pinWithName(objectKey, [object._toFullJSON()]);
  278. },
  279. // Called when object is destroyed
  280. // Unpin object and remove all references from pin names
  281. // TODO: Destroy children?
  282. async _destroyObjectIfPinned(object
  283. /*: ParseObject*/
  284. ) {
  285. if (!this.isEnabled) {
  286. return;
  287. }
  288. const localDatastore = await this._getAllContents();
  289. const objectKey = this.getKeyForObject(object);
  290. const pin = localDatastore[objectKey];
  291. if (!pin) {
  292. return;
  293. }
  294. const promises = [this.unPinWithName(objectKey)];
  295. delete localDatastore[objectKey];
  296. for (const key in localDatastore) {
  297. if (key === _LocalDatastoreUtils.DEFAULT_PIN || key.startsWith(_LocalDatastoreUtils.PIN_PREFIX)) {
  298. let pinned = localDatastore[key] || [];
  299. if (pinned.includes(objectKey)) {
  300. pinned = pinned.filter(item => item !== objectKey);
  301. if (pinned.length == 0) {
  302. promises.push(this.unPinWithName(key));
  303. delete localDatastore[key];
  304. } else {
  305. promises.push(this.pinWithName(key, pinned));
  306. localDatastore[key] = pinned;
  307. }
  308. }
  309. }
  310. }
  311. return Promise.all(promises);
  312. },
  313. // Update pin and references of the unsaved object
  314. async _updateLocalIdForObject(localId
  315. /*: string*/
  316. , object
  317. /*: ParseObject*/
  318. ) {
  319. if (!this.isEnabled) {
  320. return;
  321. }
  322. const localKey = `${_LocalDatastoreUtils.OBJECT_PREFIX}${object.className}_${localId}`;
  323. const objectKey = this.getKeyForObject(object);
  324. const unsaved = await this.fromPinWithName(localKey);
  325. if (!unsaved || unsaved.length === 0) {
  326. return;
  327. }
  328. const promises = [this.unPinWithName(localKey), this.pinWithName(objectKey, unsaved)];
  329. const localDatastore = await this._getAllContents();
  330. for (const key in localDatastore) {
  331. if (key === _LocalDatastoreUtils.DEFAULT_PIN || key.startsWith(_LocalDatastoreUtils.PIN_PREFIX)) {
  332. let pinned = localDatastore[key] || [];
  333. if (pinned.includes(localKey)) {
  334. pinned = pinned.filter(item => item !== localKey);
  335. pinned.push(objectKey);
  336. promises.push(this.pinWithName(key, pinned));
  337. localDatastore[key] = pinned;
  338. }
  339. }
  340. }
  341. return Promise.all(promises);
  342. },
  343. /**
  344. * Updates Local Datastore from Server
  345. *
  346. * <pre>
  347. * await Parse.LocalDatastore.updateFromServer();
  348. * </pre>
  349. * @method updateFromServer
  350. * @name Parse.LocalDatastore.updateFromServer
  351. * @static
  352. */
  353. async updateFromServer() {
  354. if (!this.checkIfEnabled() || this.isSyncing) {
  355. return;
  356. }
  357. const localDatastore = await this._getAllContents();
  358. const keys = [];
  359. for (const key in localDatastore) {
  360. if (key.startsWith(_LocalDatastoreUtils.OBJECT_PREFIX)) {
  361. keys.push(key);
  362. }
  363. }
  364. if (keys.length === 0) {
  365. return;
  366. }
  367. this.isSyncing = true;
  368. const pointersHash = {};
  369. for (const key of keys) {
  370. // Ignore the OBJECT_PREFIX
  371. let [,, className, objectId] = key.split('_'); // User key is split into [ 'Parse', 'LDS', '', 'User', 'objectId' ]
  372. if (key.split('_').length === 5 && key.split('_')[3] === 'User') {
  373. className = '_User';
  374. objectId = key.split('_')[4];
  375. }
  376. if (objectId.startsWith('local')) {
  377. continue;
  378. }
  379. if (!(className in pointersHash)) {
  380. pointersHash[className] = new Set();
  381. }
  382. pointersHash[className].add(objectId);
  383. }
  384. const queryPromises = Object.keys(pointersHash).map(className => {
  385. const objectIds = Array.from(pointersHash[className]);
  386. const query = new _ParseQuery.default(className);
  387. query.limit(objectIds.length);
  388. if (objectIds.length === 1) {
  389. query.equalTo('objectId', objectIds[0]);
  390. } else {
  391. query.containedIn('objectId', objectIds);
  392. }
  393. return query.find();
  394. });
  395. try {
  396. const responses = await Promise.all(queryPromises);
  397. const objects = [].concat.apply([], responses);
  398. const pinPromises = objects.map(object => {
  399. const objectKey = this.getKeyForObject(object);
  400. return this.pinWithName(objectKey, object._toFullJSON());
  401. });
  402. await Promise.all(pinPromises);
  403. this.isSyncing = false;
  404. } catch (error) {
  405. console.error('Error syncing LocalDatastore: ', error);
  406. this.isSyncing = false;
  407. }
  408. },
  409. getKeyForObject(object
  410. /*: any*/
  411. ) {
  412. const objectId = object.objectId || object._getId();
  413. return `${_LocalDatastoreUtils.OBJECT_PREFIX}${object.className}_${objectId}`;
  414. },
  415. getPinName(pinName
  416. /*: ?string*/
  417. ) {
  418. if (!pinName || pinName === _LocalDatastoreUtils.DEFAULT_PIN) {
  419. return _LocalDatastoreUtils.DEFAULT_PIN;
  420. }
  421. return _LocalDatastoreUtils.PIN_PREFIX + pinName;
  422. },
  423. checkIfEnabled() {
  424. if (!this.isEnabled) {
  425. console.error('Parse.enableLocalDatastore() must be called first');
  426. }
  427. return this.isEnabled;
  428. }
  429. };
  430. module.exports = LocalDatastore;
  431. _CoreManager.default.setLocalDatastoreController(require('./LocalDatastoreController.default'));
  432. _CoreManager.default.setLocalDatastore(LocalDatastore);