ParseOp.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. "use strict";
  2. var _Reflect$construct = require("@babel/runtime-corejs3/core-js-stable/reflect/construct");
  3. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
  4. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  5. _Object$defineProperty(exports, "__esModule", {
  6. value: true
  7. });
  8. exports.UnsetOp = exports.SetOp = exports.RemoveOp = exports.RelationOp = exports.Op = exports.IncrementOp = exports.AddUniqueOp = exports.AddOp = void 0;
  9. exports.opFromJSON = opFromJSON;
  10. var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/assertThisInitialized"));
  11. var _inherits2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/inherits"));
  12. var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/possibleConstructorReturn"));
  13. var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/getPrototypeOf"));
  14. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
  15. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
  16. var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
  17. var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array"));
  18. var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
  19. var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each"));
  20. var _indexOf = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/index-of"));
  21. var _splice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/splice"));
  22. var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/map"));
  23. var _arrayContainsObject = _interopRequireDefault(require("./arrayContainsObject"));
  24. var _decode = _interopRequireDefault(require("./decode"));
  25. var _encode = _interopRequireDefault(require("./encode"));
  26. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  27. var _ParseRelation = _interopRequireDefault(require("./ParseRelation"));
  28. var _unique = _interopRequireDefault(require("./unique"));
  29. function _createSuper(Derived) {
  30. var hasNativeReflectConstruct = _isNativeReflectConstruct();
  31. return function () {
  32. var Super = (0, _getPrototypeOf2.default)(Derived),
  33. result;
  34. if (hasNativeReflectConstruct) {
  35. var NewTarget = (0, _getPrototypeOf2.default)(this).constructor;
  36. result = _Reflect$construct(Super, arguments, NewTarget);
  37. } else {
  38. result = Super.apply(this, arguments);
  39. }
  40. return (0, _possibleConstructorReturn2.default)(this, result);
  41. };
  42. }
  43. function _isNativeReflectConstruct() {
  44. if (typeof Reflect === "undefined" || !_Reflect$construct) return false;
  45. if (_Reflect$construct.sham) return false;
  46. if (typeof Proxy === "function") return true;
  47. try {
  48. Boolean.prototype.valueOf.call(_Reflect$construct(Boolean, [], function () {}));
  49. return true;
  50. } catch (e) {
  51. return false;
  52. }
  53. }
  54. function opFromJSON(json
  55. /*: { [key: string]: any }*/
  56. )
  57. /*: ?Op*/
  58. {
  59. if (!json || !json.__op) {
  60. return null;
  61. }
  62. switch (json.__op) {
  63. case 'Delete':
  64. return new UnsetOp();
  65. case 'Increment':
  66. return new IncrementOp(json.amount);
  67. case 'Add':
  68. return new AddOp((0, _decode.default)(json.objects));
  69. case 'AddUnique':
  70. return new AddUniqueOp((0, _decode.default)(json.objects));
  71. case 'Remove':
  72. return new RemoveOp((0, _decode.default)(json.objects));
  73. case 'AddRelation':
  74. {
  75. var toAdd = (0, _decode.default)(json.objects);
  76. if (!(0, _isArray.default)(toAdd)) {
  77. return new RelationOp([], []);
  78. }
  79. return new RelationOp(toAdd, []);
  80. }
  81. case 'RemoveRelation':
  82. {
  83. var toRemove = (0, _decode.default)(json.objects);
  84. if (!(0, _isArray.default)(toRemove)) {
  85. return new RelationOp([], []);
  86. }
  87. return new RelationOp([], toRemove);
  88. }
  89. case 'Batch':
  90. {
  91. var _toAdd = [];
  92. var _toRemove = [];
  93. for (var i = 0; i < json.ops.length; i++) {
  94. if (json.ops[i].__op === 'AddRelation') {
  95. _toAdd = (0, _concat.default)(_toAdd).call(_toAdd, (0, _decode.default)(json.ops[i].objects));
  96. } else if (json.ops[i].__op === 'RemoveRelation') {
  97. _toRemove = (0, _concat.default)(_toRemove).call(_toRemove, (0, _decode.default)(json.ops[i].objects));
  98. }
  99. }
  100. return new RelationOp(_toAdd, _toRemove);
  101. }
  102. default:
  103. return null;
  104. }
  105. }
  106. var Op = /*#__PURE__*/function () {
  107. function Op() {
  108. (0, _classCallCheck2.default)(this, Op);
  109. }
  110. (0, _createClass2.default)(Op, [{
  111. key: "applyTo",
  112. value: // Empty parent class
  113. function ()
  114. /*: mixed*/
  115. {}
  116. /* eslint-disable-line no-unused-vars */
  117. }, {
  118. key: "mergeWith",
  119. value: function ()
  120. /*: ?Op*/
  121. {}
  122. /* eslint-disable-line no-unused-vars */
  123. }, {
  124. key: "toJSON",
  125. value: function ()
  126. /*: mixed*/
  127. {}
  128. }]);
  129. return Op;
  130. }();
  131. exports.Op = Op;
  132. var SetOp = /*#__PURE__*/function (_Op) {
  133. (0, _inherits2.default)(SetOp, _Op);
  134. var _super = _createSuper(SetOp);
  135. function SetOp(value
  136. /*: mixed*/
  137. ) {
  138. var _this;
  139. (0, _classCallCheck2.default)(this, SetOp);
  140. _this = _super.call(this);
  141. (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "_value", void 0);
  142. _this._value = value;
  143. return _this;
  144. }
  145. (0, _createClass2.default)(SetOp, [{
  146. key: "applyTo",
  147. value: function ()
  148. /*: mixed*/
  149. {
  150. return this._value;
  151. }
  152. }, {
  153. key: "mergeWith",
  154. value: function ()
  155. /*: SetOp*/
  156. {
  157. return new SetOp(this._value);
  158. }
  159. }, {
  160. key: "toJSON",
  161. value: function (offline
  162. /*:: ?: boolean*/
  163. ) {
  164. return (0, _encode.default)(this._value, false, true, undefined, offline);
  165. }
  166. }]);
  167. return SetOp;
  168. }(Op);
  169. exports.SetOp = SetOp;
  170. var UnsetOp = /*#__PURE__*/function (_Op2) {
  171. (0, _inherits2.default)(UnsetOp, _Op2);
  172. var _super2 = _createSuper(UnsetOp);
  173. function UnsetOp() {
  174. (0, _classCallCheck2.default)(this, UnsetOp);
  175. return _super2.apply(this, arguments);
  176. }
  177. (0, _createClass2.default)(UnsetOp, [{
  178. key: "applyTo",
  179. value: function () {
  180. return undefined;
  181. }
  182. }, {
  183. key: "mergeWith",
  184. value: function ()
  185. /*: UnsetOp*/
  186. {
  187. return new UnsetOp();
  188. }
  189. }, {
  190. key: "toJSON",
  191. value: function ()
  192. /*: { __op: string }*/
  193. {
  194. return {
  195. __op: 'Delete'
  196. };
  197. }
  198. }]);
  199. return UnsetOp;
  200. }(Op);
  201. exports.UnsetOp = UnsetOp;
  202. var IncrementOp = /*#__PURE__*/function (_Op3) {
  203. (0, _inherits2.default)(IncrementOp, _Op3);
  204. var _super3 = _createSuper(IncrementOp);
  205. function IncrementOp(amount
  206. /*: number*/
  207. ) {
  208. var _this2;
  209. (0, _classCallCheck2.default)(this, IncrementOp);
  210. _this2 = _super3.call(this);
  211. (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this2), "_amount", void 0);
  212. if (typeof amount !== 'number') {
  213. throw new TypeError('Increment Op must be initialized with a numeric amount.');
  214. }
  215. _this2._amount = amount;
  216. return _this2;
  217. }
  218. (0, _createClass2.default)(IncrementOp, [{
  219. key: "applyTo",
  220. value: function (value
  221. /*: ?mixed*/
  222. )
  223. /*: number*/
  224. {
  225. if (typeof value === 'undefined') {
  226. return this._amount;
  227. }
  228. if (typeof value !== 'number') {
  229. throw new TypeError('Cannot increment a non-numeric value.');
  230. }
  231. return this._amount + value;
  232. }
  233. }, {
  234. key: "mergeWith",
  235. value: function (previous
  236. /*: Op*/
  237. )
  238. /*: Op*/
  239. {
  240. if (!previous) {
  241. return this;
  242. }
  243. if (previous instanceof SetOp) {
  244. return new SetOp(this.applyTo(previous._value));
  245. }
  246. if (previous instanceof UnsetOp) {
  247. return new SetOp(this._amount);
  248. }
  249. if (previous instanceof IncrementOp) {
  250. return new IncrementOp(this.applyTo(previous._amount));
  251. }
  252. throw new Error('Cannot merge Increment Op with the previous Op');
  253. }
  254. }, {
  255. key: "toJSON",
  256. value: function ()
  257. /*: { __op: string, amount: number }*/
  258. {
  259. return {
  260. __op: 'Increment',
  261. amount: this._amount
  262. };
  263. }
  264. }]);
  265. return IncrementOp;
  266. }(Op);
  267. exports.IncrementOp = IncrementOp;
  268. var AddOp = /*#__PURE__*/function (_Op4) {
  269. (0, _inherits2.default)(AddOp, _Op4);
  270. var _super4 = _createSuper(AddOp);
  271. function AddOp(value
  272. /*: mixed | Array<mixed>*/
  273. ) {
  274. var _this3;
  275. (0, _classCallCheck2.default)(this, AddOp);
  276. _this3 = _super4.call(this);
  277. (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this3), "_value", void 0);
  278. _this3._value = (0, _isArray.default)(value) ? value : [value];
  279. return _this3;
  280. }
  281. (0, _createClass2.default)(AddOp, [{
  282. key: "applyTo",
  283. value: function (value
  284. /*: mixed*/
  285. )
  286. /*: Array<mixed>*/
  287. {
  288. if (value == null) {
  289. return this._value;
  290. }
  291. if ((0, _isArray.default)(value)) {
  292. return (0, _concat.default)(value).call(value, this._value);
  293. }
  294. throw new Error('Cannot add elements to a non-array value');
  295. }
  296. }, {
  297. key: "mergeWith",
  298. value: function (previous
  299. /*: Op*/
  300. )
  301. /*: Op*/
  302. {
  303. if (!previous) {
  304. return this;
  305. }
  306. if (previous instanceof SetOp) {
  307. return new SetOp(this.applyTo(previous._value));
  308. }
  309. if (previous instanceof UnsetOp) {
  310. return new SetOp(this._value);
  311. }
  312. if (previous instanceof AddOp) {
  313. return new AddOp(this.applyTo(previous._value));
  314. }
  315. throw new Error('Cannot merge Add Op with the previous Op');
  316. }
  317. }, {
  318. key: "toJSON",
  319. value: function ()
  320. /*: { __op: string, objects: mixed }*/
  321. {
  322. return {
  323. __op: 'Add',
  324. objects: (0, _encode.default)(this._value, false, true)
  325. };
  326. }
  327. }]);
  328. return AddOp;
  329. }(Op);
  330. exports.AddOp = AddOp;
  331. var AddUniqueOp = /*#__PURE__*/function (_Op5) {
  332. (0, _inherits2.default)(AddUniqueOp, _Op5);
  333. var _super5 = _createSuper(AddUniqueOp);
  334. function AddUniqueOp(value
  335. /*: mixed | Array<mixed>*/
  336. ) {
  337. var _this4;
  338. (0, _classCallCheck2.default)(this, AddUniqueOp);
  339. _this4 = _super5.call(this);
  340. (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this4), "_value", void 0);
  341. _this4._value = (0, _unique.default)((0, _isArray.default)(value) ? value : [value]);
  342. return _this4;
  343. }
  344. (0, _createClass2.default)(AddUniqueOp, [{
  345. key: "applyTo",
  346. value: function (value
  347. /*: mixed | Array<mixed>*/
  348. )
  349. /*: Array<mixed>*/
  350. {
  351. if (value == null) {
  352. return this._value || [];
  353. }
  354. if ((0, _isArray.default)(value)) {
  355. var _context;
  356. var toAdd = [];
  357. (0, _forEach.default)(_context = this._value).call(_context, function (v) {
  358. if (v instanceof _ParseObject.default) {
  359. if (!(0, _arrayContainsObject.default)(value, v)) {
  360. toAdd.push(v);
  361. }
  362. } else {
  363. if ((0, _indexOf.default)(value).call(value, v) < 0) {
  364. toAdd.push(v);
  365. }
  366. }
  367. });
  368. return (0, _concat.default)(value).call(value, toAdd);
  369. }
  370. throw new Error('Cannot add elements to a non-array value');
  371. }
  372. }, {
  373. key: "mergeWith",
  374. value: function (previous
  375. /*: Op*/
  376. )
  377. /*: Op*/
  378. {
  379. if (!previous) {
  380. return this;
  381. }
  382. if (previous instanceof SetOp) {
  383. return new SetOp(this.applyTo(previous._value));
  384. }
  385. if (previous instanceof UnsetOp) {
  386. return new SetOp(this._value);
  387. }
  388. if (previous instanceof AddUniqueOp) {
  389. return new AddUniqueOp(this.applyTo(previous._value));
  390. }
  391. throw new Error('Cannot merge AddUnique Op with the previous Op');
  392. }
  393. }, {
  394. key: "toJSON",
  395. value: function ()
  396. /*: { __op: string, objects: mixed }*/
  397. {
  398. return {
  399. __op: 'AddUnique',
  400. objects: (0, _encode.default)(this._value, false, true)
  401. };
  402. }
  403. }]);
  404. return AddUniqueOp;
  405. }(Op);
  406. exports.AddUniqueOp = AddUniqueOp;
  407. var RemoveOp = /*#__PURE__*/function (_Op6) {
  408. (0, _inherits2.default)(RemoveOp, _Op6);
  409. var _super6 = _createSuper(RemoveOp);
  410. function RemoveOp(value
  411. /*: mixed | Array<mixed>*/
  412. ) {
  413. var _this5;
  414. (0, _classCallCheck2.default)(this, RemoveOp);
  415. _this5 = _super6.call(this);
  416. (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this5), "_value", void 0);
  417. _this5._value = (0, _unique.default)((0, _isArray.default)(value) ? value : [value]);
  418. return _this5;
  419. }
  420. (0, _createClass2.default)(RemoveOp, [{
  421. key: "applyTo",
  422. value: function (value
  423. /*: mixed | Array<mixed>*/
  424. )
  425. /*: Array<mixed>*/
  426. {
  427. if (value == null) {
  428. return [];
  429. }
  430. if ((0, _isArray.default)(value)) {
  431. // var i = value.indexOf(this._value);
  432. var removed = (0, _concat.default)(value).call(value, []);
  433. for (var i = 0; i < this._value.length; i++) {
  434. var index = (0, _indexOf.default)(removed).call(removed, this._value[i]);
  435. while (index > -1) {
  436. (0, _splice.default)(removed).call(removed, index, 1);
  437. index = (0, _indexOf.default)(removed).call(removed, this._value[i]);
  438. }
  439. if (this._value[i] instanceof _ParseObject.default && this._value[i].id) {
  440. for (var j = 0; j < removed.length; j++) {
  441. if (removed[j] instanceof _ParseObject.default && this._value[i].id === removed[j].id) {
  442. (0, _splice.default)(removed).call(removed, j, 1);
  443. j--;
  444. }
  445. }
  446. }
  447. }
  448. return removed;
  449. }
  450. throw new Error('Cannot remove elements from a non-array value');
  451. }
  452. }, {
  453. key: "mergeWith",
  454. value: function (previous
  455. /*: Op*/
  456. )
  457. /*: Op*/
  458. {
  459. if (!previous) {
  460. return this;
  461. }
  462. if (previous instanceof SetOp) {
  463. return new SetOp(this.applyTo(previous._value));
  464. }
  465. if (previous instanceof UnsetOp) {
  466. return new UnsetOp();
  467. }
  468. if (previous instanceof RemoveOp) {
  469. var _context2;
  470. var uniques = (0, _concat.default)(_context2 = previous._value).call(_context2, []);
  471. for (var i = 0; i < this._value.length; i++) {
  472. if (this._value[i] instanceof _ParseObject.default) {
  473. if (!(0, _arrayContainsObject.default)(uniques, this._value[i])) {
  474. uniques.push(this._value[i]);
  475. }
  476. } else {
  477. if ((0, _indexOf.default)(uniques).call(uniques, this._value[i]) < 0) {
  478. uniques.push(this._value[i]);
  479. }
  480. }
  481. }
  482. return new RemoveOp(uniques);
  483. }
  484. throw new Error('Cannot merge Remove Op with the previous Op');
  485. }
  486. }, {
  487. key: "toJSON",
  488. value: function ()
  489. /*: { __op: string, objects: mixed }*/
  490. {
  491. return {
  492. __op: 'Remove',
  493. objects: (0, _encode.default)(this._value, false, true)
  494. };
  495. }
  496. }]);
  497. return RemoveOp;
  498. }(Op);
  499. exports.RemoveOp = RemoveOp;
  500. var RelationOp = /*#__PURE__*/function (_Op7) {
  501. (0, _inherits2.default)(RelationOp, _Op7);
  502. var _super7 = _createSuper(RelationOp);
  503. function RelationOp(adds
  504. /*: Array<ParseObject | string>*/
  505. , removes
  506. /*: Array<ParseObject | string>*/
  507. ) {
  508. var _this6;
  509. (0, _classCallCheck2.default)(this, RelationOp);
  510. _this6 = _super7.call(this);
  511. (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this6), "_targetClassName", void 0);
  512. (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this6), "relationsToAdd", void 0);
  513. (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this6), "relationsToRemove", void 0);
  514. _this6._targetClassName = null;
  515. if ((0, _isArray.default)(adds)) {
  516. _this6.relationsToAdd = (0, _unique.default)((0, _map.default)(adds).call(adds, _this6._extractId, (0, _assertThisInitialized2.default)(_this6)));
  517. }
  518. if ((0, _isArray.default)(removes)) {
  519. _this6.relationsToRemove = (0, _unique.default)((0, _map.default)(removes).call(removes, _this6._extractId, (0, _assertThisInitialized2.default)(_this6)));
  520. }
  521. return _this6;
  522. }
  523. (0, _createClass2.default)(RelationOp, [{
  524. key: "_extractId",
  525. value: function (obj
  526. /*: string | ParseObject*/
  527. )
  528. /*: string*/
  529. {
  530. if (typeof obj === 'string') {
  531. return obj;
  532. }
  533. if (!obj.id) {
  534. throw new Error('You cannot add or remove an unsaved Parse Object from a relation');
  535. }
  536. if (!this._targetClassName) {
  537. this._targetClassName = obj.className;
  538. }
  539. if (this._targetClassName !== obj.className) {
  540. var _context3;
  541. throw new Error((0, _concat.default)(_context3 = "Tried to create a Relation with 2 different object types: ".concat(this._targetClassName, " and ")).call(_context3, obj.className, "."));
  542. }
  543. return obj.id;
  544. }
  545. }, {
  546. key: "applyTo",
  547. value: function (value
  548. /*: mixed*/
  549. , object
  550. /*:: ?: { className: string, id: ?string }*/
  551. , key
  552. /*:: ?: string*/
  553. )
  554. /*: ?ParseRelation*/
  555. {
  556. if (!value) {
  557. var _context4;
  558. if (!object || !key) {
  559. throw new Error('Cannot apply a RelationOp without either a previous value, or an object and a key');
  560. }
  561. var parent = new _ParseObject.default(object.className);
  562. if (object.id && (0, _indexOf.default)(_context4 = object.id).call(_context4, 'local') === 0) {
  563. parent._localId = object.id;
  564. } else if (object.id) {
  565. parent.id = object.id;
  566. }
  567. var relation = new _ParseRelation.default(parent, key);
  568. relation.targetClassName = this._targetClassName;
  569. return relation;
  570. }
  571. if (value instanceof _ParseRelation.default) {
  572. if (this._targetClassName) {
  573. if (value.targetClassName) {
  574. if (this._targetClassName !== value.targetClassName) {
  575. var _context5;
  576. throw new Error((0, _concat.default)(_context5 = "Related object must be a ".concat(value.targetClassName, ", but a ")).call(_context5, this._targetClassName, " was passed in."));
  577. }
  578. } else {
  579. value.targetClassName = this._targetClassName;
  580. }
  581. }
  582. return value;
  583. }
  584. throw new Error('Relation cannot be applied to a non-relation field');
  585. }
  586. }, {
  587. key: "mergeWith",
  588. value: function (previous
  589. /*: Op*/
  590. )
  591. /*: Op*/
  592. {
  593. if (!previous) {
  594. return this;
  595. }
  596. if (previous instanceof UnsetOp) {
  597. throw new Error('You cannot modify a relation after deleting it.');
  598. }
  599. if (previous instanceof SetOp && previous._value instanceof _ParseRelation.default) {
  600. return this;
  601. }
  602. if (previous instanceof RelationOp) {
  603. var _context7, _context8, _context9, _context10, _context11, _context12;
  604. if (previous._targetClassName && previous._targetClassName !== this._targetClassName) {
  605. var _context6;
  606. throw new Error((0, _concat.default)(_context6 = "Related object must be of class ".concat(previous._targetClassName, ", but ")).call(_context6, this._targetClassName || 'null', " was passed in."));
  607. }
  608. var newAdd = (0, _concat.default)(_context7 = previous.relationsToAdd).call(_context7, []);
  609. (0, _forEach.default)(_context8 = this.relationsToRemove).call(_context8, function (r) {
  610. var index = (0, _indexOf.default)(newAdd).call(newAdd, r);
  611. if (index > -1) {
  612. (0, _splice.default)(newAdd).call(newAdd, index, 1);
  613. }
  614. });
  615. (0, _forEach.default)(_context9 = this.relationsToAdd).call(_context9, function (r) {
  616. var index = (0, _indexOf.default)(newAdd).call(newAdd, r);
  617. if (index < 0) {
  618. newAdd.push(r);
  619. }
  620. });
  621. var newRemove = (0, _concat.default)(_context10 = previous.relationsToRemove).call(_context10, []);
  622. (0, _forEach.default)(_context11 = this.relationsToAdd).call(_context11, function (r) {
  623. var index = (0, _indexOf.default)(newRemove).call(newRemove, r);
  624. if (index > -1) {
  625. (0, _splice.default)(newRemove).call(newRemove, index, 1);
  626. }
  627. });
  628. (0, _forEach.default)(_context12 = this.relationsToRemove).call(_context12, function (r) {
  629. var index = (0, _indexOf.default)(newRemove).call(newRemove, r);
  630. if (index < 0) {
  631. newRemove.push(r);
  632. }
  633. });
  634. var newRelation = new RelationOp(newAdd, newRemove);
  635. newRelation._targetClassName = this._targetClassName;
  636. return newRelation;
  637. }
  638. throw new Error('Cannot merge Relation Op with the previous Op');
  639. }
  640. }, {
  641. key: "toJSON",
  642. value: function ()
  643. /*: { __op?: string, objects?: mixed, ops?: mixed }*/
  644. {
  645. var _this7 = this;
  646. var idToPointer = function (id) {
  647. return {
  648. __type: 'Pointer',
  649. className: _this7._targetClassName,
  650. objectId: id
  651. };
  652. };
  653. var adds = null;
  654. var removes = null;
  655. var pointers = null;
  656. if (this.relationsToAdd.length > 0) {
  657. var _context13;
  658. pointers = (0, _map.default)(_context13 = this.relationsToAdd).call(_context13, idToPointer);
  659. adds = {
  660. __op: 'AddRelation',
  661. objects: pointers
  662. };
  663. }
  664. if (this.relationsToRemove.length > 0) {
  665. var _context14;
  666. pointers = (0, _map.default)(_context14 = this.relationsToRemove).call(_context14, idToPointer);
  667. removes = {
  668. __op: 'RemoveRelation',
  669. objects: pointers
  670. };
  671. }
  672. if (adds && removes) {
  673. return {
  674. __op: 'Batch',
  675. ops: [adds, removes]
  676. };
  677. }
  678. return adds || removes || {};
  679. }
  680. }]);
  681. return RelationOp;
  682. }(Op);
  683. exports.RelationOp = RelationOp;