ParseOp.js 22 KB

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