ParseOp.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. * @flow
  55. */
  56. function opFromJSON(json /*: { [key: string]: any }*/) /*: ?Op*/{
  57. if (!json || !json.__op) {
  58. return null;
  59. }
  60. switch (json.__op) {
  61. case 'Delete':
  62. return new UnsetOp();
  63. case 'Increment':
  64. return new IncrementOp(json.amount);
  65. case 'Add':
  66. return new AddOp((0, _decode.default)(json.objects));
  67. case 'AddUnique':
  68. return new AddUniqueOp((0, _decode.default)(json.objects));
  69. case 'Remove':
  70. return new RemoveOp((0, _decode.default)(json.objects));
  71. case 'AddRelation':
  72. {
  73. var toAdd = (0, _decode.default)(json.objects);
  74. if (!(0, _isArray.default)(toAdd)) {
  75. return new RelationOp([], []);
  76. }
  77. return new RelationOp(toAdd, []);
  78. }
  79. case 'RemoveRelation':
  80. {
  81. var toRemove = (0, _decode.default)(json.objects);
  82. if (!(0, _isArray.default)(toRemove)) {
  83. return new RelationOp([], []);
  84. }
  85. return new RelationOp([], toRemove);
  86. }
  87. case 'Batch':
  88. {
  89. var _toAdd = [];
  90. var _toRemove = [];
  91. for (var i = 0; i < json.ops.length; i++) {
  92. if (json.ops[i].__op === 'AddRelation') {
  93. _toAdd = (0, _concat.default)(_toAdd).call(_toAdd, (0, _decode.default)(json.ops[i].objects));
  94. } else if (json.ops[i].__op === 'RemoveRelation') {
  95. _toRemove = (0, _concat.default)(_toRemove).call(_toRemove, (0, _decode.default)(json.ops[i].objects));
  96. }
  97. }
  98. return new RelationOp(_toAdd, _toRemove);
  99. }
  100. }
  101. return null;
  102. }
  103. var Op = /*#__PURE__*/function () {
  104. function Op() {
  105. (0, _classCallCheck2.default)(this, Op);
  106. }
  107. (0, _createClass2.default)(Op, [{
  108. key: "applyTo",
  109. value:
  110. // Empty parent class
  111. function () /*: mixed*/{} /* eslint-disable-line no-unused-vars */
  112. }, {
  113. key: "mergeWith",
  114. value: function () /*: ?Op*/{} /* eslint-disable-line no-unused-vars */
  115. }, {
  116. key: "toJSON",
  117. value: function () /*: mixed*/{}
  118. }]);
  119. return Op;
  120. }();
  121. exports.Op = Op;
  122. var SetOp = /*#__PURE__*/function (_Op) {
  123. (0, _inherits2.default)(SetOp, _Op);
  124. var _super = _createSuper(SetOp);
  125. function SetOp(value /*: mixed*/) {
  126. var _this;
  127. (0, _classCallCheck2.default)(this, SetOp);
  128. _this = _super.call(this);
  129. (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "_value", void 0);
  130. _this._value = value;
  131. return _this;
  132. }
  133. (0, _createClass2.default)(SetOp, [{
  134. key: "applyTo",
  135. value: function () /*: mixed*/{
  136. return this._value;
  137. }
  138. }, {
  139. key: "mergeWith",
  140. value: function () /*: SetOp*/{
  141. return new SetOp(this._value);
  142. }
  143. }, {
  144. key: "toJSON",
  145. value: function (offline /*:: ?: boolean*/) {
  146. return (0, _encode.default)(this._value, false, true, undefined, offline);
  147. }
  148. }]);
  149. return SetOp;
  150. }(Op);
  151. exports.SetOp = SetOp;
  152. var UnsetOp = /*#__PURE__*/function (_Op2) {
  153. (0, _inherits2.default)(UnsetOp, _Op2);
  154. var _super2 = _createSuper(UnsetOp);
  155. function UnsetOp() {
  156. (0, _classCallCheck2.default)(this, UnsetOp);
  157. return _super2.apply(this, arguments);
  158. }
  159. (0, _createClass2.default)(UnsetOp, [{
  160. key: "applyTo",
  161. value: function () {
  162. return undefined;
  163. }
  164. }, {
  165. key: "mergeWith",
  166. value: function () /*: UnsetOp*/{
  167. return new UnsetOp();
  168. }
  169. }, {
  170. key: "toJSON",
  171. value: function () /*: { __op: string }*/{
  172. return {
  173. __op: 'Delete'
  174. };
  175. }
  176. }]);
  177. return UnsetOp;
  178. }(Op);
  179. exports.UnsetOp = UnsetOp;
  180. var IncrementOp = /*#__PURE__*/function (_Op3) {
  181. (0, _inherits2.default)(IncrementOp, _Op3);
  182. var _super3 = _createSuper(IncrementOp);
  183. function IncrementOp(amount /*: number*/) {
  184. var _this2;
  185. (0, _classCallCheck2.default)(this, IncrementOp);
  186. _this2 = _super3.call(this);
  187. (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this2), "_amount", void 0);
  188. if (typeof amount !== 'number') {
  189. throw new TypeError('Increment Op must be initialized with a numeric amount.');
  190. }
  191. _this2._amount = amount;
  192. return _this2;
  193. }
  194. (0, _createClass2.default)(IncrementOp, [{
  195. key: "applyTo",
  196. value: function (value /*: ?mixed*/) /*: number*/{
  197. if (typeof value === 'undefined') {
  198. return this._amount;
  199. }
  200. if (typeof value !== 'number') {
  201. throw new TypeError('Cannot increment a non-numeric value.');
  202. }
  203. return this._amount + value;
  204. }
  205. }, {
  206. key: "mergeWith",
  207. value: function (previous /*: Op*/) /*: Op*/{
  208. if (!previous) {
  209. return this;
  210. }
  211. if (previous instanceof SetOp) {
  212. return new SetOp(this.applyTo(previous._value));
  213. }
  214. if (previous instanceof UnsetOp) {
  215. return new SetOp(this._amount);
  216. }
  217. if (previous instanceof IncrementOp) {
  218. return new IncrementOp(this.applyTo(previous._amount));
  219. }
  220. throw new Error('Cannot merge Increment Op with the previous Op');
  221. }
  222. }, {
  223. key: "toJSON",
  224. value: function () /*: { __op: string, amount: number }*/{
  225. return {
  226. __op: 'Increment',
  227. amount: this._amount
  228. };
  229. }
  230. }]);
  231. return IncrementOp;
  232. }(Op);
  233. exports.IncrementOp = IncrementOp;
  234. var AddOp = /*#__PURE__*/function (_Op4) {
  235. (0, _inherits2.default)(AddOp, _Op4);
  236. var _super4 = _createSuper(AddOp);
  237. function AddOp(value /*: mixed | Array<mixed>*/) {
  238. var _this3;
  239. (0, _classCallCheck2.default)(this, AddOp);
  240. _this3 = _super4.call(this);
  241. (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this3), "_value", void 0);
  242. _this3._value = (0, _isArray.default)(value) ? value : [value];
  243. return _this3;
  244. }
  245. (0, _createClass2.default)(AddOp, [{
  246. key: "applyTo",
  247. value: function (value /*: mixed*/) /*: Array<mixed>*/{
  248. if (value == null) {
  249. return this._value;
  250. }
  251. if ((0, _isArray.default)(value)) {
  252. return (0, _concat.default)(value).call(value, this._value);
  253. }
  254. throw new Error('Cannot add elements to a non-array value');
  255. }
  256. }, {
  257. key: "mergeWith",
  258. value: function (previous /*: Op*/) /*: Op*/{
  259. if (!previous) {
  260. return this;
  261. }
  262. if (previous instanceof SetOp) {
  263. return new SetOp(this.applyTo(previous._value));
  264. }
  265. if (previous instanceof UnsetOp) {
  266. return new SetOp(this._value);
  267. }
  268. if (previous instanceof AddOp) {
  269. return new AddOp(this.applyTo(previous._value));
  270. }
  271. throw new Error('Cannot merge Add Op with the previous Op');
  272. }
  273. }, {
  274. key: "toJSON",
  275. value: function () /*: { __op: string, objects: mixed }*/{
  276. return {
  277. __op: 'Add',
  278. objects: (0, _encode.default)(this._value, false, true)
  279. };
  280. }
  281. }]);
  282. return AddOp;
  283. }(Op);
  284. exports.AddOp = AddOp;
  285. var AddUniqueOp = /*#__PURE__*/function (_Op5) {
  286. (0, _inherits2.default)(AddUniqueOp, _Op5);
  287. var _super5 = _createSuper(AddUniqueOp);
  288. function AddUniqueOp(value /*: mixed | Array<mixed>*/) {
  289. var _this4;
  290. (0, _classCallCheck2.default)(this, AddUniqueOp);
  291. _this4 = _super5.call(this);
  292. (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this4), "_value", void 0);
  293. _this4._value = (0, _unique.default)((0, _isArray.default)(value) ? value : [value]);
  294. return _this4;
  295. }
  296. (0, _createClass2.default)(AddUniqueOp, [{
  297. key: "applyTo",
  298. value: function (value /*: mixed | Array<mixed>*/) /*: Array<mixed>*/{
  299. if (value == null) {
  300. return this._value || [];
  301. }
  302. if ((0, _isArray.default)(value)) {
  303. var _context;
  304. var toAdd = [];
  305. (0, _forEach.default)(_context = this._value).call(_context, function (v) {
  306. if (v instanceof _ParseObject.default) {
  307. if (!(0, _arrayContainsObject.default)(value, v)) {
  308. toAdd.push(v);
  309. }
  310. } else {
  311. if ((0, _indexOf.default)(value).call(value, v) < 0) {
  312. toAdd.push(v);
  313. }
  314. }
  315. });
  316. return (0, _concat.default)(value).call(value, toAdd);
  317. }
  318. throw new Error('Cannot add elements to a non-array value');
  319. }
  320. }, {
  321. key: "mergeWith",
  322. value: function (previous /*: Op*/) /*: Op*/{
  323. if (!previous) {
  324. return this;
  325. }
  326. if (previous instanceof SetOp) {
  327. return new SetOp(this.applyTo(previous._value));
  328. }
  329. if (previous instanceof UnsetOp) {
  330. return new SetOp(this._value);
  331. }
  332. if (previous instanceof AddUniqueOp) {
  333. return new AddUniqueOp(this.applyTo(previous._value));
  334. }
  335. throw new Error('Cannot merge AddUnique Op with the previous Op');
  336. }
  337. }, {
  338. key: "toJSON",
  339. value: function () /*: { __op: string, objects: mixed }*/{
  340. return {
  341. __op: 'AddUnique',
  342. objects: (0, _encode.default)(this._value, false, true)
  343. };
  344. }
  345. }]);
  346. return AddUniqueOp;
  347. }(Op);
  348. exports.AddUniqueOp = AddUniqueOp;
  349. var RemoveOp = /*#__PURE__*/function (_Op6) {
  350. (0, _inherits2.default)(RemoveOp, _Op6);
  351. var _super6 = _createSuper(RemoveOp);
  352. function RemoveOp(value /*: mixed | Array<mixed>*/) {
  353. var _this5;
  354. (0, _classCallCheck2.default)(this, RemoveOp);
  355. _this5 = _super6.call(this);
  356. (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this5), "_value", void 0);
  357. _this5._value = (0, _unique.default)((0, _isArray.default)(value) ? value : [value]);
  358. return _this5;
  359. }
  360. (0, _createClass2.default)(RemoveOp, [{
  361. key: "applyTo",
  362. value: function (value /*: mixed | Array<mixed>*/) /*: Array<mixed>*/{
  363. if (value == null) {
  364. return [];
  365. }
  366. if ((0, _isArray.default)(value)) {
  367. // var i = value.indexOf(this._value);
  368. var removed = (0, _concat.default)(value).call(value, []);
  369. for (var i = 0; i < this._value.length; i++) {
  370. var index = (0, _indexOf.default)(removed).call(removed, this._value[i]);
  371. while (index > -1) {
  372. (0, _splice.default)(removed).call(removed, index, 1);
  373. index = (0, _indexOf.default)(removed).call(removed, this._value[i]);
  374. }
  375. if (this._value[i] instanceof _ParseObject.default && this._value[i].id) {
  376. for (var j = 0; j < removed.length; j++) {
  377. if (removed[j] instanceof _ParseObject.default && this._value[i].id === removed[j].id) {
  378. (0, _splice.default)(removed).call(removed, j, 1);
  379. j--;
  380. }
  381. }
  382. }
  383. }
  384. return removed;
  385. }
  386. throw new Error('Cannot remove elements from a non-array value');
  387. }
  388. }, {
  389. key: "mergeWith",
  390. value: function (previous /*: Op*/) /*: Op*/{
  391. if (!previous) {
  392. return this;
  393. }
  394. if (previous instanceof SetOp) {
  395. return new SetOp(this.applyTo(previous._value));
  396. }
  397. if (previous instanceof UnsetOp) {
  398. return new UnsetOp();
  399. }
  400. if (previous instanceof RemoveOp) {
  401. var _context2;
  402. var uniques = (0, _concat.default)(_context2 = previous._value).call(_context2, []);
  403. for (var i = 0; i < this._value.length; i++) {
  404. if (this._value[i] instanceof _ParseObject.default) {
  405. if (!(0, _arrayContainsObject.default)(uniques, this._value[i])) {
  406. uniques.push(this._value[i]);
  407. }
  408. } else {
  409. if ((0, _indexOf.default)(uniques).call(uniques, this._value[i]) < 0) {
  410. uniques.push(this._value[i]);
  411. }
  412. }
  413. }
  414. return new RemoveOp(uniques);
  415. }
  416. throw new Error('Cannot merge Remove Op with the previous Op');
  417. }
  418. }, {
  419. key: "toJSON",
  420. value: function () /*: { __op: string, objects: mixed }*/{
  421. return {
  422. __op: 'Remove',
  423. objects: (0, _encode.default)(this._value, false, true)
  424. };
  425. }
  426. }]);
  427. return RemoveOp;
  428. }(Op);
  429. exports.RemoveOp = RemoveOp;
  430. var RelationOp = /*#__PURE__*/function (_Op7) {
  431. (0, _inherits2.default)(RelationOp, _Op7);
  432. var _super7 = _createSuper(RelationOp);
  433. function RelationOp(adds /*: Array<ParseObject | string>*/, removes /*: Array<ParseObject | string>*/) {
  434. var _this6;
  435. (0, _classCallCheck2.default)(this, RelationOp);
  436. _this6 = _super7.call(this);
  437. (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this6), "_targetClassName", void 0);
  438. (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this6), "relationsToAdd", void 0);
  439. (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this6), "relationsToRemove", void 0);
  440. _this6._targetClassName = null;
  441. if ((0, _isArray.default)(adds)) {
  442. _this6.relationsToAdd = (0, _unique.default)((0, _map.default)(adds).call(adds, _this6._extractId, (0, _assertThisInitialized2.default)(_this6)));
  443. }
  444. if ((0, _isArray.default)(removes)) {
  445. _this6.relationsToRemove = (0, _unique.default)((0, _map.default)(removes).call(removes, _this6._extractId, (0, _assertThisInitialized2.default)(_this6)));
  446. }
  447. return _this6;
  448. }
  449. (0, _createClass2.default)(RelationOp, [{
  450. key: "_extractId",
  451. value: function (obj /*: string | ParseObject*/) /*: string*/{
  452. if (typeof obj === 'string') {
  453. return obj;
  454. }
  455. if (!obj.id) {
  456. throw new Error('You cannot add or remove an unsaved Parse Object from a relation');
  457. }
  458. if (!this._targetClassName) {
  459. this._targetClassName = obj.className;
  460. }
  461. if (this._targetClassName !== obj.className) {
  462. throw new Error('Tried to create a Relation with 2 different object types: ' + this._targetClassName + ' and ' + obj.className + '.');
  463. }
  464. return obj.id;
  465. }
  466. }, {
  467. key: "applyTo",
  468. value: function (value /*: mixed*/, object /*:: ?: { className: string, id: ?string }*/, key /*:: ?: string*/) /*: ?ParseRelation*/{
  469. if (!value) {
  470. var _context3;
  471. if (!object || !key) {
  472. throw new Error('Cannot apply a RelationOp without either a previous value, or an object and a key');
  473. }
  474. var parent = new _ParseObject.default(object.className);
  475. if (object.id && (0, _indexOf.default)(_context3 = object.id).call(_context3, 'local') === 0) {
  476. parent._localId = object.id;
  477. } else if (object.id) {
  478. parent.id = object.id;
  479. }
  480. var relation = new _ParseRelation.default(parent, key);
  481. relation.targetClassName = this._targetClassName;
  482. return relation;
  483. }
  484. if (value instanceof _ParseRelation.default) {
  485. if (this._targetClassName) {
  486. if (value.targetClassName) {
  487. if (this._targetClassName !== value.targetClassName) {
  488. throw new Error('Related object must be a ' + value.targetClassName + ', but a ' + this._targetClassName + ' was passed in.');
  489. }
  490. } else {
  491. value.targetClassName = this._targetClassName;
  492. }
  493. }
  494. return value;
  495. } else {
  496. throw new Error('Relation cannot be applied to a non-relation field');
  497. }
  498. }
  499. }, {
  500. key: "mergeWith",
  501. value: function (previous /*: Op*/) /*: Op*/{
  502. if (!previous) {
  503. return this;
  504. } else if (previous instanceof UnsetOp) {
  505. throw new Error('You cannot modify a relation after deleting it.');
  506. } else if (previous instanceof SetOp && previous._value instanceof _ParseRelation.default) {
  507. return this;
  508. } else if (previous instanceof RelationOp) {
  509. var _context4, _context5, _context6, _context7, _context8, _context9;
  510. if (previous._targetClassName && previous._targetClassName !== this._targetClassName) {
  511. throw new Error('Related object must be of class ' + previous._targetClassName + ', but ' + (this._targetClassName || 'null') + ' was passed in.');
  512. }
  513. var newAdd = (0, _concat.default)(_context4 = previous.relationsToAdd).call(_context4, []);
  514. (0, _forEach.default)(_context5 = this.relationsToRemove).call(_context5, function (r) {
  515. var index = (0, _indexOf.default)(newAdd).call(newAdd, r);
  516. if (index > -1) {
  517. (0, _splice.default)(newAdd).call(newAdd, index, 1);
  518. }
  519. });
  520. (0, _forEach.default)(_context6 = this.relationsToAdd).call(_context6, function (r) {
  521. var index = (0, _indexOf.default)(newAdd).call(newAdd, r);
  522. if (index < 0) {
  523. newAdd.push(r);
  524. }
  525. });
  526. var newRemove = (0, _concat.default)(_context7 = previous.relationsToRemove).call(_context7, []);
  527. (0, _forEach.default)(_context8 = this.relationsToAdd).call(_context8, function (r) {
  528. var index = (0, _indexOf.default)(newRemove).call(newRemove, r);
  529. if (index > -1) {
  530. (0, _splice.default)(newRemove).call(newRemove, index, 1);
  531. }
  532. });
  533. (0, _forEach.default)(_context9 = this.relationsToRemove).call(_context9, function (r) {
  534. var index = (0, _indexOf.default)(newRemove).call(newRemove, r);
  535. if (index < 0) {
  536. newRemove.push(r);
  537. }
  538. });
  539. var newRelation = new RelationOp(newAdd, newRemove);
  540. newRelation._targetClassName = this._targetClassName;
  541. return newRelation;
  542. }
  543. throw new Error('Cannot merge Relation Op with the previous Op');
  544. }
  545. }, {
  546. key: "toJSON",
  547. value: function () /*: { __op?: string, objects?: mixed, ops?: mixed }*/{
  548. var _this7 = this;
  549. var idToPointer = function (id) {
  550. return {
  551. __type: 'Pointer',
  552. className: _this7._targetClassName,
  553. objectId: id
  554. };
  555. };
  556. var adds = null;
  557. var removes = null;
  558. var pointers = null;
  559. if (this.relationsToAdd.length > 0) {
  560. var _context10;
  561. pointers = (0, _map.default)(_context10 = this.relationsToAdd).call(_context10, idToPointer);
  562. adds = {
  563. __op: 'AddRelation',
  564. objects: pointers
  565. };
  566. }
  567. if (this.relationsToRemove.length > 0) {
  568. var _context11;
  569. pointers = (0, _map.default)(_context11 = this.relationsToRemove).call(_context11, idToPointer);
  570. removes = {
  571. __op: 'RemoveRelation',
  572. objects: pointers
  573. };
  574. }
  575. if (adds && removes) {
  576. return {
  577. __op: 'Batch',
  578. ops: [adds, removes]
  579. };
  580. }
  581. return adds || removes || {};
  582. }
  583. }]);
  584. return RelationOp;
  585. }(Op);
  586. exports.RelationOp = RelationOp;