ParseOp.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.opFromJSON = opFromJSON;
  6. exports.RelationOp = exports.RemoveOp = exports.AddUniqueOp = exports.AddOp = exports.IncrementOp = exports.UnsetOp = exports.SetOp = exports.Op = void 0;
  7. var _arrayContainsObject = _interopRequireDefault(require("./arrayContainsObject"));
  8. var _decode = _interopRequireDefault(require("./decode"));
  9. var _encode = _interopRequireDefault(require("./encode"));
  10. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  11. var _ParseRelation = _interopRequireDefault(require("./ParseRelation"));
  12. var _unique = _interopRequireDefault(require("./unique"));
  13. function _interopRequireDefault(obj) {
  14. return obj && obj.__esModule ? obj : {
  15. default: obj
  16. };
  17. }
  18. /**
  19. * Copyright (c) 2015-present, Parse, LLC.
  20. * All rights reserved.
  21. *
  22. * This source code is licensed under the BSD-style license found in the
  23. * LICENSE file in the root directory of this source tree. An additional grant
  24. * of patent rights can be found in the PATENTS file in the same directory.
  25. *
  26. * @flow
  27. */
  28. function opFromJSON(json
  29. /*: { [key: string]: any }*/
  30. )
  31. /*: ?Op*/
  32. {
  33. if (!json || !json.__op) {
  34. return null;
  35. }
  36. switch (json.__op) {
  37. case 'Delete':
  38. return new UnsetOp();
  39. case 'Increment':
  40. return new IncrementOp(json.amount);
  41. case 'Add':
  42. return new AddOp((0, _decode.default)(json.objects));
  43. case 'AddUnique':
  44. return new AddUniqueOp((0, _decode.default)(json.objects));
  45. case 'Remove':
  46. return new RemoveOp((0, _decode.default)(json.objects));
  47. case 'AddRelation':
  48. {
  49. const toAdd = (0, _decode.default)(json.objects);
  50. if (!Array.isArray(toAdd)) {
  51. return new RelationOp([], []);
  52. }
  53. return new RelationOp(toAdd, []);
  54. }
  55. case 'RemoveRelation':
  56. {
  57. const toRemove = (0, _decode.default)(json.objects);
  58. if (!Array.isArray(toRemove)) {
  59. return new RelationOp([], []);
  60. }
  61. return new RelationOp([], toRemove);
  62. }
  63. case 'Batch':
  64. {
  65. let toAdd = [];
  66. let toRemove = [];
  67. for (let i = 0; i < json.ops.length; i++) {
  68. if (json.ops[i].__op === 'AddRelation') {
  69. toAdd = toAdd.concat((0, _decode.default)(json.ops[i].objects));
  70. } else if (json.ops[i].__op === 'RemoveRelation') {
  71. toRemove = toRemove.concat((0, _decode.default)(json.ops[i].objects));
  72. }
  73. }
  74. return new RelationOp(toAdd, toRemove);
  75. }
  76. }
  77. return null;
  78. }
  79. class Op {
  80. // Empty parent class
  81. applyTo(value
  82. /*: mixed*/
  83. )
  84. /*: mixed*/
  85. {}
  86. /* eslint-disable-line no-unused-vars */
  87. mergeWith(previous
  88. /*: Op*/
  89. )
  90. /*: ?Op*/
  91. {}
  92. /* eslint-disable-line no-unused-vars */
  93. toJSON()
  94. /*: mixed*/
  95. {}
  96. }
  97. exports.Op = Op;
  98. class SetOp extends Op {
  99. /*:: _value: ?mixed;*/
  100. constructor(value
  101. /*: mixed*/
  102. ) {
  103. super();
  104. this._value = value;
  105. }
  106. applyTo()
  107. /*: mixed*/
  108. {
  109. return this._value;
  110. }
  111. mergeWith()
  112. /*: SetOp*/
  113. {
  114. return new SetOp(this._value);
  115. }
  116. toJSON() {
  117. return (0, _encode.default)(this._value, false, true);
  118. }
  119. }
  120. exports.SetOp = SetOp;
  121. class UnsetOp extends Op {
  122. applyTo() {
  123. return undefined;
  124. }
  125. mergeWith()
  126. /*: UnsetOp*/
  127. {
  128. return new UnsetOp();
  129. }
  130. toJSON()
  131. /*: { __op: string }*/
  132. {
  133. return {
  134. __op: 'Delete'
  135. };
  136. }
  137. }
  138. exports.UnsetOp = UnsetOp;
  139. class IncrementOp extends Op {
  140. /*:: _amount: number;*/
  141. constructor(amount
  142. /*: number*/
  143. ) {
  144. super();
  145. if (typeof amount !== 'number') {
  146. throw new TypeError('Increment Op must be initialized with a numeric amount.');
  147. }
  148. this._amount = amount;
  149. }
  150. applyTo(value
  151. /*: ?mixed*/
  152. )
  153. /*: number*/
  154. {
  155. if (typeof value === 'undefined') {
  156. return this._amount;
  157. }
  158. if (typeof value !== 'number') {
  159. throw new TypeError('Cannot increment a non-numeric value.');
  160. }
  161. return this._amount + value;
  162. }
  163. mergeWith(previous
  164. /*: Op*/
  165. )
  166. /*: Op*/
  167. {
  168. if (!previous) {
  169. return this;
  170. }
  171. if (previous instanceof SetOp) {
  172. return new SetOp(this.applyTo(previous._value));
  173. }
  174. if (previous instanceof UnsetOp) {
  175. return new SetOp(this._amount);
  176. }
  177. if (previous instanceof IncrementOp) {
  178. return new IncrementOp(this.applyTo(previous._amount));
  179. }
  180. throw new Error('Cannot merge Increment Op with the previous Op');
  181. }
  182. toJSON()
  183. /*: { __op: string; amount: number }*/
  184. {
  185. return {
  186. __op: 'Increment',
  187. amount: this._amount
  188. };
  189. }
  190. }
  191. exports.IncrementOp = IncrementOp;
  192. class AddOp extends Op {
  193. /*:: _value: Array<mixed>;*/
  194. constructor(value
  195. /*: mixed | Array<mixed>*/
  196. ) {
  197. super();
  198. this._value = Array.isArray(value) ? value : [value];
  199. }
  200. applyTo(value
  201. /*: mixed*/
  202. )
  203. /*: Array<mixed>*/
  204. {
  205. if (value == null) {
  206. return this._value;
  207. }
  208. if (Array.isArray(value)) {
  209. return value.concat(this._value);
  210. }
  211. throw new Error('Cannot add elements to a non-array value');
  212. }
  213. mergeWith(previous
  214. /*: Op*/
  215. )
  216. /*: Op*/
  217. {
  218. if (!previous) {
  219. return this;
  220. }
  221. if (previous instanceof SetOp) {
  222. return new SetOp(this.applyTo(previous._value));
  223. }
  224. if (previous instanceof UnsetOp) {
  225. return new SetOp(this._value);
  226. }
  227. if (previous instanceof AddOp) {
  228. return new AddOp(this.applyTo(previous._value));
  229. }
  230. throw new Error('Cannot merge Add Op with the previous Op');
  231. }
  232. toJSON()
  233. /*: { __op: string; objects: mixed }*/
  234. {
  235. return {
  236. __op: 'Add',
  237. objects: (0, _encode.default)(this._value, false, true)
  238. };
  239. }
  240. }
  241. exports.AddOp = AddOp;
  242. class AddUniqueOp extends Op {
  243. /*:: _value: Array<mixed>;*/
  244. constructor(value
  245. /*: mixed | Array<mixed>*/
  246. ) {
  247. super();
  248. this._value = (0, _unique.default)(Array.isArray(value) ? value : [value]);
  249. }
  250. applyTo(value
  251. /*: mixed | Array<mixed>*/
  252. )
  253. /*: Array<mixed>*/
  254. {
  255. if (value == null) {
  256. return this._value || [];
  257. }
  258. if (Array.isArray(value)) {
  259. // copying value lets Flow guarantee the pointer isn't modified elsewhere
  260. const valueCopy = value;
  261. const toAdd = [];
  262. this._value.forEach(v => {
  263. if (v instanceof _ParseObject.default) {
  264. if (!(0, _arrayContainsObject.default)(valueCopy, v)) {
  265. toAdd.push(v);
  266. }
  267. } else {
  268. if (valueCopy.indexOf(v) < 0) {
  269. toAdd.push(v);
  270. }
  271. }
  272. });
  273. return value.concat(toAdd);
  274. }
  275. throw new Error('Cannot add elements to a non-array value');
  276. }
  277. mergeWith(previous
  278. /*: Op*/
  279. )
  280. /*: Op*/
  281. {
  282. if (!previous) {
  283. return this;
  284. }
  285. if (previous instanceof SetOp) {
  286. return new SetOp(this.applyTo(previous._value));
  287. }
  288. if (previous instanceof UnsetOp) {
  289. return new SetOp(this._value);
  290. }
  291. if (previous instanceof AddUniqueOp) {
  292. return new AddUniqueOp(this.applyTo(previous._value));
  293. }
  294. throw new Error('Cannot merge AddUnique Op with the previous Op');
  295. }
  296. toJSON()
  297. /*: { __op: string; objects: mixed }*/
  298. {
  299. return {
  300. __op: 'AddUnique',
  301. objects: (0, _encode.default)(this._value, false, true)
  302. };
  303. }
  304. }
  305. exports.AddUniqueOp = AddUniqueOp;
  306. class RemoveOp extends Op {
  307. /*:: _value: Array<mixed>;*/
  308. constructor(value
  309. /*: mixed | Array<mixed>*/
  310. ) {
  311. super();
  312. this._value = (0, _unique.default)(Array.isArray(value) ? value : [value]);
  313. }
  314. applyTo(value
  315. /*: mixed | Array<mixed>*/
  316. )
  317. /*: Array<mixed>*/
  318. {
  319. if (value == null) {
  320. return [];
  321. }
  322. if (Array.isArray(value)) {
  323. // var i = value.indexOf(this._value);
  324. const removed = value.concat([]);
  325. for (let i = 0; i < this._value.length; i++) {
  326. let index = removed.indexOf(this._value[i]);
  327. while (index > -1) {
  328. removed.splice(index, 1);
  329. index = removed.indexOf(this._value[i]);
  330. }
  331. if (this._value[i] instanceof _ParseObject.default && this._value[i].id) {
  332. for (let j = 0; j < removed.length; j++) {
  333. if (removed[j] instanceof _ParseObject.default && this._value[i].id === removed[j].id) {
  334. removed.splice(j, 1);
  335. j--;
  336. }
  337. }
  338. }
  339. }
  340. return removed;
  341. }
  342. throw new Error('Cannot remove elements from a non-array value');
  343. }
  344. mergeWith(previous
  345. /*: Op*/
  346. )
  347. /*: Op*/
  348. {
  349. if (!previous) {
  350. return this;
  351. }
  352. if (previous instanceof SetOp) {
  353. return new SetOp(this.applyTo(previous._value));
  354. }
  355. if (previous instanceof UnsetOp) {
  356. return new UnsetOp();
  357. }
  358. if (previous instanceof RemoveOp) {
  359. const uniques = previous._value.concat([]);
  360. for (let i = 0; i < this._value.length; i++) {
  361. if (this._value[i] instanceof _ParseObject.default) {
  362. if (!(0, _arrayContainsObject.default)(uniques, this._value[i])) {
  363. uniques.push(this._value[i]);
  364. }
  365. } else {
  366. if (uniques.indexOf(this._value[i]) < 0) {
  367. uniques.push(this._value[i]);
  368. }
  369. }
  370. }
  371. return new RemoveOp(uniques);
  372. }
  373. throw new Error('Cannot merge Remove Op with the previous Op');
  374. }
  375. toJSON()
  376. /*: { __op: string; objects: mixed }*/
  377. {
  378. return {
  379. __op: 'Remove',
  380. objects: (0, _encode.default)(this._value, false, true)
  381. };
  382. }
  383. }
  384. exports.RemoveOp = RemoveOp;
  385. class RelationOp extends Op {
  386. /*:: _targetClassName: ?string;*/
  387. /*:: relationsToAdd: Array<string>;*/
  388. /*:: relationsToRemove: Array<string>;*/
  389. constructor(adds
  390. /*: Array<ParseObject | string>*/
  391. , removes
  392. /*: Array<ParseObject | string>*/
  393. ) {
  394. super();
  395. this._targetClassName = null;
  396. if (Array.isArray(adds)) {
  397. this.relationsToAdd = (0, _unique.default)(adds.map(this._extractId, this));
  398. }
  399. if (Array.isArray(removes)) {
  400. this.relationsToRemove = (0, _unique.default)(removes.map(this._extractId, this));
  401. }
  402. }
  403. _extractId(obj
  404. /*: string | ParseObject*/
  405. )
  406. /*: string*/
  407. {
  408. if (typeof obj === 'string') {
  409. return obj;
  410. }
  411. if (!obj.id) {
  412. throw new Error('You cannot add or remove an unsaved Parse Object from a relation');
  413. }
  414. if (!this._targetClassName) {
  415. this._targetClassName = obj.className;
  416. }
  417. if (this._targetClassName !== obj.className) {
  418. throw new Error('Tried to create a Relation with 2 different object types: ' + this._targetClassName + ' and ' + obj.className + '.');
  419. }
  420. return obj.id;
  421. }
  422. applyTo(value
  423. /*: mixed*/
  424. , object
  425. /*:: ?: { className: string, id: ?string }*/
  426. , key
  427. /*:: ?: string*/
  428. )
  429. /*: ?ParseRelation*/
  430. {
  431. if (!value) {
  432. if (!object || !key) {
  433. throw new Error('Cannot apply a RelationOp without either a previous value, or an object and a key');
  434. }
  435. const parent = new _ParseObject.default(object.className);
  436. if (object.id && object.id.indexOf('local') === 0) {
  437. parent._localId = object.id;
  438. } else if (object.id) {
  439. parent.id = object.id;
  440. }
  441. const relation = new _ParseRelation.default(parent, key);
  442. relation.targetClassName = this._targetClassName;
  443. return relation;
  444. }
  445. if (value instanceof _ParseRelation.default) {
  446. if (this._targetClassName) {
  447. if (value.targetClassName) {
  448. if (this._targetClassName !== value.targetClassName) {
  449. throw new Error('Related object must be a ' + value.targetClassName + ', but a ' + this._targetClassName + ' was passed in.');
  450. }
  451. } else {
  452. value.targetClassName = this._targetClassName;
  453. }
  454. }
  455. return value;
  456. } else {
  457. throw new Error('Relation cannot be applied to a non-relation field');
  458. }
  459. }
  460. mergeWith(previous
  461. /*: Op*/
  462. )
  463. /*: Op*/
  464. {
  465. if (!previous) {
  466. return this;
  467. } else if (previous instanceof UnsetOp) {
  468. throw new Error('You cannot modify a relation after deleting it.');
  469. } else if (previous instanceof SetOp && previous._value instanceof _ParseRelation.default) {
  470. return this;
  471. } else if (previous instanceof RelationOp) {
  472. if (previous._targetClassName && previous._targetClassName !== this._targetClassName) {
  473. throw new Error('Related object must be of class ' + previous._targetClassName + ', but ' + (this._targetClassName || 'null') + ' was passed in.');
  474. }
  475. const newAdd = previous.relationsToAdd.concat([]);
  476. this.relationsToRemove.forEach(r => {
  477. const index = newAdd.indexOf(r);
  478. if (index > -1) {
  479. newAdd.splice(index, 1);
  480. }
  481. });
  482. this.relationsToAdd.forEach(r => {
  483. const index = newAdd.indexOf(r);
  484. if (index < 0) {
  485. newAdd.push(r);
  486. }
  487. });
  488. const newRemove = previous.relationsToRemove.concat([]);
  489. this.relationsToAdd.forEach(r => {
  490. const index = newRemove.indexOf(r);
  491. if (index > -1) {
  492. newRemove.splice(index, 1);
  493. }
  494. });
  495. this.relationsToRemove.forEach(r => {
  496. const index = newRemove.indexOf(r);
  497. if (index < 0) {
  498. newRemove.push(r);
  499. }
  500. });
  501. const newRelation = new RelationOp(newAdd, newRemove);
  502. newRelation._targetClassName = this._targetClassName;
  503. return newRelation;
  504. }
  505. throw new Error('Cannot merge Relation Op with the previous Op');
  506. }
  507. toJSON()
  508. /*: { __op?: string; objects?: mixed; ops?: mixed }*/
  509. {
  510. const idToPointer = id => {
  511. return {
  512. __type: 'Pointer',
  513. className: this._targetClassName,
  514. objectId: id
  515. };
  516. };
  517. let adds = null;
  518. let removes = null;
  519. let pointers = null;
  520. if (this.relationsToAdd.length > 0) {
  521. pointers = this.relationsToAdd.map(idToPointer);
  522. adds = {
  523. __op: 'AddRelation',
  524. objects: pointers
  525. };
  526. }
  527. if (this.relationsToRemove.length > 0) {
  528. pointers = this.relationsToRemove.map(idToPointer);
  529. removes = {
  530. __op: 'RemoveRelation',
  531. objects: pointers
  532. };
  533. }
  534. if (adds && removes) {
  535. return {
  536. __op: 'Batch',
  537. ops: [adds, removes]
  538. };
  539. }
  540. return adds || removes || {};
  541. }
  542. }
  543. exports.RelationOp = RelationOp;